-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvec2.go
271 lines (231 loc) · 5.81 KB
/
vec2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package gfx
import (
"fmt"
"image"
"math"
)
// Vec is a 2D vector type with X and Y coordinates.
//
// Create vectors with the V constructor:
//
// u := gfx.V(1, 2)
// v := gfx.V(8, -3)
//
// Use various methods to manipulate them:
//
// w := u.Add(v)
// fmt.Println(w) // gfx.V(9, -1)
// fmt.Println(u.Sub(v)) // gfx.V(-7, 5)
// u = gfx.V(2, 3)
// v = gfx.V(8, 1)
// if u.X < 0 {
// fmt.Println("this won't happen")
// }
// x := u.Unit().Dot(v.Unit())
type Vec struct {
X, Y float64
}
// ZV is a zero vector.
var ZV = Vec{0, 0}
// V returns a new 2D vector with the given coordinates.
func V(x, y float64) Vec {
return Vec{x, y}
}
// IV returns a new 2d vector based on the given int x, y pair.
func IV(x, y int) Vec {
return Vec{
float64(x),
float64(y),
}
}
// PV returns a new 2D vector based on the given image.Point.
func PV(p image.Point) Vec {
return IV(p.X, p.Y)
}
// Unit returns a vector of length 1 facing the given angle.
func Unit(angle float64) Vec {
return Vec{1, 0}.Rotated(angle)
}
// String returns the string representation of the vector u.
//
// u := gfx.V(4.5, -1.3)
// u.String() // returns "gfx.V(4.5, -1.3)"
// fmt.Println(u) // gfx.V(4.5, -1.3)
func (u Vec) String() string {
return fmt.Sprintf("gfx.V(%.8f, %.8f)", u.X, u.Y)
}
// XY returns the components of the vector in two return values.
func (u Vec) XY() (x, y float64) {
return u.X, u.Y
}
// Eq checks the equality of two vectors.
func (u Vec) Eq(v Vec) bool {
return u.X == v.X && u.Y == v.Y
}
// Add returns the sum of vectors u and v.
func (u Vec) Add(v Vec) Vec {
return Vec{
u.X + v.X,
u.Y + v.Y,
}
}
// AddXY returns the sum of x and y added to v.
func (u Vec) AddXY(x, y float64) Vec {
return Vec{
u.X + x,
u.Y + y,
}
}
// Sub returns the difference betweeen vectors u and v.
func (u Vec) Sub(v Vec) Vec {
return Vec{
u.X - v.X,
u.Y - v.Y,
}
}
// To returns the vector from u to v. Equivalent to v.Sub(u).
func (u Vec) To(v Vec) Vec {
return Vec{
v.X - u.X,
v.Y - u.Y,
}
}
// Mod returns the floating-point remainder vector of x and y.
func (u Vec) Mod(v Vec) Vec {
return Vec{
math.Mod(u.X, v.X),
math.Mod(u.Y, v.Y),
}
}
// Scaled returns the vector u multiplied by c.
func (u Vec) Scaled(c float64) Vec {
return Vec{
u.X * c,
u.Y * c,
}
}
// ScaledXY returns the vector u multiplied by the vector v component-wise.
func (u Vec) ScaledXY(v Vec) Vec {
return Vec{
u.X * v.X,
u.Y * v.Y,
}
}
// Len returns the length of the vector u.
func (u Vec) Len() float64 {
return math.Hypot(u.X, u.Y)
}
// Angle returns the angle between the vector u and the x-axis. The result is in range [-Pi, Pi].
func (u Vec) Angle() float64 {
return math.Atan2(u.Y, u.X)
}
// Unit returns a vector of length 1 facing the direction of u (has the same angle).
func (u Vec) Unit() Vec {
if u.X == 0 && u.Y == 0 {
return Vec{1, 0}
}
return u.Scaled(1 / u.Len())
}
// Abs returns the absolute vector of the vector u.
func (u Vec) Abs() Vec {
return Vec{
math.Abs(u.X),
math.Abs(u.Y),
}
}
// Max returns the maximum vector of u and v.
func (u Vec) Max(v Vec) Vec {
return Vec{
math.Max(u.X, v.X),
math.Max(u.Y, v.Y),
}
}
// Min returns the minimum vector of u and v.
func (u Vec) Min(v Vec) Vec {
return Vec{
math.Min(u.X, v.X),
math.Min(u.Y, v.Y),
}
}
// Rotated returns the vector u rotated by the given angle in radians.
func (u Vec) Rotated(angle float64) Vec {
sin, cos := math.Sincos(angle)
return Vec{
u.X*cos - u.Y*sin,
u.X*sin + u.Y*cos,
}
}
// Normal returns a vector normal to u. Equivalent to u.Rotated(math.Pi / 2), but faster.
func (u Vec) Normal() Vec {
return Vec{
-u.Y,
u.X,
}
}
// Dot returns the dot product of vectors u and v.
func (u Vec) Dot(v Vec) float64 {
return u.X*v.X + u.Y*v.Y
}
// Cross return the cross product of vectors u and v.
func (u Vec) Cross(v Vec) float64 {
return u.X*v.Y - v.X*u.Y
}
// Project returns a projection (or component) of vector u in the direction of vector v.
//
// Behaviour is undefined if v is a zero vector.
func (u Vec) Project(v Vec) Vec {
len := u.Dot(v) / v.Len()
return v.Unit().Scaled(len)
}
// Map applies the function f to both x and y components of the vector u and returns the modified
// vector.
//
// u := gfx.V(10.5, -1.5)
// v := u.Map(math.Floor) // v is gfx.V(10, -2), both components of u floored
func (u Vec) Map(f func(float64) float64) Vec {
return Vec{
f(u.X),
f(u.Y),
}
}
// Vec3 converts the vector into a Vec3.
func (u Vec) Vec3(z float64) Vec3 {
return Vec3{u.X, u.Y, z}
}
// Pt returns the image.Point for the vector.
func (u Vec) Pt() image.Point {
return image.Pt(int(u.X), int(u.Y))
}
// R creates a new Rect for the vectors u and v.
//
// Note that the returned rectangle is not automatically normalized.
func (u Vec) R(v Vec) Rect {
return NewRect(u, v)
}
// B creates a new image.Rectangle for the vectors u and v.
func (u Vec) B(v Vec) image.Rectangle {
return u.R(v).Bounds()
}
// Rect constructs a Rect around the vector based on the provided Left, Top, Right, Bottom values.
func (u Vec) Rect(l, t, r, b float64) Rect {
return R(u.X+l, u.Y+t, u.X+r, u.Y+b)
}
// Bounds returns the bounds around the vector based on the provided Left, Top, Right, Bottom values.
func (u Vec) Bounds(l, t, r, b float64) image.Rectangle {
return u.Rect(l, t, r, b).Bounds()
}
// Lerp returns a linear interpolation between vectors u and v.
//
// This function basically returns a point along the line between a and b and t chooses which one.
// If t is 0, then a will be returned, if t is 1, b will be returned. Anything between 0 and 1 will
// return the appropriate point between a and b and so on.
func (u Vec) Lerp(v Vec, t float64) Vec {
return u.Scaled(1 - t).Add(v.Scaled(t))
}
// Centroid returns the centroid O of three vectors.
func Centroid(a, b, c Vec) Vec {
return V(
(a.X+b.X+c.X)/3,
(a.Y+b.Y+c.Y)/3,
)
}