bug in java.awt.geom.Line2D.ptLineDistSq() ?

Before I make a bug report on this I thought i’d get the opinions of you ppl. (cos I don’t wanna get laughed at for my appauling knowledge of Maths :D)

The class java.awt.geom.Line2D has a method :-


public static double ptLineDistSq(double X1, double Y1, double X2, double Y2, double PX, double PY)

It calculates the distance (squared) between a given point and the nearest part of a line segment.

If however you pass in a line segment of zero length (X1=X2, Y1=Y2), the method performs a div by zero, which causes NaN to be returned.

I believe this is a bug, as the method documentation makes no exceptions to valid input.
Also IMO the distance from a point to a line segment still has a value even if the line segment has a zero length.

A simple sanity check at the start of the method would fix it,

if(X1==X2 && Y1==Y2) return (X1-PX)(X1-PX)+(Y1-PY)(Y1-PY);

So, whadda you reckon, is it a bug?

It’s definitely a line segment is it? If the two points are used to describe a line, then passing two points zero distance apart wouldn’t be a valid description of a line.

HOWEVER, if it returns NaN but the documentation doesn’t mention it, then yes, I’d say it’s at least a documentation bug, regardless of what caused the problem!

sigh, stupid stupid me.

I meant segDist, but in the test case i made, i put lineDist DOH!

I just checked segDist, and it worked as it should <_<

there u go, I knew i HAD to be wrong ;D

incidentally, passing in 2 identical points to the ptLineDist method does generate a NaN (which isn’t mentioned in the docs) but I guess its a perfectly valid return value for such stupid parameters :smiley:
The alternative whould be an IllegalArgumentException, but that would just be ugly.