-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.go
More file actions
93 lines (80 loc) · 2.33 KB
/
Copy pathvector.go
File metadata and controls
93 lines (80 loc) · 2.33 KB
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
package MeshTypes
// Copyright (c) 2025 Michael Fogleman
// Portions adapted from FauxGL (https://github.com/fogleman/fauxgl)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the “Software”), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import "math"
type Vector struct {
X float64
Y float64
Z float64
}
func (a Vector) DivScalar(b float64) Vector {
return Vector{a.X / b, a.Y / b, a.Z / b}
}
func (a Vector) Add(b Vector) Vector {
return Vector{a.X + b.X, a.Y + b.Y, a.Z + b.Z}
}
func (a Vector) Sub(b Vector) Vector {
return Vector{a.X - b.X, a.Y - b.Y, a.Z - b.Z}
}
func (a Vector) Cross(b Vector) Vector {
x := a.Y*b.Z - a.Z*b.Y
y := a.Z*b.X - a.X*b.Z
z := a.X*b.Y - a.Y*b.X
return Vector{x, y, z}
}
func (a Vector) Normalize() Vector {
r := 1 / math.Sqrt(a.X*a.X+a.Y*a.Y+a.Z*a.Z)
return Vector{a.X * r, a.Y * r, a.Z * r}
}
func (obj Vector) ToVertex(normal *Vector) Vertex {
return Vertex{
Position: obj,
Normal: normal,
}
}
func (a Vector) Min(b Vector) Vector {
return Vector{
math.Min(a.X, b.X),
math.Min(a.Y, b.Y),
math.Min(a.Z, b.Z),
}
}
func (a Vector) Max(b Vector) Vector {
return Vector{
math.Max(a.X, b.X),
math.Max(a.Y, b.Y),
math.Max(a.Z, b.Z),
}
}
func (a Vector) Mult(b Vector) Vector {
return Vector{
a.X * b.X,
a.Y * b.Y,
a.Z * b.Z,
}
}
func (a Vector) Div(b Vector) Vector {
return Vector{
a.X / b.X,
a.Y / b.Y,
a.Z / b.Z,
}
}