Example usage for java.awt.geom Point2D distanceSq

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

Introduction

In this page you can find the example usage for java.awt.geom Point2D 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:GeometryUtilities.java

public static Vector<Point2D> getCrossings(Arc2D arc0, Point2D arc0Center, Arc2D arc1, Point2D arc1Center) {
    Vector<Point2D> ret = new Vector<Point2D>();

    double distance = arc0Center.distance(arc1Center);
    double radius0Squared = arc0Center.distanceSq(arc0.getStartPoint());
    double radius0 = sqrt(radius0Squared);
    double radius1Squared = arc1Center.distanceSq(arc1.getStartPoint());
    double radius1 = sqrt(radius1Squared);

    if (distance > radius0 + radius1) {
        // There are no solutions because the circles are separate.
    } else if (distance < abs(radius0 - radius1)) {
        // There are no solutions because one circle is contained within the
        // other.
    } else if (distance == 0 && radius0 == radius1) {
        // There are an infinite number of solutions because the circles are
        // coincident.
    } else {/*  w w  w  .  j a  va  2  s  .  c om*/
        // Calculate the first intersection
        double x0 = arc0Center.getX(), y0 = arc0Center.getY();
        double x1 = arc1Center.getX(), y1 = arc1Center.getY();

        double a = (radius0Squared - radius1Squared + distance * distance) / (2 * distance);
        double h = sqrt(radius0Squared - a * a);

        double x2 = x0 + a * (x1 - x0) / distance;
        double y2 = y0 + a * (y1 - y0) / distance;

        Point2D.Double intersection = new Point2D.Double(x2 + h * (y1 - y0) / distance,
                y2 - h * (x1 - x0) / distance);
        double angle0ToIntersection = toDegrees(atan2(-(intersection.y - y0), intersection.x - x0));
        double angle1ToIntersection = toDegrees(atan2(-(intersection.y - y1), intersection.x - x1));
        if (arc0.containsAngle(angle0ToIntersection) && arc1.containsAngle(angle1ToIntersection))
            ret.add(intersection);

        // If the circles aren't tangential, calculate the second
        // intersection
        if (distance != radius0 + radius1) {
            intersection = new Point2D.Double(x2 - h * (y1 - y0) / distance, y2 + h * (x1 - x0) / distance);
            angle0ToIntersection = toDegrees(atan2(-(intersection.y - y0), intersection.x - x0));
            angle1ToIntersection = toDegrees(atan2(-(intersection.y - y1), intersection.x - x1));
            if (arc0.containsAngle(angle0ToIntersection) && arc1.containsAngle(angle1ToIntersection))
                ret.add(intersection);
        }
    }

    return ret;
}

From source file:edu.uci.ics.jung.algorithms.layout.SpringLayout.java

protected void calculateRepulsion() {
    try {/*from   w w  w .  ja v  a 2s. c  om*/
        for (V v : getGraph().getVertices()) {
            if (isLocked(v))
                continue;

            SpringVertexData svd = springVertexData.get(v);
            if (svd == null)
                continue;
            double dx = 0, dy = 0;

            for (V v2 : getGraph().getVertices()) {
                if (v == v2)
                    continue;
                Point2D p = transform(v);
                Point2D p2 = transform(v2);
                if (p == null || p2 == null)
                    continue;
                double vx = p.getX() - p2.getX();
                double vy = p.getY() - p2.getY();
                double distanceSq = p.distanceSq(p2);
                if (distanceSq == 0) {
                    dx += Math.random();
                    dy += Math.random();
                } else if (distanceSq < repulsion_range_sq) {
                    double factor = 1;
                    dx += factor * vx / distanceSq;
                    dy += factor * vy / distanceSq;
                }
            }
            double dlen = dx * dx + dy * dy;
            if (dlen > 0) {
                dlen = Math.sqrt(dlen) / 2;
                svd.repulsiondx += dx / dlen;
                svd.repulsiondy += dy / dlen;
            }
        }
    } catch (ConcurrentModificationException cme) {
        calculateRepulsion();
    }
}

