converted intersection to return point pointers

This commit is contained in:
sm
2014-01-15 23:11:40 -08:00
parent 282bcb448f
commit 8a5e55e410
2 changed files with 25 additions and 19 deletions
+11 -10
View File
@@ -4,14 +4,15 @@ import (
"math"
)
func Intersection(p1, p2 Point2d, v1, v2 Vector2d) (bool, Point2d) {
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)
if t > 0 && t < 1 && s > 0 && s < 1 {
return true, p1.Add(v1.Scale(t))
intersection := p1.Add(v1.Scale(t))
return true, &intersection
}
return false, Point2d{0, 0}
return false, nil
}
func RectRectIntersection(r1 AABB2d, r2 AABB2d) bool {
@@ -20,14 +21,14 @@ func RectRectIntersection(r1 AABB2d, r2 AABB2d) bool {
}
// returns collision, inside, point of collision
func RectIntersection(r AABB2d, p Point2d, v Vector2d) (bool, bool, Point2d) {
func RectIntersection(r AABB2d, p Point2d, v Vector2d) (bool, bool, *Point2d) {
collision := false
inside := false
start_inside := PointInRect(p, r)
end_inside := PointInRect(p.Add(v), r)
if start_inside && end_inside {
inside = true
return collision, inside, p
return collision, inside, nil
} else {
wall_left, col1 := Intersection(p, r.A, v, Vector2d{X: 0, Y: r.B.Y - r.A.Y})
if wall_left {
@@ -46,7 +47,7 @@ func RectIntersection(r AABB2d, p Point2d, v Vector2d) (bool, bool, Point2d) {
return wall_bottom, inside, col4
}
return false, false, p
return false, false, nil
}
}
@@ -66,11 +67,11 @@ func ProjectPolygon(axis Vector2d, p Polygon2d) (min float32, max float32) {
return min, max
}
func PolygonIntersection(r Polygon2d, p Point2d, v Vector2d) (bool, Point2d) {
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{}
var iPoint *Point2d
distance_sq := float32(math.MaxFloat32)
for point, _ := range r.Points {
@@ -80,13 +81,13 @@ func PolygonIntersection(r Polygon2d, p Point2d, v Vector2d) (bool, Point2d) {
intersection = true
d_sq := pt.Sub(p).MagSquared()
if d_sq < distance_sq {
i_point = pt
iPoint = pt
distance_sq = d_sq
}
}
}
return intersection, i_point
return intersection, iPoint
}
func PointInPolygon(p Point2d, py Polygon2d) bool {