Incorrect intersectsLine output from Line2D in Java

Why is this code:

Line2D line1 = new Line2D.Double(464.9298111721873, 103.78661133348942, 684.8391765253534, -155.44752172931908);

Line2D line2 = new Line2D.Double(414.16903384086487, 163.62456359144306, 393.52528378472925, 187.95988300984624);

line1.intersectsLine(line2);

return true?

It is clear that the x-coordinates of the lines are different and they do not intersect. I drew them on the turntable and they are separated, but they look as if they are collinear. This is problem? I tried testing on simple collinear lines (e.g. (1, 3, 4, 3), (6, 3, 8, 3)) and it seems to work fine.

+4
source share
1 answer

Java Docs say the class method Line2D:

public boolean intersects(double x, double y, double w, double h)

if the inside intersects the inside of a given rectangular region. To do this, he uses the method , but the calculations to accurately determine this intersection are very expensive. ShapeShape.intersects()

, Shapes true, Shape.

itersectsLine(), intersects() intersectsLine() . linesIntersect() 298

Area , Shape, , . :

boolean intersectionExists(Shape shape1, Shape shape2) {
   Area area1 = new Area(shape1);
   area1.intersect(new Area(shape2));
   return !area1.isEmpty();
}

:

public static void main(String[] args) {
    Line2D line1 = new Line2D.Double(464.9298111721873, 103.78661133348942, 684.8391765253534, -155.44752172931908);
    Line2D line2 = new Line2D.Double(414.16903384086487, 163.62456359144306, 393.52528378472925, 187.95988300984624);
    System.out.println("Lines intersect? " + intersectionExists(line1, line2));
}

:

Lines intersect? false
+2

All Articles