noinia / hgeometry

HGeometry is a library for computing with geometric objects in Haskell. It defines basic geometric types and primitives, and it implements some geometric data structures and algorithms.
121 stars 40 forks source link

sqDistanceToSegArg errors when result should be an open endpoint #170

Closed damon-sava-stanley closed 2 years ago

damon-sava-stanley commented 2 years ago

Howdy. As per the title, there's an issue with LineSegment's sqDistanceToSegArg shown by the following code.

let ls = OpenLineSegment (Point2 0 0 :+ ()) (Point2 1 0 :+ ())
    p = Point2 2 0
in  snd (sqDistanceToSegArg p ls) == Point2 1 0

This should return True as per the documentation

Note that if the segment is open, the closest point returned may be one of the (open) end points, even though technically the end point does not lie on the segment. (The true closest point then lies arbitrarily close to the end point).

Instead it throws Prelude.foldl1: empty list. This is because although the implementation (correctly) adds the endpoints as candidates it, incorrectly, filters them out as not on the list.

noinia commented 2 years ago

Thanks for reporting this! Fixed in the above PR (which also introduces a typeclass for things for which we can compute a distance to)