Example usage for java.awt.geom Point2D distance

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

Introduction

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

Prototype

public double distance(Point2D pt) 

Source Link

Document

Returns the distance from this Point2D to a specified Point2D .

Usage

From source file:Main.java

public static boolean snapEquals2D(Point2D a, Point2D b, double snapTolerance) {
    return a.distance(b) <= snapTolerance;
}

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 {/*from  w  w  w.  j a v  a 2s  . c o m*/
        // 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:de.bund.bfr.jung.JungUtils.java

static Line2D getLineInMiddle(Shape edgeShape) {
    GeneralPath path = new GeneralPath(edgeShape);
    float[] seg = new float[6];
    List<Point2D> points = new ArrayList<>();

    for (PathIterator i = path.getPathIterator(null, 1); !i.isDone(); i.next()) {
        i.currentSegment(seg);/*from   w ww . ja va  2 s .  c  om*/
        points.add(new Point2D.Float(seg[0], seg[1]));
    }

    Point2D first = points.get(0);
    Point2D last = points.get(points.size() - 1);

    if (first.equals(last)) {
        Point2D minP = points.stream().min((p1, p2) -> Double.compare(p1.getY(), p2.getY())).get();

        return new Line2D.Float(minP, new Point2D.Float((float) (minP.getX() + 1.0), (float) minP.getY()));
    } else {
        for (int i = 0; i < points.size() - 1; i++) {
            Point2D p1 = points.get(i);
            Point2D p2 = points.get(i + 1);

            if (p2.distance(last) < p2.distance(first)) {
                Line2D ortho = getOrthogonal(new Line2D.Float(first, last));
                Point2D pp1 = getIntersection(new Line2D.Float(p1, p2), ortho);
                Point2D pp2 = new Point2D.Float((float) (pp1.getX() + last.getX() - first.getX()),
                        (float) (pp1.getY() + last.getY() - first.getY()));

                return new Line2D.Float(pp1, pp2);
            }
        }

        return null;
    }
}

From source file:algorithms.MedianDivergenceComputer.java

private void deriveMedianColormapToJNDRatio() {
    int len = points.size() / 2;
    ratios = new double[len];
    Iterator<Point2D> ptIt = points.iterator();

    int i = 0;//from   w  w w.ja v a  2 s  .c  om
    while (i < len && ptIt.hasNext()) {
        Point2D p1 = ptIt.next();
        Point2D p2 = ptIt.next();

        double dist = p1.distance(p2);

        Color colorA = colormap.getColor(p1.getX(), p1.getY());
        Color colorB = colormap.getColor(p2.getX(), p2.getY());

        // color distance
        double cdist = colorDiff(colorA, colorB);

        // filter zero divisions, as long as the value distance is small
        // DON'T protect colormaps that contain duplicate colors
        if (cdist == 0 && dist < 0.05)
            continue;
        double ratio = cdist / dist;
        ratios[i] = ratio;
        i++;
    }
    Arrays.sort(ratios);
}

From source file:algorithms.quality.ColorDivergenceVariance.java

@Override
public double getQuality(Colormap colormap) {
    DescriptiveStatistics stats = new DescriptiveStatistics();
    Iterator<Point2D> ptIt = strategy.getPoints().iterator();

    while (ptIt.hasNext()) {
        Point2D p1 = ptIt.next();

        if (!ptIt.hasNext())
            break;

        Point2D p2 = ptIt.next();

        double dist = p1.distance(p2);

        Color colorA = colormap.getColor(p1.getX(), p1.getY());
        Color colorB = colormap.getColor(p2.getX(), p2.getY());

        // roughly 0-100
        double cdist = MedianDivergenceComputer.colorDiff(colorA, colorB);

        double ratio = cdist / dist;

        stats.addValue(ratio);//w w  w.java2s .c  o  m
    }

    return stats.getStandardDeviation();
}

From source file:br.unicamp.rctapp.codelets.behaviors.GetClosestJewel.java

@Override
public void act() {
    String jewelName = "";
    closestJewel = (Thing) closestJewelMO.getI();
    cis = (CreatureInnerSense) innerSenseMO.getI();
    //Find distance between closest apple and self
    //If closer than reachDistance, eat the apple

    if (closestJewel != null) {
        double jewelX = 0;
        double jewelY = 0;
        try {// www.jav a  2  s .c o  m
            jewelX = closestJewel.getX1();
            jewelY = closestJewel.getY1();
            jewelName = closestJewel.getName();

        } catch (Exception e) {
            e.printStackTrace();
        }

        double selfX = cis.getPosition().getX();
        double selfY = cis.getPosition().getY();

        Point2D pApple = new Point();
        pApple.setLocation(jewelX, jewelY);

        Point2D pSelf = new Point();
        pSelf.setLocation(selfX, selfY);

        double distance = pSelf.distance(pApple);
        JSONObject message = new JSONObject();
        try {
            if (distance < reachDistance) { //eat it   
                message.put("OBJECT", jewelName);
                message.put("ACTION", "PICKUP");
                handsMO.updateI(message.toString());

            } else {
                handsMO.updateI(""); //nothing
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        handsMO.updateI(""); //nothing
    }
}

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

protected void calcAttraction(E e) {
    Pair<V> endpoints = getGraph().getEndpoints(e);
    V v1 = endpoints.getFirst();/*from   w  ww.jav  a 2 s.co  m*/
    V v2 = endpoints.getSecond();
    boolean v1_locked = isLocked(v1);
    boolean v2_locked = isLocked(v2);

    if (v1_locked && v2_locked) {
        // both locked, do nothing
        return;
    }
    Point2D p1 = transform(v1);
    Point2D p2 = transform(v2);
    if (p1 == null || p2 == null)
        return;
    double xDelta = p1.getX() - p2.getX();
    double yDelta = p1.getY() - p2.getY();

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

    double force = deltaLength / attraction_constant;

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

    double dx = xDelta * force;
    double dy = yDelta * force;
    Point2D fvd1 = frVertexData.get(v1);
    Point2D fvd2 = frVertexData.get(v2);
    if (v2_locked) {
        // double the offset for v1, as v2 will not be moving in
        // the opposite direction
        fvd1.setLocation(fvd1.getX() - 2 * dx, fvd1.getY() - 2 * dy);
    } else {
        fvd1.setLocation(fvd1.getX() - dx, fvd1.getY() - dy);
    }
    if (v1_locked) {
        // double the offset for v2, as v1 will not be moving in
        // the opposite direction
        fvd2.setLocation(fvd2.getX() + 2 * dx, fvd2.getY() + 2 * dy);
    } else {
        fvd2.setLocation(fvd2.getX() + dx, fvd2.getY() + dy);
    }
}

From source file:pt.lsts.neptus.plugins.sunfish.awareness.SituationAwareness.java

@Override
public void paintInteraction(Graphics2D g, StateRenderer2D source) {
    super.paintInteraction(g, source);
    g.setStroke(new BasicStroke(1f));
    paint(g, source);/*from  ww w .  jav  a2 s  .c o m*/
    AssetPosition pivot = intercepted;
    if (pivot != null) {
        Point2D pt = source.getScreenPosition(pivot.getLoc());
        g.setColor(Color.white);
        g.draw(new Ellipse2D.Double(pt.getX() - 6, pt.getY() - 6, 12, 12));
        if (assetProperties.containsKey(pivot.getAssetName()))
            pivot.putExtra("Description", assetProperties.get(pivot.getAssetName()).description);
        if (assetProperties.containsKey(pivot.getAssetName()))
            pivot.putExtra("Friendly name", assetProperties.get(pivot.getAssetName()).friendly);

        lbl.setOpaque(true);
        lbl.setBackground(new Color(255, 255, 255, 128));
        lbl.setText(pivot.getHtml());
        Dimension d = lbl.getPreferredSize();
        lbl.setSize(d);
        Graphics copy = g.create();
        copy.translate(10, 10);
        lbl.paint(copy);
    }

    for (AssetTrack t : assets.values()) {
        AssetPosition prev = t.getLatest();
        AssetPosition pred = t.getPrediction();

        if (prev != null && pred != null) {
            if (prev.getTimestamp() < oldestTimestampSelection
                    || prev.getTimestamp() > newestTimestampSelection)
                continue;
            g.setColor(new Color(t.getColor().getRed(), t.getColor().getGreen(), t.getColor().getBlue(), 128));
            Point2D pt1 = source.getScreenPosition(prev.getLoc());
            Point2D pt2 = source.getScreenPosition(pred.getLoc());
            if (pt1.distance(pt2) < 1000)
                g.draw(new Line2D.Double(pt1, pt2));
        }
    }
}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.reporting.AnnotationReportCanvas.java

protected void computeConnections() {

    connections = new HashMap<AnnotationObject, Polygon>();
    connections_cp = new HashMap<AnnotationObject, Point2D>();

    for (AnnotationObject a : theDocument.getAnnotations()) {
        Rectangle rect = rectangles_complete.get(a);
        Point2D cp = dataToScreenCoords(theDocument.getControlPoint(a));
        Point2D peak = dataToScreenCoords(a.getPeakPoint());

        // select anchor
        Point2D anchor = computeAnchor(rect, cp, peak);
        boolean add_cp = (peak.getY() > bottom(rect));

        if (anchor.distance(peak) > 10) {
            // create shape
            Polygon connection = new Polygon();
            connection.addPoint((int) anchor.getX(), (int) anchor.getY());
            if (add_cp)
                connection.addPoint((int) cp.getX(), (int) cp.getY());
            connection.addPoint((int) peak.getX(), (int) peak.getY());
            if (add_cp)
                connection.addPoint((int) cp.getX(), (int) cp.getY());

            // save
            connections.put(a, connection);
            if (add_cp)
                connections_cp.put(a, cp);
        }//from w w w  .  ja v a  2s  . c om
    }
}

From source file:pt.lsts.neptus.plugins.sunfish.awareness.SituationAwareness.java

@Override
public void paint(Graphics2D g, StateRenderer2D renderer) {
    double radius = isActive() ? 6 : 2.5;
    for (AssetTrack track : assets.values()) {
        List<AssetPosition> positions = track.getTrack();
        Point2D lastLoc = null;
        long lastAge = 0;
        for (AssetPosition p : positions) {
            if (hiddenPosTypes.contains(p.getType()))
                continue;

            if (p.getTimestamp() < oldestTimestampSelection || p.getTimestamp() > newestTimestampSelection)
                continue;

            //if (p.getAge() >= maxAge * 3600 * 1000)
            //    continue;
            Point2D pt = renderer.getScreenPosition(p.getLoc());
            if (assetProperties.containsKey(track.getAssetName()))
                g.setColor(assetProperties.get(track.getAssetName()).color);
            else/*from  w w  w  .  j  a  va2 s  .  c  om*/
                g.setColor(track.getColor());
            if (lastLoc != null && lastLoc.distance(pt) < 20000) {
                g.draw(new Line2D.Double(lastLoc, pt));
            }
            g.fill(new Ellipse2D.Double(pt.getX() - radius, pt.getY() - radius, radius * 2, radius * 2));
            lastLoc = pt;
            lastAge = p.getAge();
        }
        g.setStroke(new BasicStroke(2.0f));
        if (lastLoc != null) {
            Color c = cmap2.getColor(1 - (lastAge / (7200000.0)));
            g.setColor(c);
            g.setStroke(new BasicStroke(2.0f));
            g.draw(new Ellipse2D.Double(lastLoc.getX() - radius - 1.5, lastLoc.getY() - radius - 1.5,
                    radius * 2 + 3, radius * 2 + 3));
        }
    }

    if (paintLabels)
        paintLabels(g, renderer);

    if (paintIcons)
        paintIcons(g, renderer);
}