added tests to some test cases

This commit is contained in:
Stephen McQuay 2014-01-07 17:39:19 -08:00
parent 7c28ca3ca9
commit d8e3dd04a4
2 changed files with 28 additions and 32 deletions

View File

@ -106,6 +106,7 @@ func IntervalDistance(p1min, p1max, p2min, p2max float32) float32 {
} }
} }
// returns intersect, will intersect, translation.Scale(minimum_interval_distance)
func PolyPolyIntersection(p1 Polygon2d, v1 Vector2d, p2 Polygon2d) (bool, bool, Vector2d) { func PolyPolyIntersection(p1 Polygon2d, v1 Vector2d, p2 Polygon2d) (bool, bool, Vector2d) {
// p1 is moving v1, p2 is stationary // p1 is moving v1, p2 is stationary
intersect := true intersect := true

View File

@ -1,7 +1,6 @@
package govector package govector
import ( import (
"fmt"
"math" "math"
"testing" "testing"
) )
@ -33,8 +32,6 @@ func TestScale(t *testing.T) {
m2 := v.Scale(2) m2 := v.Scale(2)
fmt.Printf("%v\n", m2)
if m2.Mag() != 10 { if m2.Mag() != 10 {
t.Errorf("Mag Error") t.Errorf("Mag Error")
} }
@ -45,8 +42,6 @@ func TestAdd(t *testing.T) {
v := Vector2d{3, 4} v := Vector2d{3, 4}
m := p.Add(v) m := p.Add(v)
fmt.Printf("%v\n", m)
if m.X != 13 && m.Y != 14 { if m.X != 13 && m.Y != 14 {
t.Errorf("Add Error") t.Errorf("Add Error")
} }
@ -99,7 +94,6 @@ func TestDot(t *testing.T) {
v1 = v1.Normalize() v1 = v1.Normalize()
v2 = v2.Normalize() v2 = v2.Normalize()
d = v1.Dot(v2) d = v1.Dot(v2)
fmt.Printf("%v\n", d)
if math.Abs(float64(d)-math.Acos(math.Pi/4)) < 0.0001 { if math.Abs(float64(d)-math.Acos(math.Pi/4)) < 0.0001 {
t.Errorf("Dot Error") t.Errorf("Dot Error")
@ -113,8 +107,7 @@ func TestIntersection(t *testing.T) {
v1 := Vector2d{10, 0} v1 := Vector2d{10, 0}
v2 := Vector2d{0, 10} v2 := Vector2d{0, 10}
i, p := Intersection(p1, p2, v1, v2) i, _ := Intersection(p1, p2, v1, v2)
fmt.Printf("%v %v\n", i, p)
if !i { if !i {
t.Errorf("Intersection Error") t.Errorf("Intersection Error")
} }
@ -125,8 +118,7 @@ func TestIntersection(t *testing.T) {
v1 = Vector2d{10, 0} v1 = Vector2d{10, 0}
v2 = Vector2d{0, 10} v2 = Vector2d{0, 10}
i, p = Intersection(p1, p2, v1, v2) i, _ = Intersection(p1, p2, v1, v2)
fmt.Printf("%v %v\n", i, p)
if i { if i {
t.Errorf("Intersection Error") t.Errorf("Intersection Error")
} }
@ -138,7 +130,6 @@ func TestAngle(t *testing.T) {
a := Angle(v2, v1) a := Angle(v2, v1)
fmt.Printf("%v %v %f\n", v1, v2, a*Rad2deg)
if math.Abs(float64(a))-math.Pi/2 > Epsilon { if math.Abs(float64(a))-math.Pi/2 > Epsilon {
t.Errorf("Angle Error") t.Errorf("Angle Error")
} }
@ -148,8 +139,6 @@ func TestAngle(t *testing.T) {
a = Angle(v1, v2) a = Angle(v1, v2)
fmt.Printf("%v %v %f\n", v1, v2, a*Rad2deg)
if math.Abs(float64(a))-math.Pi/4 > Epsilon { if math.Abs(float64(a))-math.Pi/4 > Epsilon {
t.Errorf("Angle Error") t.Errorf("Angle Error")
} }
@ -160,7 +149,6 @@ func TestRotate(t *testing.T) {
v1 := Vector2d{10, 0} v1 := Vector2d{10, 0}
v2 := v1.Rotate(math.Pi / 2) v2 := v1.Rotate(math.Pi / 2)
fmt.Printf("%v %v\n", v1, v2)
if v2.X > Epsilon || v2.Y-10 > Epsilon { if v2.X > Epsilon || v2.Y-10 > Epsilon {
t.Errorf("Rotate Error") t.Errorf("Rotate Error")
} }
@ -168,7 +156,6 @@ func TestRotate(t *testing.T) {
v1 = Vector2d{1, 0} v1 = Vector2d{1, 0}
v2 = v1.Rotate(-30 * Deg2rad) v2 = v1.Rotate(-30 * Deg2rad)
fmt.Printf("%v %v\n", v1, v2)
if v2.X-0.866025403784438 > Epsilon || v2.Y+0.5 > Epsilon { if v2.X-0.866025403784438 > Epsilon || v2.Y+0.5 > Epsilon {
t.Errorf("Rotate Error") t.Errorf("Rotate Error")
} }
@ -187,7 +174,6 @@ func TestPtInRect(t *testing.T) {
B: pB, B: pB,
} }
fmt.Printf("%v %v\n", p1, r)
out := PointInRect(p1, r) out := PointInRect(p1, r)
if out != false { if out != false {
t.Errorf("PointInRect Error") t.Errorf("PointInRect Error")
@ -217,18 +203,14 @@ func TestRectIntersect(t *testing.T) {
B: pB, B: pB,
} }
fmt.Printf("%v %v\n", p1, r)
coll, inside, pos := RectIntersection(r, p1, v1) coll, inside, pos := RectIntersection(r, p1, v1)
if coll != true { if coll != true {
fmt.Printf("Coll Err %v %v %v\n", inside, coll, pos)
t.Errorf("RectIntersection Error") t.Errorf("RectIntersection Error")
} }
if inside != false { if inside != false {
fmt.Printf("Inside Err %v %v %v\n", inside, coll, pos)
t.Errorf("RectIntersection Error") t.Errorf("RectIntersection Error")
} }
if pos.X != 10 && pos.Y != 5 { if pos.X != 10 && pos.Y != 5 {
fmt.Printf("Pos Err %v %v %v\n", inside, coll, pos)
t.Errorf("RectIntersection Error") t.Errorf("RectIntersection Error")
} }
} }
@ -267,9 +249,8 @@ func TestPolyIntersect(t *testing.T) {
p1.Points = append(p1.Points, Vector2d{5, 5}) p1.Points = append(p1.Points, Vector2d{5, 5})
p1.Points = append(p1.Points, Vector2d{5, 3}) p1.Points = append(p1.Points, Vector2d{5, 3})
i, p := PolygonIntersection(p1, Point2d{0, 0}, Vector2d{10, 10}) i, _ := PolygonIntersection(p1, Point2d{0, 0}, Vector2d{10, 10})
if i { if i {
fmt.Printf("%v", p)
t.Errorf("PolyIntersect Error") t.Errorf("PolyIntersect Error")
} }
} }
@ -280,9 +261,8 @@ func TestPolyIntersectFail(t *testing.T) {
p1.Points = append(p1.Points, Vector2d{5, 5}) p1.Points = append(p1.Points, Vector2d{5, 5})
p1.Points = append(p1.Points, Vector2d{5, 3}) p1.Points = append(p1.Points, Vector2d{5, 3})
i, p := PolygonIntersection(p1, Point2d{0, 0}, Vector2d{-10, -10}) i, _ := PolygonIntersection(p1, Point2d{0, 0}, Vector2d{-10, -10})
if i { if i {
fmt.Printf("%v", p)
t.Errorf("PolyIntersect Error") t.Errorf("PolyIntersect Error")
} }
} }
@ -296,7 +276,6 @@ func TestPolyIntersectTwoHits(t *testing.T) {
i, p := PolygonIntersection(p1, Point2d{-1, 5}, Vector2d{20, 0}) i, p := PolygonIntersection(p1, Point2d{-1, 5}, Vector2d{20, 0})
if !i || (p.X != 0 && p.Y != 5) { if !i || (p.X != 0 && p.Y != 5) {
fmt.Printf("%v", p)
t.Errorf("PolyIntersect Error") t.Errorf("PolyIntersect Error")
} }
} }
@ -315,8 +294,10 @@ func TestPolyPolyIntersect(t *testing.T) {
p2.Points = append(p2.Points, Vector2d{10, 10}) p2.Points = append(p2.Points, Vector2d{10, 10})
p2.Points = append(p2.Points, Vector2d{10, 0}) p2.Points = append(p2.Points, Vector2d{10, 0})
i, m, p := PolyPolyIntersection(p1, Vector2d{0, 0}, p2) i, m, _ := PolyPolyIntersection(p1, Vector2d{0, 0}, p2)
fmt.Printf("%v %v %v\n", i, m, p) if !i || !m {
t.Errorf("should not have intersected")
}
} }
func TestPolyPolyIntersectMoving(t *testing.T) { func TestPolyPolyIntersectMoving(t *testing.T) {
@ -333,8 +314,10 @@ func TestPolyPolyIntersectMoving(t *testing.T) {
p2.Points = append(p2.Points, Vector2d{10, 10}) p2.Points = append(p2.Points, Vector2d{10, 10})
p2.Points = append(p2.Points, Vector2d{10, 0}) p2.Points = append(p2.Points, Vector2d{10, 0})
i, m, p := PolyPolyIntersection(p1, Vector2d{10, 10}, p2) i, m, _ := PolyPolyIntersection(p1, Vector2d{10, 10}, p2)
fmt.Printf("%v %v %v\n", i, m, p) if i || !m {
t.Errorf("should not have intersected")
}
} }
func TestPolyPolyIntersectFail(t *testing.T) { func TestPolyPolyIntersectFail(t *testing.T) {
@ -351,11 +334,23 @@ func TestPolyPolyIntersectFail(t *testing.T) {
p2.Points = append(p2.Points, Vector2d{10, 10}) p2.Points = append(p2.Points, Vector2d{10, 10})
p2.Points = append(p2.Points, Vector2d{10, 0}) p2.Points = append(p2.Points, Vector2d{10, 0})
i, m, p := PolyPolyIntersection(p1, Vector2d{0, 0}, p2) i, m, _ := PolyPolyIntersection(p1, Vector2d{0, 0}, p2)
fmt.Printf("%v %v %v\n", i, m, p) if i || m {
t.Errorf("should not have intersected")
}
} }
func TestOrientedSquare(t *testing.T) { func TestOrientedSquare(t *testing.T) {
p := OrientedSquare(Point2d{10, 10}, Vector2d{0.5, 0.5}, 5) p := OrientedSquare(Point2d{10, 10}, Vector2d{0.5, 0.5}, 5)
fmt.Printf("Square: %v\n", p) expected := []Vector2d{
{0, 7.071068},
{-7.071068, 0},
{0, -7.071068},
{7.071068, 0},
}
for i, v := range expected {
if p.Points[i] != v {
t.Errorf("unexpected points: %+v, expected: %+v", p.Points, expected)
}
}
} }