All of vectorpoint coverage

This commit is contained in:
Stephen McQuay 2014-01-07 17:21:04 -08:00
parent d8e3dd04a4
commit 30aee4f09f
2 changed files with 101 additions and 15 deletions

View File

@ -125,24 +125,47 @@ func TestIntersection(t *testing.T) {
} }
func TestAngle(t *testing.T) { func TestAngle(t *testing.T) {
v1 := Vector2d{10, 0} cases := []struct {
v2 := Vector2d{0, 10} v1 Vector2d
v2 Vector2d
a := Angle(v2, v1) expected float32
}{
if math.Abs(float64(a))-math.Pi/2 > Epsilon { {
t.Errorf("Angle Error") v1: Vector2d{10, 0},
v2: Vector2d{0, 10},
expected: -math.Pi / 2,
},
{
v1: Vector2d{5, 0},
v2: Vector2d{10, 10},
expected: -math.Pi / 4,
},
{
v1: Vector2d{0, 10},
v2: Vector2d{0, -10},
expected: math.Pi,
},
// nan angles:
{
v1: Vector2d{0, 0},
v2: Vector2d{0, 0},
expected: 0,
},
// nan angles
{
v1: Vector2d{0.0001, 0},
v2: Vector2d{0, 0},
expected: math.Pi * Rad2deg,
},
} }
var a float32
for _, c := range cases {
a = Angle(c.v2, c.v1)
v1 = Vector2d{5, 0} if math.Abs(float64(a-c.expected)) > Epsilon {
v2 = Vector2d{10, 10} t.Errorf("Angle Error; expected: %f, actual: %f", c.expected, a)
}
a = Angle(v1, v2)
if math.Abs(float64(a))-math.Pi/4 > Epsilon {
t.Errorf("Angle Error")
} }
} }
func TestRotate(t *testing.T) { func TestRotate(t *testing.T) {
@ -354,3 +377,65 @@ func TestOrientedSquare(t *testing.T) {
} }
} }
} }
func TestPopPop(t *testing.T) {
v1 := Vector2d{10, 10}
if v1.PopPop() != float32(math.Sqrt(10*10+10*10)) {
t.Errorf("Incorrect Magnitude!")
}
}
func TestAASquareAtPoint(t *testing.T) {
p1 := Point2d{10, 10}
expected := AABB2d{
A: Point2d{5, 5},
B: Point2d{15, 15},
}
aabb := AASquareAtPoint(p1, 10)
if aabb != expected {
t.Errorf("incorrect AABB2d; expected: %+v, calculated: %+v ", expected, aabb)
}
}
func TestDistance(t *testing.T) {
if Distance(Point2d{0, 0}, Point2d{0, 0}) != 0.0 {
t.Error("incorectly calculating distance between two points at origin")
}
if Distance(Point2d{0, 0}, Point2d{10, 0}) != 10.0 {
t.Error("incorectly calculating distance between points on y axis")
}
if Distance(Point2d{0, 0}, Point2d{3, 4}) != 5.0 {
t.Error("incorectly calculating distance for pythagorean triple")
}
}
func TestAABBToRect(t *testing.T) {
r := AABB2d{
A: Point2d{5, 5},
B: Point2d{15, 15},
}
p := r.ToPolygon()
expected := Polygon2d{
Points: []Vector2d{
Vector2d{X: 0, Y: 0},
Vector2d{X: 0, Y: 10},
Vector2d{X: 10, Y: 10},
Vector2d{X: 10, Y: 0},
},
Origin: Point2d{X: 5, Y: 5},
}
if len(p.Points) != 4 {
t.Error("incorrect number of points")
}
// alright, this just feels cumbersome ... unfortunate that structs with
// slices can't be compared this way ...
if p.Origin != expected.Origin {
t.Error("just wrong")
}
for i, pp := range expected.Points {
if p.Points[i] != pp {
t.Error("just wrong")
}
}
}

View File

@ -140,6 +140,7 @@ func Distance(p1, p2 Point2d) float32 {
return p1.Sub(p2).Mag() return p1.Sub(p2).Mag()
} }
// returns radians
func Angle(v1, v2 Vector2d) float32 { func Angle(v1, v2 Vector2d) float32 {
x := (v1.Dot(v2) / (v1.Mag() * v2.Mag())) x := (v1.Dot(v2) / (v1.Mag() * v2.Mag()))
angle := math.Acos(float64(x)) angle := math.Acos(float64(x))