-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvector.go
74 lines (61 loc) · 2.01 KB
/
vector.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
/*
gonav - A Source Engine navigation mesh file parser written in Go.
Copyright (C) 2016 Matt Razza
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Package gonav provides functionality related to CS:GO Nav Meshes
package gonav
import "math"
// Vector3 represents a 3D vector or point in space with X, Y, Z components.
type Vector3 struct {
X, Y, Z float32 // The X, Y, and Z coordinates of the vector
}
// LengthSquared gets the square of the length of the Vector.
// This operation is faster than Length().
func (v *Vector3) LengthSquared() float32 {
return v.X*v.X + v.Y*v.Y + v.Z*v.Z
}
// Length gets the length of the Vector.
func (v *Vector3) Length() float32 {
return float32(math.Sqrt(float64(v.LengthSquared())))
}
// Normalize normalizes the Vector by setting its length to 1.
func (v *Vector3) Normalize() {
length := v.Length()
v.X /= length
v.Y /= length
v.Z /= length
}
// Add adds the specified Vector to this one.
func (v *Vector3) Add(right Vector3) {
v.X += right.X
v.Y += right.Y
v.Z += right.Z
}
// Sub subtracts the specified Vector from this one.
func (v *Vector3) Sub(right Vector3) {
v.X -= right.X
v.Y -= right.Y
v.Z -= right.Z
}
// Mul multiplies the specified scalar to this vector.
func (v *Vector3) Mul(right float32) {
v.X *= right
v.Y *= right
v.Z *= right
}
// Div divides this vector by the specified scalar.
func (v *Vector3) Div(right float32) {
v.X /= right
v.Y /= right
v.Z /= right
}