From 461ee96e83391b450fad22af26d153f36d7ef4bc Mon Sep 17 00:00:00 2001 From: stephen mcquay Date: Sat, 26 Apr 2014 14:40:18 -0700 Subject: [PATCH] float32 -> float64 --- collision.go | 10 +++++----- geometry.go | 38 +++++++++++++++++++------------------- interp.go | 6 +++--- vector_test.go | 15 ++++++++------- 4 files changed, 35 insertions(+), 34 deletions(-) diff --git a/collision.go b/collision.go index 0c6c386..379e43e 100644 --- a/collision.go +++ b/collision.go @@ -51,7 +51,7 @@ func RectIntersection(r AABB2d, p Point2d, v Vector2d) (bool, bool, *Point2d) { } } -func ProjectPolygon(axis Vector2d, p Polygon2d) (min float32, max float32) { +func ProjectPolygon(axis Vector2d, p Polygon2d) (min float64, max float64) { min = axis.Dot(p.Origin.Add(p.Points[0]).ToVector()) max = min for _, point := range p.Points { @@ -72,7 +72,7 @@ func PolygonIntersection(r Polygon2d, p Point2d, v Vector2d) (bool, *Point2d) { // inside situations intersection := false var iPoint *Point2d - distance_sq := float32(math.MaxFloat32) + distance_sq := math.MaxFloat64 for point, _ := range r.Points { edgePt, edgeVec := r.Edge(point) @@ -95,7 +95,7 @@ func PointInPolygon(p Point2d, py Polygon2d) bool { return false } -func IntervalDistance(p1min, p1max, p2min, p2max float32) float32 { +func IntervalDistance(p1min, p1max, p2min, p2max float64) float64 { if p1min < p2min { return p2min - p1max } else { @@ -110,7 +110,7 @@ func PolyPolyIntersection(p1 Polygon2d, v1 Vector2d, p2 Polygon2d) (bool, bool, will_intersect := true translation := Vector2d{} edges := []Vector2d{} - minimum_interval_distance := float32(math.MaxFloat32) + minimum_interval_distance := math.MaxFloat64 for point, _ := range p1.Points { _, edgeVec := p1.Edge(point) @@ -151,7 +151,7 @@ func PolyPolyIntersection(p1 Polygon2d, v1 Vector2d, p2 Polygon2d) (bool, bool, break } - interval_distance := float32(math.Abs(float64(dist))) + interval_distance := math.Abs(dist) if interval_distance < minimum_interval_distance { minimum_interval_distance = interval_distance translation = axis diff --git a/geometry.go b/geometry.go index 92ad60d..6ccbea2 100644 --- a/geometry.go +++ b/geometry.go @@ -31,13 +31,13 @@ func (p Polygon2d) Edge(i int) (Point2d, Vector2d) { } type Vector2d struct { - X float32 `json:"x"` - Y float32 `json:"y"` + X float64 `json:"x"` + Y float64 `json:"y"` } type Point2d struct { - X float32 `json:"x"` - Y float32 `json:"y"` + X float64 `json:"x"` + Y float64 `json:"y"` } const Epsilon = 1e-7 @@ -52,27 +52,27 @@ func (p Point2d) Add(v Vector2d) Point2d { return Point2d{p.X + v.X, p.Y + v.Y} } -func (v Vector2d) Mag() float32 { - return float32(math.Abs(math.Sqrt(float64(v.X*v.X) + float64(v.Y*v.Y)))) +func (v Vector2d) Mag() float64 { + return math.Abs(math.Sqrt(v.X*v.X + v.Y*v.Y)) } -func (v Vector2d) MagSquared() float32 { - return float32(math.Abs(float64(v.X*v.X) + float64(v.Y*v.Y))) +func (v Vector2d) MagSquared() float64 { + return math.Abs(v.X*v.X + v.Y*v.Y) } -func (v Vector2d) PopPop() float32 { +func (v Vector2d) PopPop() float64 { return v.Mag() } -func (v Vector2d) Scale(s float32) Vector2d { +func (v Vector2d) Scale(s float64) Vector2d { return Vector2d{v.X * s, v.Y * s} } -func (v1 Vector2d) Cross(v2 Vector2d) float32 { +func (v1 Vector2d) Cross(v2 Vector2d) float64 { return v1.X*v2.Y - v1.Y*v2.X } -func (v1 Vector2d) Dot(v2 Vector2d) float32 { +func (v1 Vector2d) Dot(v2 Vector2d) float64 { return (v1.X * v2.X) + (v1.Y * v2.Y) } @@ -84,17 +84,17 @@ func (v Vector2d) ToPoint() Point2d { return Point2d{v.X, v.Y} } -func (v1 Vector2d) Rotate(a float32) Vector2d { +func (v1 Vector2d) Rotate(a float64) Vector2d { x := float64(v1.X)*math.Cos(float64(a)) - float64(v1.Y)*math.Sin(float64(a)) y := float64(v1.X)*math.Sin(float64(a)) + float64(v1.Y)*math.Cos(float64(a)) - return Vector2d{float32(x), float32(y)} + return Vector2d{x, y} } func PointInRect(p Point2d, r AABB2d) bool { return (p.X > r.A.X) && (p.X < r.B.X) && (p.Y > r.A.Y) && (p.Y < r.B.Y) } -func AASquareAtPoint(p Point2d, edgeLength float32) AABB2d { +func AASquareAtPoint(p Point2d, edgeLength float64) AABB2d { size := edgeLength / 2.0 return AABB2d{ A: Point2d{X: p.X - size, Y: p.Y - size}, @@ -113,7 +113,7 @@ func (r AABB2d) ToPolygon() Polygon2d { return p } -func OrientedSquare(center Point2d, heading Vector2d, size float32) Polygon2d { +func OrientedSquare(center Point2d, heading Vector2d, size float64) Polygon2d { out := Polygon2d{} out.Origin.X = center.X out.Origin.Y = center.Y @@ -136,12 +136,12 @@ func (v Vector2d) Normalize() Vector2d { return Vector2d{v.X / m, v.Y / m} } -func Distance(p1, p2 Point2d) float32 { +func Distance(p1, p2 Point2d) float64 { return p1.Sub(p2).Mag() } // returns radians -func Angle(v1, v2 Vector2d) float32 { +func Angle(v1, v2 Vector2d) float64 { x := (v1.Dot(v2) / (v1.Mag() * v2.Mag())) angle := math.Acos(float64(x)) @@ -158,5 +158,5 @@ func Angle(v1, v2 Vector2d) float32 { if v1.Y*v2.X > v1.X*v2.Y { angle = angle * -1.0 } - return float32(angle) + return angle } diff --git a/interp.go b/interp.go index 46e604a..5db2371 100644 --- a/interp.go +++ b/interp.go @@ -37,10 +37,10 @@ func Neville(P []Point2d, ts []float64, t float64) (*Point2d, error) { order := len(P) - 1 for i := 0; i < order; i++ { for j := 0; j < order-i; j++ { - var tLeft float32 = float32(ts[i+j+1] - t) - var tRight float32 = float32(t - ts[j]) + tLeft := ts[i+j+1] - t + tRight := t - ts[j] Q[j] = Q[j].ToVector().Scale(tLeft).ToPoint().Add(P[j+1].ToVector().Scale(tRight)) - Q[j] = Q[j].ToVector().Scale(float32(1.0 / (ts[i+j+1] - ts[j]))).ToPoint() + Q[j] = Q[j].ToVector().Scale(1.0 / (ts[i+j+1] - ts[j])).ToPoint() } } diff --git a/vector_test.go b/vector_test.go index 3a4ca1a..46ea6a2 100644 --- a/vector_test.go +++ b/vector_test.go @@ -129,7 +129,7 @@ func TestAngle(t *testing.T) { cases := []struct { v1 Vector2d v2 Vector2d - expected float32 + expected float64 }{ { v1: Vector2d{10, 0}, @@ -159,7 +159,7 @@ func TestAngle(t *testing.T) { expected: math.Pi * Rad2deg, }, } - var a float32 + var a float64 for _, c := range cases { a = Angle(c.v2, c.v1) @@ -508,11 +508,12 @@ func TestPolyPolyNoOverlapMoveOnto(t *testing.T) { func TestOrientedSquare(t *testing.T) { p := OrientedSquare(Point2d{10, 10}, Vector2d{0.5, 0.5}, 5) expected := []Vector2d{ - {0, 7.071068}, - {-7.071068, 0}, - {0, -7.071068}, - {7.071068, 0}, + {X: 0, Y: 7.071067811865475}, + {X: -7.071067811865475, Y: 0}, + {X: 0, Y: -7.071067811865475}, + {X: 7.071067811865475, Y: 0}, } + for i, v := range expected { if p.Points[i] != v { t.Errorf("unexpected points: %+v, expected: %+v", p.Points, expected) @@ -522,7 +523,7 @@ func TestOrientedSquare(t *testing.T) { func TestPopPop(t *testing.T) { v1 := Vector2d{10, 10} - if v1.PopPop() != float32(math.Sqrt(10*10+10*10)) { + if v1.PopPop() != math.Sqrt(10*10+10*10) { t.Errorf("Incorrect Magnitude!") } }