| 1 |
package gfx |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math" |
| 6 |
) |
| 7 |
|
| 8 |
// Vec3 is a 3D vector type with X, Y and Z coordinates. |
| 9 |
// |
| 10 |
// Create vectors with the V3 constructor: |
| 11 |
// |
| 12 |
// u := gfx.V3(1, 2, 3) |
| 13 |
// v := gfx.V3(8, -3, 4) |
| 14 |
type Vec3 struct { |
| 15 |
X, Y, Z float64 |
| 16 |
} |
| 17 |
|
| 18 |
// ZV3 is the zero Vec3 |
| 19 |
var ZV3 = Vec3{0, 0, 0} |
| 20 |
|
| 21 |
// V3 is shorthand for Vec3{X: x, Y: y, Z: z}. |
| 22 |
func V3(x, y, z float64) Vec3 { |
| 23 |
return Vec3{x, y, z} |
| 24 |
} |
| 25 |
|
| 26 |
// IV3 returns a new 3D vector based on the given int x, y, z values. |
| 27 |
func IV3(x, y, z int) Vec3 { |
| 28 |
return Vec3{float64(x), float64(y), float64(z)} |
| 29 |
} |
| 30 |
|
| 31 |
// String returns the string representation of the vector u. |
| 32 |
func (u Vec3) String() string { |
| 33 |
return fmt.Sprintf("gfx.V3(%v, %v, %v)", u.X, u.Y, u.Z) |
| 34 |
} |
| 35 |
|
| 36 |
// XYZ returns the components of the vector in three return values. |
| 37 |
func (u Vec3) XYZ() (x, y, z float64) { |
| 38 |
return u.X, u.Y, u.Z |
| 39 |
} |
| 40 |
|
| 41 |
// Eq checks the equality of two vectors. |
| 42 |
func (u Vec3) Eq(v Vec3) bool { |
| 43 |
return u.X == v.X && u.Y == v.Y && u.Z == v.Z |
| 44 |
} |
| 45 |
|
| 46 |
// Vec returns a Vec with X, Y coordinates. |
| 47 |
func (u Vec3) Vec() Vec { |
| 48 |
return V(u.X, u.Y) |
| 49 |
} |
| 50 |
|
| 51 |
// Add returns the sum of vectors u and v. |
| 52 |
func (u Vec3) Add(v Vec3) Vec3 { |
| 53 |
return Vec3{ |
| 54 |
u.X + v.X, |
| 55 |
u.Y + v.Y, |
| 56 |
u.Z + v.Z, |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
// AddXYZ returns the sum of x, y and z added to u. |
| 61 |
func (u Vec3) AddXYZ(x, y, z float64) Vec3 { |
| 62 |
return Vec3{ |
| 63 |
u.X + x, |
| 64 |
u.Y + y, |
| 65 |
u.Z + z, |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// Sub returns the difference betweeen vectors u and v. |
| 70 |
func (u Vec3) Sub(v Vec3) Vec3 { |
| 71 |
return Vec3{ |
| 72 |
u.X - v.X, |
| 73 |
u.Y - v.Y, |
| 74 |
u.Z - v.Z, |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// Scaled returns the vector u multiplied by c. |
| 79 |
func (u Vec3) Scaled(s float64) Vec3 { |
| 80 |
return Vec3{ |
| 81 |
u.X * s, |
| 82 |
u.Y * s, |
| 83 |
u.Z * s, |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// ScaledXYZ returns the component-wise multiplication of two vectors. |
| 88 |
func (u Vec3) ScaledXYZ(v Vec3) Vec3 { |
| 89 |
return Vec3{ |
| 90 |
u.X * v.X, |
| 91 |
u.Y * v.Y, |
| 92 |
u.Z * v.Z, |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// Len returns the length (euclidian norm) of a vector. |
| 97 |
func (u Vec3) Len() float64 { |
| 98 |
return math.Sqrt(u.SqLen()) |
| 99 |
} |
| 100 |
|
| 101 |
// Div returns the vector v/s. |
| 102 |
func (u Vec3) Div(s float64) Vec3 { |
| 103 |
return Vec3{ |
| 104 |
u.X / s, |
| 105 |
u.Y / s, |
| 106 |
u.Z / s, |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
// Dot returns the dot product of vectors u and v. |
| 111 |
func (u Vec3) Dot(v Vec3) float64 { |
| 112 |
return u.X*v.X + u.Y*v.Y + u.Z*v.Z |
| 113 |
} |
| 114 |
|
| 115 |
// SqDist returns the square of the euclidian distance between two vectors. |
| 116 |
func (u Vec3) SqDist(v Vec3) float64 { |
| 117 |
return u.Sub(v).SqLen() |
| 118 |
} |
| 119 |
|
| 120 |
// Dist returns the euclidian distance between two vectors. |
| 121 |
func (u Vec3) Dist(v Vec3) float64 { |
| 122 |
return u.Sub(v).Len() |
| 123 |
} |
| 124 |
|
| 125 |
// SqLen returns the square of the length (euclidian norm) of a vector. |
| 126 |
func (u Vec3) SqLen() float64 { |
| 127 |
return u.Dot(u) |
| 128 |
} |
| 129 |
|
| 130 |
// Unit returns the normalized vector of a vector. |
| 131 |
func (u Vec3) Unit() Vec3 { |
| 132 |
return u.Div(u.Len()) |
| 133 |
} |
| 134 |
|
| 135 |
// Map applies the function f to the x, y and z components of the vector u |
| 136 |
// and returns the modified vector. |
| 137 |
func (u Vec3) Map(f func(float64) float64) Vec3 { |
| 138 |
return Vec3{ |
| 139 |
f(u.X), |
| 140 |
f(u.Y), |
| 141 |
f(u.Z), |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// Lerp returns the linear interpolation between v and w by amount t. |
| 146 |
// The amount t is usually a value between 0 and 1. If t=0 v will be |
| 147 |
// returned; if t=1 w will be returned. |
| 148 |
func (u Vec3) Lerp(v Vec3, t float64) Vec3 { |
| 149 |
return Vec3{ |
| 150 |
Lerp(u.X, v.X, t), |
| 151 |
Lerp(u.Y, v.Y, t), |
| 152 |
Lerp(u.Z, v.Z, t), |
| 153 |
} |
| 154 |
} |