Example usage for java.awt.geom Point2D.Double distanceSq

List of usage examples for java.awt.geom Point2D.Double distanceSq

Introduction

In this page you can find the example usage for java.awt.geom Point2D.Double distanceSq.

Prototype

public double distanceSq(Point2D pt) 

Source Link

Document

Returns the square of the distance from this Point2D to a specified Point2D .

Usage

From source file:MathFunctions.java

public static AffineTransform generateAffineTransformFromPointPairs(
        Map<Point2D.Double, Point2D.Double> pointPairs, double srcTol, double destTol) throws Exception {
    AffineTransform transform = generateAffineTransformFromPointPairs(pointPairs);
    double srcDevSqSum = 0;
    double destDevSqSum = 0;
    for (Map.Entry pair : pointPairs.entrySet()) {
        try {//w  ww .j ava2s .  co  m
            Point2D.Double srcPt = (Point2D.Double) pair.getKey();
            Point2D.Double destPt = (Point2D.Double) pair.getValue();

            Point2D.Double srcPt2 = (Point2D.Double) transform.inverseTransform(destPt, null);
            Point2D.Double destPt2 = (Point2D.Double) transform.transform(srcPt, null);

            srcDevSqSum += srcPt.distanceSq(srcPt2);
            destDevSqSum += destPt.distanceSq(destPt2);

        } catch (NoninvertibleTransformException ex) {
            throw new Exception();
        }
    }

    int n = pointPairs.size();
    double srcRMS = Math.sqrt(srcDevSqSum / n);
    double destRMS = Math.sqrt(destDevSqSum / n);

    if (srcRMS > srcTol || destRMS > destTol) {
        throw new Exception("Point mapping scatter exceeds tolerance.");
    }

    return transform;
}

From source file:biogenesis.Organism.java

/**
 * Finds if two organism are touching and if so applies the effects of the
 * collision.//from   w w  w . j a va  2s .c  o  m
 * 
 * @param org  The organism to check for collisions.
 * @return  true if the two organisms are touching, false otherwise.
 */
public final boolean contact(Organism org) {
    int i, j;
    ExLine2DDouble line = new ExLine2DDouble();
    ExLine2DDouble bline = new ExLine2DDouble();
    // Check collisions for all segments
    for (i = _segments - 1; i >= 0; i--) {
        // Consider only segments with modulus greater than 1
        if (_m[i] >= 1) {
            line.setLine(x1[i] + _centerX, y1[i] + _centerY, x2[i] + _centerX, y2[i] + _centerY);
            // First check if the line intersects the bounding box of the other organism
            if (org.intersectsLine(line)) {
                // Do the same for the other organism's segments.
                for (j = org._segments - 1; j >= 0; j--) {
                    if (org._m[j] >= 1) {
                        bline.setLine(org.x1[j] + org._centerX, org.y1[j] + org._centerY,
                                org.x2[j] + org._centerX, org.y2[j] + org._centerY);
                        if (intersectsLine(bline) && line.intersectsLine(bline)) {
                            // If we found two intersecting segments, apply effects
                            touchEffects(org, i, j, true);
                            // Intersection point
                            Point2D.Double intersec = line.getIntersection(bline);
                            /* touchMove needs to know which is the line that collides from the middle (not
                             * from a vertex). Try to guess it by finding the vertex nearest to the
                             * intersection point.
                             */
                            double dl1, dl2, dbl1, dbl2;
                            dl1 = intersec.distanceSq(line.getP1());
                            dl2 = intersec.distanceSq(line.getP2());
                            dbl1 = intersec.distanceSq(bline.getP1());
                            dbl2 = intersec.distanceSq(bline.getP2());
                            // Use this to send the best choice to touchMove
                            if (Math.min(dl1, dl2) < Math.min(dbl1, dbl2))
                                touchMove(org, intersec, bline, false);
                            else
                                touchMove(org, intersec, line, true);

                            OrganismCollidedEvent event = new OrganismCollidedEvent();
                            actionCollidedListeners.fire().perform(event);

                            // Find only one collision to speed up.
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}