-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvec3.go
154 lines (131 loc) · 3.08 KB
/
vec3.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
package gfx
import (
"fmt"
"math"
)
// Vec3 is a 3D vector type with X, Y and Z coordinates.
//
// Create vectors with the V3 constructor:
//
// u := gfx.V3(1, 2, 3)
// v := gfx.V3(8, -3, 4)
type Vec3 struct {
X, Y, Z float64
}
// ZV3 is the zero Vec3
var ZV3 = Vec3{0, 0, 0}
// V3 is shorthand for Vec3{X: x, Y: y, Z: z}.
func V3(x, y, z float64) Vec3 {
return Vec3{x, y, z}
}
// IV3 returns a new 3D vector based on the given int x, y, z values.
func IV3(x, y, z int) Vec3 {
return Vec3{float64(x), float64(y), float64(z)}
}
// String returns the string representation of the vector u.
func (u Vec3) String() string {
return fmt.Sprintf("gfx.V3(%v, %v, %v)", u.X, u.Y, u.Z)
}
// XYZ returns the components of the vector in three return values.
func (u Vec3) XYZ() (x, y, z float64) {
return u.X, u.Y, u.Z
}
// Eq checks the equality of two vectors.
func (u Vec3) Eq(v Vec3) bool {
return u.X == v.X && u.Y == v.Y && u.Z == v.Z
}
// Vec returns a Vec with X, Y coordinates.
func (u Vec3) Vec() Vec {
return V(u.X, u.Y)
}
// Add returns the sum of vectors u and v.
func (u Vec3) Add(v Vec3) Vec3 {
return Vec3{
u.X + v.X,
u.Y + v.Y,
u.Z + v.Z,
}
}
// AddXYZ returns the sum of x, y and z added to u.
func (u Vec3) AddXYZ(x, y, z float64) Vec3 {
return Vec3{
u.X + x,
u.Y + y,
u.Z + z,
}
}
// Sub returns the difference betweeen vectors u and v.
func (u Vec3) Sub(v Vec3) Vec3 {
return Vec3{
u.X - v.X,
u.Y - v.Y,
u.Z - v.Z,
}
}
// Scaled returns the vector u multiplied by c.
func (u Vec3) Scaled(s float64) Vec3 {
return Vec3{
u.X * s,
u.Y * s,
u.Z * s,
}
}
// ScaledXYZ returns the component-wise multiplication of two vectors.
func (u Vec3) ScaledXYZ(v Vec3) Vec3 {
return Vec3{
u.X * v.X,
u.Y * v.Y,
u.Z * v.Z,
}
}
// Len returns the length (euclidian norm) of a vector.
func (u Vec3) Len() float64 {
return math.Sqrt(u.SqLen())
}
// Div returns the vector v/s.
func (u Vec3) Div(s float64) Vec3 {
return Vec3{
u.X / s,
u.Y / s,
u.Z / s,
}
}
// Dot returns the dot product of vectors u and v.
func (u Vec3) Dot(v Vec3) float64 {
return u.X*v.X + u.Y*v.Y + u.Z*v.Z
}
// SqDist returns the square of the euclidian distance between two vectors.
func (u Vec3) SqDist(v Vec3) float64 {
return u.Sub(v).SqLen()
}
// Dist returns the euclidian distance between two vectors.
func (u Vec3) Dist(v Vec3) float64 {
return u.Sub(v).Len()
}
// SqLen returns the square of the length (euclidian norm) of a vector.
func (u Vec3) SqLen() float64 {
return u.Dot(u)
}
// Unit returns the normalized vector of a vector.
func (u Vec3) Unit() Vec3 {
return u.Div(u.Len())
}
// Map applies the function f to the x, y and z components of the vector u
// and returns the modified vector.
func (u Vec3) Map(f func(float64) float64) Vec3 {
return Vec3{
f(u.X),
f(u.Y),
f(u.Z),
}
}
// Lerp returns the linear interpolation between v and w by amount t.
// The amount t is usually a value between 0 and 1. If t=0 v will be
// returned; if t=1 w will be returned.
func (u Vec3) Lerp(v Vec3, t float64) Vec3 {
return Vec3{
Lerp(u.X, v.X, t),
Lerp(u.Y, v.Y, t),
Lerp(u.Z, v.Z, t),
}
}