line poly intersect should gove closest point if intersection hits two edges

This commit is contained in:
Fraser Graham
2013-11-25 21:13:22 -08:00
parent 6170691eb1
commit 0559630cc6
3 changed files with 37 additions and 4 deletions
+19 -4
View File
@@ -1,5 +1,9 @@
package govector
import (
"math"
)
func Intersection(p1, p2 Point2d, v1, v2 Vector2d) (bool, Point2d) {
t := p2.Sub(p1).Cross(v2) / v1.Cross(v2)
s := p2.Sub(p1).Cross(v1) / v1.Cross(v2)
@@ -63,19 +67,30 @@ func ProjectPolygon(axis Vector2d, p Polygon2d) (min float32, max float32) {
func PolygonIntersection(r Polygon2d, p Point2d, v Vector2d) (bool, Point2d) {
// TODO - need to test point in polygon for start and end to detect wholly
// inside situations
intersection := false
i_point := Point2d{}
distance_sq := float32(math.MaxFloat32)
for point, _ := range r.Points {
if point == len(r.Points) {
break
}
edgePt, edgeVec := r.Edge(point)
intersection, pt := Intersection(p, edgePt, v, edgeVec)
if intersection {
return intersection, pt
i, pt := Intersection(p, edgePt, v, edgeVec)
if i {
intersection = true
d_sq := pt.Sub(p).MagSquared()
if d_sq < distance_sq {
i_point = pt
distance_sq = d_sq
}
}
}
return false, Point2d{}
return intersection, i_point
}
func PolyPolyIntersection(p1 Polygon2d, v1 Vector2d, p2 Polygon2d, v2 Vector2d) bool {