added tests to some test cases
This commit is contained in:
parent
7c28ca3ca9
commit
d8e3dd04a4
@ -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) {
|
||||
// p1 is moving v1, p2 is stationary
|
||||
intersect := true
|
||||
|
@ -1,7 +1,6 @@
|
||||
package govector
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
@ -33,8 +32,6 @@ func TestScale(t *testing.T) {
|
||||
|
||||
m2 := v.Scale(2)
|
||||
|
||||
fmt.Printf("%v\n", m2)
|
||||
|
||||
if m2.Mag() != 10 {
|
||||
t.Errorf("Mag Error")
|
||||
}
|
||||
@ -45,8 +42,6 @@ func TestAdd(t *testing.T) {
|
||||
v := Vector2d{3, 4}
|
||||
m := p.Add(v)
|
||||
|
||||
fmt.Printf("%v\n", m)
|
||||
|
||||
if m.X != 13 && m.Y != 14 {
|
||||
t.Errorf("Add Error")
|
||||
}
|
||||
@ -99,7 +94,6 @@ func TestDot(t *testing.T) {
|
||||
v1 = v1.Normalize()
|
||||
v2 = v2.Normalize()
|
||||
d = v1.Dot(v2)
|
||||
fmt.Printf("%v\n", d)
|
||||
|
||||
if math.Abs(float64(d)-math.Acos(math.Pi/4)) < 0.0001 {
|
||||
t.Errorf("Dot Error")
|
||||
@ -113,8 +107,7 @@ func TestIntersection(t *testing.T) {
|
||||
v1 := Vector2d{10, 0}
|
||||
v2 := Vector2d{0, 10}
|
||||
|
||||
i, p := Intersection(p1, p2, v1, v2)
|
||||
fmt.Printf("%v %v\n", i, p)
|
||||
i, _ := Intersection(p1, p2, v1, v2)
|
||||
if !i {
|
||||
t.Errorf("Intersection Error")
|
||||
}
|
||||
@ -125,8 +118,7 @@ func TestIntersection(t *testing.T) {
|
||||
v1 = Vector2d{10, 0}
|
||||
v2 = Vector2d{0, 10}
|
||||
|
||||
i, p = Intersection(p1, p2, v1, v2)
|
||||
fmt.Printf("%v %v\n", i, p)
|
||||
i, _ = Intersection(p1, p2, v1, v2)
|
||||
if i {
|
||||
t.Errorf("Intersection Error")
|
||||
}
|
||||
@ -138,7 +130,6 @@ func TestAngle(t *testing.T) {
|
||||
|
||||
a := Angle(v2, v1)
|
||||
|
||||
fmt.Printf("%v %v %f\n", v1, v2, a*Rad2deg)
|
||||
if math.Abs(float64(a))-math.Pi/2 > Epsilon {
|
||||
t.Errorf("Angle Error")
|
||||
}
|
||||
@ -148,8 +139,6 @@ func TestAngle(t *testing.T) {
|
||||
|
||||
a = Angle(v1, v2)
|
||||
|
||||
fmt.Printf("%v %v %f\n", v1, v2, a*Rad2deg)
|
||||
|
||||
if math.Abs(float64(a))-math.Pi/4 > Epsilon {
|
||||
t.Errorf("Angle Error")
|
||||
}
|
||||
@ -160,7 +149,6 @@ func TestRotate(t *testing.T) {
|
||||
v1 := Vector2d{10, 0}
|
||||
v2 := v1.Rotate(math.Pi / 2)
|
||||
|
||||
fmt.Printf("%v %v\n", v1, v2)
|
||||
if v2.X > Epsilon || v2.Y-10 > Epsilon {
|
||||
t.Errorf("Rotate Error")
|
||||
}
|
||||
@ -168,7 +156,6 @@ func TestRotate(t *testing.T) {
|
||||
v1 = Vector2d{1, 0}
|
||||
v2 = v1.Rotate(-30 * Deg2rad)
|
||||
|
||||
fmt.Printf("%v %v\n", v1, v2)
|
||||
if v2.X-0.866025403784438 > Epsilon || v2.Y+0.5 > Epsilon {
|
||||
t.Errorf("Rotate Error")
|
||||
}
|
||||
@ -187,7 +174,6 @@ func TestPtInRect(t *testing.T) {
|
||||
B: pB,
|
||||
}
|
||||
|
||||
fmt.Printf("%v %v\n", p1, r)
|
||||
out := PointInRect(p1, r)
|
||||
if out != false {
|
||||
t.Errorf("PointInRect Error")
|
||||
@ -217,18 +203,14 @@ func TestRectIntersect(t *testing.T) {
|
||||
B: pB,
|
||||
}
|
||||
|
||||
fmt.Printf("%v %v\n", p1, r)
|
||||
coll, inside, pos := RectIntersection(r, p1, v1)
|
||||
if coll != true {
|
||||
fmt.Printf("Coll Err %v %v %v\n", inside, coll, pos)
|
||||
t.Errorf("RectIntersection Error")
|
||||
}
|
||||
if inside != false {
|
||||
fmt.Printf("Inside Err %v %v %v\n", inside, coll, pos)
|
||||
t.Errorf("RectIntersection Error")
|
||||
}
|
||||
if pos.X != 10 && pos.Y != 5 {
|
||||
fmt.Printf("Pos Err %v %v %v\n", inside, coll, pos)
|
||||
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, 3})
|
||||
|
||||
i, p := PolygonIntersection(p1, Point2d{0, 0}, Vector2d{10, 10})
|
||||
i, _ := PolygonIntersection(p1, Point2d{0, 0}, Vector2d{10, 10})
|
||||
if i {
|
||||
fmt.Printf("%v", p)
|
||||
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, 3})
|
||||
|
||||
i, p := PolygonIntersection(p1, Point2d{0, 0}, Vector2d{-10, -10})
|
||||
i, _ := PolygonIntersection(p1, Point2d{0, 0}, Vector2d{-10, -10})
|
||||
if i {
|
||||
fmt.Printf("%v", p)
|
||||
t.Errorf("PolyIntersect Error")
|
||||
}
|
||||
}
|
||||
@ -296,7 +276,6 @@ func TestPolyIntersectTwoHits(t *testing.T) {
|
||||
|
||||
i, p := PolygonIntersection(p1, Point2d{-1, 5}, Vector2d{20, 0})
|
||||
if !i || (p.X != 0 && p.Y != 5) {
|
||||
fmt.Printf("%v", p)
|
||||
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, 0})
|
||||
|
||||
i, m, p := PolyPolyIntersection(p1, Vector2d{0, 0}, p2)
|
||||
fmt.Printf("%v %v %v\n", i, m, p)
|
||||
i, m, _ := PolyPolyIntersection(p1, Vector2d{0, 0}, p2)
|
||||
if !i || !m {
|
||||
t.Errorf("should not have intersected")
|
||||
}
|
||||
}
|
||||
|
||||
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, 0})
|
||||
|
||||
i, m, p := PolyPolyIntersection(p1, Vector2d{10, 10}, p2)
|
||||
fmt.Printf("%v %v %v\n", i, m, p)
|
||||
i, m, _ := PolyPolyIntersection(p1, Vector2d{10, 10}, p2)
|
||||
if i || !m {
|
||||
t.Errorf("should not have intersected")
|
||||
}
|
||||
}
|
||||
|
||||
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, 0})
|
||||
|
||||
i, m, p := PolyPolyIntersection(p1, Vector2d{0, 0}, p2)
|
||||
fmt.Printf("%v %v %v\n", i, m, p)
|
||||
i, m, _ := PolyPolyIntersection(p1, Vector2d{0, 0}, p2)
|
||||
if i || m {
|
||||
t.Errorf("should not have intersected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrientedSquare(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user