forked from drakmaniso/old-cozely
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec4.go
155 lines (127 loc) · 3.74 KB
/
vec4.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
// Copyright (c) 2013 Laurent Moussault. All rights reserved.
// Licensed under a simplified BSD license (see LICENSE file).
package glam
import "github.com/Ferguzz/glam/math"
//------------------------------------------------------------------------------
// `Vec4` is single-precision vector with 4 components.
type Vec4 struct {
X float32
Y float32
Z float32
W float32
}
//------------------------------------------------------------------------------
// `Dehomogenized` returns the dehomogenization of `a` (perspective divide).
// `a.W` must be non-zero.
func (a Vec4) Dehomogenized() Vec3 {
return Vec3{a.X / a.W, a.Y / a.W, a.Z / a.W}
}
//------------------------------------------------------------------------------
// `Plus` returns the sum `a + b`.
//
// See also `Add`.
func (a Vec4) Plus(b Vec4) Vec4 {
return Vec4{a.X + b.X, a.Y + b.Y, a.Z + b.Z, a.W + b.W}
}
// `Add` sets `a` to the sum `a + b`.
//
// More efficient than `Plus`.
func (a *Vec4) Add(b Vec4) {
a.X += b.X
a.Y += b.Y
a.Z += b.Z
a.W += b.W
}
//------------------------------------------------------------------------------
// `Minus` returns the difference `a - b`.
//
// See also `Subtract`.
func (a Vec4) Minus(b Vec4) Vec4 {
return Vec4{a.X - b.X, a.Y - b.Y, a.Z - b.Z, a.W - b.W}
}
// `Subtract` sets `a` to the difference `a - b`.
// More efficient than `Minus`.
func (a *Vec4) Subtract(b Vec4) {
a.X -= b.X
a.Y -= b.Y
a.Z -= b.Z
a.W -= b.W
}
//------------------------------------------------------------------------------
// `Inverse` return the inverse of `a`.
//
// See also `Invert`.
func (a Vec4) Inverse() Vec4 {
return Vec4{-a.X, -a.Y, -a.Z, -a.W}
}
// `Invert` sets `a` to its inverse.
// More efficient than `Inverse`.
func (a *Vec4) Invert() {
a.X = -a.X
a.Y = -a.Y
a.Z = -a.Z
a.W = -a.W
}
//------------------------------------------------------------------------------
// `Times` returns the product of `a` with the scalar `s`.
//
// See also `Multiply`.
func (a Vec4) Times(s float32) Vec4 {
return Vec4{a.X * s, a.Y * s, a.Z * s, a.W * s}
}
// `Multiply` sets `a` to the product of `a` with the scalar `s`.
// More efficient than `Times`.
func (a *Vec4) Multiply(s float32) {
a.X *= s
a.Y *= s
a.Z *= s
a.W *= s
}
//------------------------------------------------------------------------------
// `Slash` returns the division of `a` by the scalar `s`.
// `s` must be non-zero.
//
// See also `Divide`.
func (a Vec4) Slash(s float32) Vec4 {
return Vec4{a.X / s, a.Y / s, a.Z / s, a.W / s}
}
// `Divide` sets `a` to the division of `a` by the scalar `s`.
// `s` must be non-zero.
//
// More efficient than `Slash`.
func (a *Vec4) Divide(s float32) {
a.X /= s
a.Y /= s
a.Z /= s
a.W /= s
}
//------------------------------------------------------------------------------
// `Dot` returns the dot product of `a` and `b`.
func (a Vec4) Dot(b Vec4) float32 {
return a.X*b.X + a.Y*b.Y + a.Z*b.Z + a.W*b.W
}
//------------------------------------------------------------------------------
// `Length` returns `|a|` (the euclidian length of `a`).
func (a Vec4) Length() float32 {
return math.Sqrt(a.X*a.X + a.Y*a.Y + a.Z*a.Z + a.W*a.W)
}
// `Normalized` return `a/|a|` (i.e. the normalization of `a`).
// `a` must be non-zero.
//
// See also `Normalize`.
func (a Vec4) Normalized() Vec4 {
length := math.Sqrt(a.X*a.X + a.Y*a.Y + a.Z*a.Z + a.W*a.W)
return Vec4{a.X / length, a.Y / length, a.Z / length, a.W / length}
}
// `Normalize` sets `a` to `a/|a|` (i.e. normalizes `a`).
// `a` must be non-zero.
//
// More efficitent than `Normalized`.
func (a *Vec4) Normalize() {
length := math.Sqrt(a.X*a.X + a.Y*a.Y + a.Z*a.Z + a.W*a.W)
a.X /= length
a.Y /= length
a.Z /= length
a.W /= length
}
//------------------------------------------------------------------------------