Browse Source

float32 -> float64

Stephen McQuay 5 years ago
parent
commit
461ee96e83
4 changed files with 35 additions and 34 deletions
  1. 5
    5
      collision.go
  2. 19
    19
      geometry.go
  3. 3
    3
      interp.go
  4. 8
    7
      vector_test.go

+ 5
- 5
collision.go View File

@@ -51,7 +51,7 @@ func RectIntersection(r AABB2d, p Point2d, v Vector2d) (bool, bool, *Point2d) {
51 51
 	}
52 52
 }
53 53
 
54
-func ProjectPolygon(axis Vector2d, p Polygon2d) (min float32, max float32) {
54
+func ProjectPolygon(axis Vector2d, p Polygon2d) (min float64, max float64) {
55 55
 	min = axis.Dot(p.Origin.Add(p.Points[0]).ToVector())
56 56
 	max = min
57 57
 	for _, point := range p.Points {
@@ -72,7 +72,7 @@ func PolygonIntersection(r Polygon2d, p Point2d, v Vector2d) (bool, *Point2d) {
72 72
 	// inside situations
73 73
 	intersection := false
74 74
 	var iPoint *Point2d
75
-	distance_sq := float32(math.MaxFloat32)
75
+	distance_sq := math.MaxFloat64
76 76
 
77 77
 	for point, _ := range r.Points {
78 78
 		edgePt, edgeVec := r.Edge(point)
@@ -95,7 +95,7 @@ func PointInPolygon(p Point2d, py Polygon2d) bool {
95 95
 	return false
96 96
 }
97 97
 
98
-func IntervalDistance(p1min, p1max, p2min, p2max float32) float32 {
98
+func IntervalDistance(p1min, p1max, p2min, p2max float64) float64 {
99 99
 	if p1min < p2min {
100 100
 		return p2min - p1max
101 101
 	} else {
@@ -110,7 +110,7 @@ func PolyPolyIntersection(p1 Polygon2d, v1 Vector2d, p2 Polygon2d) (bool, bool,
110 110
 	will_intersect := true
111 111
 	translation := Vector2d{}
112 112
 	edges := []Vector2d{}
113
-	minimum_interval_distance := float32(math.MaxFloat32)
113
+	minimum_interval_distance := math.MaxFloat64
114 114
 
115 115
 	for point, _ := range p1.Points {
116 116
 		_, edgeVec := p1.Edge(point)
@@ -151,7 +151,7 @@ func PolyPolyIntersection(p1 Polygon2d, v1 Vector2d, p2 Polygon2d) (bool, bool,
151 151
 			break
152 152
 		}
153 153
 
154
-		interval_distance := float32(math.Abs(float64(dist)))
154
+		interval_distance := math.Abs(dist)
155 155
 		if interval_distance < minimum_interval_distance {
156 156
 			minimum_interval_distance = interval_distance
157 157
 			translation = axis

+ 19
- 19
geometry.go View File

@@ -31,13 +31,13 @@ func (p Polygon2d) Edge(i int) (Point2d, Vector2d) {
31 31
 }
32 32
 
33 33
 type Vector2d struct {
34
-	X float32 `json:"x"`
35
-	Y float32 `json:"y"`
34
+	X float64 `json:"x"`
35
+	Y float64 `json:"y"`
36 36
 }
37 37
 
38 38
 type Point2d struct {
39
-	X float32 `json:"x"`
40
-	Y float32 `json:"y"`
39
+	X float64 `json:"x"`
40
+	Y float64 `json:"y"`
41 41
 }
42 42
 
43 43
 const Epsilon = 1e-7
@@ -52,27 +52,27 @@ func (p Point2d) Add(v Vector2d) Point2d {
52 52
 	return Point2d{p.X + v.X, p.Y + v.Y}
53 53
 }
54 54
 
55
-func (v Vector2d) Mag() float32 {
56
-	return float32(math.Abs(math.Sqrt(float64(v.X*v.X) + float64(v.Y*v.Y))))
55
+func (v Vector2d) Mag() float64 {
56
+	return math.Abs(math.Sqrt(v.X*v.X + v.Y*v.Y))
57 57
 }
58 58
 
59
-func (v Vector2d) MagSquared() float32 {
60
-	return float32(math.Abs(float64(v.X*v.X) + float64(v.Y*v.Y)))
59
+func (v Vector2d) MagSquared() float64 {
60
+	return math.Abs(v.X*v.X + v.Y*v.Y)
61 61
 }
62 62
 
63
-func (v Vector2d) PopPop() float32 {
63
+func (v Vector2d) PopPop() float64 {
64 64
 	return v.Mag()
65 65
 }
66 66
 
67
-func (v Vector2d) Scale(s float32) Vector2d {
67
+func (v Vector2d) Scale(s float64) Vector2d {
68 68
 	return Vector2d{v.X * s, v.Y * s}
69 69
 }
70 70
 
71
-func (v1 Vector2d) Cross(v2 Vector2d) float32 {
71
+func (v1 Vector2d) Cross(v2 Vector2d) float64 {
72 72
 	return v1.X*v2.Y - v1.Y*v2.X
73 73
 }
74 74
 
75
-func (v1 Vector2d) Dot(v2 Vector2d) float32 {
75
+func (v1 Vector2d) Dot(v2 Vector2d) float64 {
76 76
 	return (v1.X * v2.X) + (v1.Y * v2.Y)
77 77
 }
78 78
 
@@ -84,17 +84,17 @@ func (v Vector2d) ToPoint() Point2d {
84 84
 	return Point2d{v.X, v.Y}
85 85
 }
86 86
 
87
-func (v1 Vector2d) Rotate(a float32) Vector2d {
87
+func (v1 Vector2d) Rotate(a float64) Vector2d {
88 88
 	x := float64(v1.X)*math.Cos(float64(a)) - float64(v1.Y)*math.Sin(float64(a))
89 89
 	y := float64(v1.X)*math.Sin(float64(a)) + float64(v1.Y)*math.Cos(float64(a))
90
-	return Vector2d{float32(x), float32(y)}
90
+	return Vector2d{x, y}
91 91
 }
92 92
 
93 93
 func PointInRect(p Point2d, r AABB2d) bool {
94 94
 	return (p.X > r.A.X) && (p.X < r.B.X) && (p.Y > r.A.Y) && (p.Y < r.B.Y)
95 95
 }
96 96
 
97
-func AASquareAtPoint(p Point2d, edgeLength float32) AABB2d {
97
+func AASquareAtPoint(p Point2d, edgeLength float64) AABB2d {
98 98
 	size := edgeLength / 2.0
99 99
 	return AABB2d{
100 100
 		A: Point2d{X: p.X - size, Y: p.Y - size},
@@ -113,7 +113,7 @@ func (r AABB2d) ToPolygon() Polygon2d {
113 113
 	return p
114 114
 }
115 115
 
116
-func OrientedSquare(center Point2d, heading Vector2d, size float32) Polygon2d {
116
+func OrientedSquare(center Point2d, heading Vector2d, size float64) Polygon2d {
117 117
 	out := Polygon2d{}
118 118
 	out.Origin.X = center.X
119 119
 	out.Origin.Y = center.Y
@@ -136,12 +136,12 @@ func (v Vector2d) Normalize() Vector2d {
136 136
 	return Vector2d{v.X / m, v.Y / m}
137 137
 }
138 138
 
139
-func Distance(p1, p2 Point2d) float32 {
139
+func Distance(p1, p2 Point2d) float64 {
140 140
 	return p1.Sub(p2).Mag()
141 141
 }
142 142
 
143 143
 // returns radians
144
-func Angle(v1, v2 Vector2d) float32 {
144
+func Angle(v1, v2 Vector2d) float64 {
145 145
 	x := (v1.Dot(v2) / (v1.Mag() * v2.Mag()))
146 146
 	angle := math.Acos(float64(x))
147 147
 
@@ -158,5 +158,5 @@ func Angle(v1, v2 Vector2d) float32 {
158 158
 	if v1.Y*v2.X > v1.X*v2.Y {
159 159
 		angle = angle * -1.0
160 160
 	}
161
-	return float32(angle)
161
+	return angle
162 162
 }

+ 3
- 3
interp.go View File

@@ -37,10 +37,10 @@ func Neville(P []Point2d, ts []float64, t float64) (*Point2d, error) {
37 37
 	order := len(P) - 1
38 38
 	for i := 0; i < order; i++ {
39 39
 		for j := 0; j < order-i; j++ {
40
-			var tLeft float32 = float32(ts[i+j+1] - t)
41
-			var tRight float32 = float32(t - ts[j])
40
+			tLeft := ts[i+j+1] - t
41
+			tRight := t - ts[j]
42 42
 			Q[j] = Q[j].ToVector().Scale(tLeft).ToPoint().Add(P[j+1].ToVector().Scale(tRight))
43
-			Q[j] = Q[j].ToVector().Scale(float32(1.0 / (ts[i+j+1] - ts[j]))).ToPoint()
43
+			Q[j] = Q[j].ToVector().Scale(1.0 / (ts[i+j+1] - ts[j])).ToPoint()
44 44
 
45 45
 		}
46 46
 	}

+ 8
- 7
vector_test.go View File

@@ -129,7 +129,7 @@ func TestAngle(t *testing.T) {
129 129
 	cases := []struct {
130 130
 		v1       Vector2d
131 131
 		v2       Vector2d
132
-		expected float32
132
+		expected float64
133 133
 	}{
134 134
 		{
135 135
 			v1:       Vector2d{10, 0},
@@ -159,7 +159,7 @@ func TestAngle(t *testing.T) {
159 159
 			expected: math.Pi * Rad2deg,
160 160
 		},
161 161
 	}
162
-	var a float32
162
+	var a float64
163 163
 	for _, c := range cases {
164 164
 		a = Angle(c.v2, c.v1)
165 165
 
@@ -508,11 +508,12 @@ func TestPolyPolyNoOverlapMoveOnto(t *testing.T) {
508 508
 func TestOrientedSquare(t *testing.T) {
509 509
 	p := OrientedSquare(Point2d{10, 10}, Vector2d{0.5, 0.5}, 5)
510 510
 	expected := []Vector2d{
511
-		{0, 7.071068},
512
-		{-7.071068, 0},
513
-		{0, -7.071068},
514
-		{7.071068, 0},
511
+		{X: 0, Y: 7.071067811865475},
512
+		{X: -7.071067811865475, Y: 0},
513
+		{X: 0, Y: -7.071067811865475},
514
+		{X: 7.071067811865475, Y: 0},
515 515
 	}
516
+
516 517
 	for i, v := range expected {
517 518
 		if p.Points[i] != v {
518 519
 			t.Errorf("unexpected points: %+v, expected: %+v", p.Points, expected)
@@ -522,7 +523,7 @@ func TestOrientedSquare(t *testing.T) {
522 523
 
523 524
 func TestPopPop(t *testing.T) {
524 525
 	v1 := Vector2d{10, 10}
525
-	if v1.PopPop() != float32(math.Sqrt(10*10+10*10)) {
526
+	if v1.PopPop() != math.Sqrt(10*10+10*10) {
526 527
 		t.Errorf("Incorrect Magnitude!")
527 528
 	}
528 529
 }

Loading…
Cancel
Save