From source file:edu.uci.ics.jung.algorithms.layout.FRLayout2.java

protected void calcRepulsion(V v1) {
    Point2D fvd1 = frVertexData.get(v1);
    if (fvd1 == null)
        return;//w  w  w . j a  v  a2s  .  c  o m
    fvd1.setLocation(0, 0);
    boolean v1_locked = isLocked(v1);

    try {
        for (V v2 : getGraph().getVertices()) {

            boolean v2_locked = isLocked(v2);
            if (v1_locked && v2_locked)
                continue;
            if (v1 != v2) {
                Point2D p1 = transform(v1);
                Point2D p2 = transform(v2);
                if (p1 == null || p2 == null)
                    continue;
                double xDelta = p1.getX() - p2.getX();
                double yDelta = p1.getY() - p2.getY();

                double deltaLength = Math.max(EPSILON, p1.distanceSq(p2));

                double force = (repulsion_constant * repulsion_constant);// / deltaLength;

                double forceOverDeltaLength = force / deltaLength;

                assert Double.isNaN(
                        force) == false : "Unexpected mathematical result in FRLayout:calcPositions [repulsion]";

                if (v2_locked) {
                    // double the offset for v1, as v2 will not be moving in
                    // the opposite direction
                    fvd1.setLocation(fvd1.getX() + 2 * xDelta * forceOverDeltaLength,
                            fvd1.getY() + 2 * yDelta * forceOverDeltaLength);
                } else {
                    fvd1.setLocation(fvd1.getX() + xDelta * forceOverDeltaLength,
                            fvd1.getY() + yDelta * forceOverDeltaLength);
                }
            }
        }
    } catch (ConcurrentModificationException cme) {
        calcRepulsion(v1);
    }
}

From source file:com.wasteofplastic.beaconz.Register.java

/**
 * Checks if a beacon is within the range around point
 * @param point//  w  w w  .  j  a  va  2 s  . c  o  m
 * @param range
 * @return true if beacon is there, false if not
 */
public boolean isNearBeacon(Point2D point, int range) {
    int distSquared = range * range;
    for (Point2D beacon : beaconRegister.keySet()) {
        // Distance squared check is less computationally intensive than checking the square
        if (distSquared > point.distanceSq(beacon)) {
            return true;
        }
    }
    return false;
}

From source file:com.wasteofplastic.beaconz.Register.java

/**
 * Get a list of all nearby beacons within range
 * @param location//from   ww  w  .  j  a  v  a2 s  .c  o  m
 * @param range
 * @return list of nearby beacons
 */
public List<BeaconObj> getNearbyBeacons(Location location, int range) {
    int distSquared = range * range;
    List<BeaconObj> result = new ArrayList<BeaconObj>();
    Point2D point = new Point2D.Double(location.getX(), location.getZ());
    for (Point2D beacon : beaconRegister.keySet()) {
        // Distance squared check is less computationally intensive than checking the square
        if (distSquared > point.distanceSq(beacon)) {
            result.add(beaconRegister.get(beacon));
        }
    }
    return result;
}

From source file:org.jcurl.core.swing.RockEditDisplay.java

private int findHotSpeed(final Point2D wc, final Point2D dc) {
    Point2D tmp = new Point2D.Float();
    final double RR = hotRadiusDC * hotRadiusDC;
    for (int i = RockSet.ROCKS_PER_SET - 1; i >= 0; i--) {
        if ((1 << i & selectedMask) == 0)
            continue;
        tmp = getSpeedSpotWC(i, tmp);//from   ww  w  . ja va  2s.c om
        wc2dc(tmp, tmp);
        final double dist = tmp.distanceSq(dc);
        if (log.isDebugEnabled())
            log.debug("i=" + i + " dist=" + dist);
        if (dist <= RR)
            return i;
    }
    return -1;
}

From source file:util.ModSpringLayout.java

