fix unit tests

This commit is contained in:
Fraser Graham 2013-10-19 16:15:27 -07:00
parent 1f22a8b12f
commit 84c959f0df
2 changed files with 30 additions and 21 deletions

View File

@ -101,7 +101,7 @@ func TestDot(t *testing.T) {
d = v1.Dot(v2) d = v1.Dot(v2)
fmt.Printf("%v\n", d) fmt.Printf("%v\n", d)
if math.Abs(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")
} }
} }
@ -139,7 +139,7 @@ func TestAngle(t *testing.T) {
a := Angle(v2, v1) a := Angle(v2, v1)
fmt.Printf("%v %v %f\n", v1, v2, a*Rad2deg) fmt.Printf("%v %v %f\n", v1, v2, a*Rad2deg)
if math.Abs(a)-math.Pi/2 > Epsilon { if math.Abs(float64(a))-math.Pi/2 > Epsilon {
t.Errorf("Angle Error") t.Errorf("Angle Error")
} }
@ -150,7 +150,7 @@ func TestAngle(t *testing.T) {
fmt.Printf("%v %v %f\n", v1, v2, a*Rad2deg) fmt.Printf("%v %v %f\n", v1, v2, a*Rad2deg)
if math.Abs(a)-math.Pi/4 > Epsilon { if math.Abs(float64(a))-math.Pi/4 > Epsilon {
t.Errorf("Angle Error") t.Errorf("Angle Error")
} }

View File

@ -10,13 +10,13 @@ type Rect2d struct {
} }
type Vector2d struct { type Vector2d struct {
X float64 `json:"x"` X float32 `json:"x"`
Y float64 `json:"y"` Y float32 `json:"y"`
} }
type Point2d struct { type Point2d struct {
X float64 `json:"x"` X float32 `json:"x"`
Y float64 `json:"y"` Y float32 `json:"y"`
} }
const Epsilon = 1e-7 const Epsilon = 1e-7
@ -31,30 +31,30 @@ func (p Point2d) Add(v Vector2d) Point2d {
return Point2d{p.X + v.X, p.Y + v.Y} return Point2d{p.X + v.X, p.Y + v.Y}
} }
func (v Vector2d) Mag() float64 { func (v Vector2d) Mag() float32 {
return math.Abs(math.Sqrt(v.X*v.X + v.Y*v.Y)) return float32(math.Abs(math.Sqrt(float64(v.X*v.X) + float64(v.Y*v.Y))))
} }
func (v Vector2d) PopPop() float64 { func (v Vector2d) PopPop() float32 {
return v.Mag() return v.Mag()
} }
func (v Vector2d) Scale(s float64) Vector2d { func (v Vector2d) Scale(s float32) Vector2d {
return Vector2d{v.X * s, v.Y * s} return Vector2d{v.X * s, v.Y * s}
} }
func (v1 Vector2d) Cross(v2 Vector2d) float64 { func (v1 Vector2d) Cross(v2 Vector2d) float32 {
return v1.X*v2.Y - v1.Y*v2.X return v1.X*v2.Y - v1.Y*v2.X
} }
func (v1 Vector2d) Dot(v2 Vector2d) float64 { func (v1 Vector2d) Dot(v2 Vector2d) float32 {
return (v1.X * v2.X) + (v1.Y * v2.Y) return (v1.X * v2.X) + (v1.Y * v2.Y)
} }
func (v1 Vector2d) Rotate(a float64) Vector2d { func (v1 Vector2d) Rotate(a float32) Vector2d {
x := v1.X*math.Cos(a) - v1.Y*math.Sin(a) x := float64(v1.X)*math.Cos(float64(a)) - float64(v1.Y)*math.Sin(float64(a))
y := v1.X*math.Sin(a) + v1.Y*math.Cos(a) y := float64(v1.X)*math.Sin(float64(a)) + float64(v1.Y)*math.Cos(float64(a))
return Vector2d{x, y} return Vector2d{float32(x), float32(y)}
} }
func Intersection(p1, p2 Point2d, v1, v2 Vector2d) (bool, Point2d) { func Intersection(p1, p2 Point2d, v1, v2 Vector2d) (bool, Point2d) {
@ -110,17 +110,26 @@ func (v Vector2d) Normalize() Vector2d {
return Vector2d{v.X / m, v.Y / m} return Vector2d{v.X / m, v.Y / m}
} }
func Distance(p1, p2 Point2d) float64 { func Distance(p1, p2 Point2d) float32 {
return p1.Sub(p2).Mag() return p1.Sub(p2).Mag()
} }
func Angle(v1, v2 Vector2d) float64 { 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(x) angle := math.Acos(float64(x))
if math.IsNaN(angle) {
if math.Abs(float64(v1.X-v2.X)) > Epsilon {
return 180.0
} else {
return 0
}
}
// Determine the sign to see what direction // Determine the sign to see what direction
// the angle should go in // the angle should go in
if v1.Y*v2.X > v1.X*v2.Y { if v1.Y*v2.X > v1.X*v2.Y {
angle = angle * -1.0 angle = angle * -1.0
} }
return angle return float32(angle)
} }