Java Code Examples for java.awt.geom.Point2D#distanceSq()

The following examples show how to use java.awt.geom.Point2D#distanceSq() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConnectionWidget.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an index of a control point that is hit by the local location
 * @param localLocation the local location
 * @return the index; -1 if no control point was hit
 */
public final int getControlPointHitAt (Point localLocation) {
    int controlRadius = controlPointShape.getRadius ();
    controlRadius *= controlRadius;

    if (isFirstControlPointHitAt (localLocation))
        return 0;

    if (isLastControlPointHitAt (localLocation))
        return controlPoints.size () - 1;

    for (int i = 0; i < controlPoints.size (); i ++) {
        Point point = controlPoints.get (i);
        if (Point2D.distanceSq (point.x, point.y, localLocation.x, localLocation.y) <= controlRadius)
            return i;
    }

    return -1;
}
 
Example 2
Source File: TouchableTransformerTests.java    From workcraft with MIT License 6 votes vote down vote up
@Test
public void testTranslateHitTest() {
    TouchableTransformer toucher = new TouchableTransformer(
            new Dummy() {
                @Override
                public boolean hitTest(Point2D point) {
                    return point.distanceSq(0, 0) < 1.0;
                }
            }, AffineTransform.getTranslateInstance(10, 1));

    Assertions.assertTrue(toucher.hitTest(new Point2D.Double(10, 1)));
    Assertions.assertTrue(toucher.hitTest(new Point2D.Double(10.9, 1)));
    Assertions.assertTrue(toucher.hitTest(new Point2D.Double(9.1, 1)));
    Assertions.assertFalse(toucher.hitTest(new Point2D.Double(11.1, 1)));
    Assertions.assertFalse(toucher.hitTest(new Point2D.Double(8.9, 1)));
    Assertions.assertTrue(toucher.hitTest(new Point2D.Double(10.6, 1.6)));
    Assertions.assertFalse(toucher.hitTest(new Point2D.Double(10.8, 1.8)));
    Assertions.assertTrue(toucher.hitTest(new Point2D.Double(9.4, 0.4)));
    Assertions.assertFalse(toucher.hitTest(new Point2D.Double(9.2, 0.2)));
}
 
Example 3
Source File: ConnectionWidget.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the local location hits the first control point (also meant to be the source anchor).
 * @param localLocation the local location
 * @return true if it hits the first control point
 */
public final boolean isFirstControlPointHitAt (Point localLocation) {
    int endRadius = endPointShape.getRadius ();
    endRadius *= endRadius;
    Point firstPoint = getFirstControlPoint ();
    if (firstPoint != null)
        if (Point2D.distanceSq (firstPoint.x, firstPoint.y, localLocation.x, localLocation.y) <= endRadius)
            return true;
    return false;
}
 
Example 4
Source File: ConnectionWidget.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the local location hits the last control point (also meant to be the target anchor).
 * @param localLocation the local location
 * @return true if it hits the last control point
 */
public final boolean isLastControlPointHitAt (Point localLocation) {
    int endRadius = endPointShape.getRadius ();
    endRadius *= endRadius;
    Point lastPoint = getLastControlPoint ();
    if (lastPoint != null)
        if (Point2D.distanceSq (lastPoint.x, lastPoint.y, localLocation.x, localLocation.y) <= endRadius)
            return true;
    return false;
}
 
Example 5
Source File: CircuitLayoutCommand.java    From workcraft with MIT License 5 votes vote down vote up
private Point2D getLastCommonPointInRootSpace(VisualConnection c1, VisualConnection c2) {
    if (c1.getFirst() != c2.getFirst()) {
        return null;
    }
    Point2D pos = ((VisualTransformableNode) c1.getFirst()).getRootSpacePosition();
    ConnectionGraphic g1 = c1.getGraphic();
    ConnectionGraphic g2 = c2.getGraphic();
    if ((g1 instanceof Polyline) && (g2 instanceof Polyline)) {
        Polyline poly1 = (Polyline) g1;
        Polyline poly2 = (Polyline) g2;
        int count = Math.min(poly1.getControlPointCount(), poly2.getControlPointCount());
        for (int i = 0; i <= count; i++) {
            Point2D pos1 = (i < poly1.getControlPointCount()) ? poly1.getControlPoint(i).getRootSpacePosition()
                                   : ((VisualTransformableNode) c1.getSecond()).getRootSpacePosition();
            Point2D pos2 = (i < poly2.getControlPointCount()) ? poly2.getControlPoint(i).getRootSpacePosition()
                                   : ((VisualTransformableNode) c2.getSecond()).getRootSpacePosition();
            if (pos1.distance(pos2) < SAME_POINT_DISTANCE_THRESHOLD) {
                pos = pos1;
            } else {
                double gradient = ConnectionHelper.calcGradient(pos, pos1, pos2);
                boolean sameSide = ((pos2.getX() > pos.getX()) == (pos1.getX() > pos.getX()))
                                           && ((pos2.getY() > pos.getY()) == (pos1.getY() > pos.getY()));
                if ((Math.abs(gradient) < SAME_POINT_GRADIENT_THRESHOLD) && sameSide) {
                    pos = (pos.distanceSq(pos1) < pos.distanceSq(pos2)) ? pos1 : pos2;
                }
                break;
            }
        }
    }
    return pos;
}
 
Example 6
Source File: VisualCondition.java    From workcraft with MIT License 4 votes vote down vote up
@Override
public boolean hitTestInLocalSpace(Point2D pointInLocalSpace) {
    return pointInLocalSpace.distanceSq(0, 0) < size * size / 4;
}
 
Example 7
Source File: VisualChannelPlace.java    From workcraft with MIT License 4 votes vote down vote up
@Override
public boolean hitTestInLocalSpace(Point2D pointInLocalSpace) {
    return pointInLocalSpace.distanceSq(0, 0) < size * size / 4;
}
 
Example 8
Source File: VisualState.java    From workcraft with MIT License 4 votes vote down vote up
@Override
public boolean hitTestInLocalSpace(Point2D pointInLocalSpace) {
    double size = VisualCommonSettings.getNodeSize() - VisualCommonSettings.getStrokeWidth();
    return pointInLocalSpace.distanceSq(0, 0) < size * size / 4;
}
 
Example 9
Source File: VisualJoint.java    From workcraft with MIT License 4 votes vote down vote up
@Override
public boolean hitTestInLocalSpace(Point2D pointInLocalSpace) {
    return 4 * pointInLocalSpace.distanceSq(0, 0) < size * size;
}
 
Example 10
Source File: VisualPlace.java    From workcraft with MIT License 4 votes vote down vote up
@Override
public boolean hitTestInLocalSpace(Point2D pointInLocalSpace) {
    double size = VisualCommonSettings.getNodeSize() - VisualCommonSettings.getStrokeWidth();
    return pointInLocalSpace.distanceSq(0, 0) < size * size / 4;
}