protected void calculateRepulsion() {
    try {/*from   w w w.  j  a v a  2  s.  co  m*/
        for (V v : getGraph().getVertices()) {
            if (isLocked(v)) {
                continue;
            }

            SpringVertexData svd = springVertexData.get(v);
            if (svd == null) {
                continue;
            }
            double dx = 0, dy = 0;

            for (V v2 : getGraph().getVertices()) {
                if (v == v2) {
                    continue;
                }
                Point2D p = transform(v);
                Point2D p2 = transform(v2);
                if (p == null || p2 == null) {
                    continue;
                }
                double vx = p.getX() - p2.getX();
                double vy = p.getY() - p2.getY();
                double distanceSq = p.distanceSq(p2);
                if (distanceSq == 0) {
                    dx += Math.random();
                    dy += Math.random();
                } else if (distanceSq < repulsion_range_sq) {
                    double factor = 1;
                    dx += factor * vx / distanceSq;
                    dy += factor * vy / distanceSq;
                }
            }
            double dlen = dx * dx + dy * dy;
            if (dlen > 0) {
                dlen = Math.sqrt(dlen) / 2;
                svd.repulsiondx += dx / dlen;
                svd.repulsiondy += dy / dlen;
            }
        }
    } catch (ConcurrentModificationException cme) {
        calculateRepulsion();
    }
}

From source file:util.ModSpringLayout2.java

protected void calculateRepulsion() {
    try {//from   w ww .  j  a  va 2  s .  c o  m
        for (V v : getGraph().getVertices()) {
            if (isLocked(v))
                continue;

            SpringVertexData svd = getSpringData(v);
            if (svd == null)
                continue;
            double dx = 0, dy = 0;

            for (V v2 : getGraph().getVertices()) {
                if (v == v2)
                    continue;
                Point2D p = transform(v);
                Point2D p2 = transform(v2);
                if (p == null || p2 == null)
                    continue;
                double vx = p.getX() - p2.getX();
                double vy = p.getY() - p2.getY();
                //                double distance = vx * vx + vy * vy;
                double distanceSq = p.distanceSq(p2);
                if (distanceSq == 0) {
                    dx += Math.random();
                    dy += Math.random();
                } else if (distanceSq < repulsion_range) {// * repulsion_range) {
                    double factor = 1;
                    dx += factor * vx / distanceSq;//Math.pow(distance, 2);
                    dy += factor * vy / distanceSq;//Math.pow(distance, 2);
                }
            }
            double dlen = dx * dx + dy * dy;
            if (dlen > 0) {
                dlen = Math.sqrt(dlen) / 2;
                svd.repulsiondx += dx / dlen;
                svd.repulsiondy += dy / dlen;
            }
        }
    } catch (ConcurrentModificationException cme) {
        calculateRepulsion();
    }
}

From source file:Visualizer.SpringLayoutWeighted.java

protected void calculateRepulsion() {
    try {//from ww w . ja  va 2 s.  c om
        for (Functionality.Node v : getGraph().getVertices()) {
            if (isLocked(v))
                continue;

            SpringVertexData svd = springVertexData.get(v);
            if (svd == null)
                continue;
            double dx = 0, dy = 0;

            for (Functionality.Node v2 : getGraph().getVertices()) {
                if (v == v2)
                    continue;
                Point2D p = transform(v);
                Point2D p2 = transform(v2);
                if (p == null || p2 == null)
                    continue;
                double vx = p.getX() - p2.getX();
                double vy = p.getY() - p2.getY();
                double distanceSq = p.distanceSq(p2);
                if (distanceSq == 0) {
                    dx += Math.random();
                    dy += Math.random();
                } else if (distanceSq < repulsion_range_sq) {
                    double factor = 1;
                    dx += factor * vx / distanceSq;
                    dy += factor * vy / distanceSq;
                }
            }
            double dlen = dx * dx + dy * dy;
            if (dlen > 0) {
                dlen = Math.sqrt(dlen) / 2;
                svd.repulsiondx += dx / dlen;
                svd.repulsiondy += dy / dlen;
            }
        }
    } catch (ConcurrentModificationException cme) {
        calculateRepulsion();
    }
}