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