-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint3d.go
111 lines (93 loc) · 1.93 KB
/
point3d.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
package raytracing
import (
"math"
"math/rand"
)
type (
Point3D [3]float64
)
func (self Point3D) Distance(other Point3D) float64 {
dx := self[0] - other[0]
dy := self[1] - other[1]
dz := self[2] - other[2]
return math.Sqrt(dx*dx + dy*dy + dz*dz)
}
func (self Point3D) Length() float64 {
return self.Distance(Point3D{})
}
func (self Point3D) LengthSquared() float64 {
return self[0]*self[0] + self[1]*self[1] + self[2]*self[2]
}
func (self Point3D) Unit() Point3D {
l := self.Length()
return Point3D{
self[0] / l,
self[1] / l,
self[2] / l,
}
}
func (self Point3D) Multiply(v float64) Point3D {
return Point3D{
self[0] * v,
self[1] * v,
self[2] * v,
}
}
func (self Point3D) Dot(other Point3D) float64 {
return self[0]*other[0] +
self[1]*other[1] +
self[2]*other[2]
}
func (self Point3D) Cross(other Point3D) Point3D {
return Point3D{
self[1]*other[2] - self[2]*other[1],
self[2]*other[0] - self[0]*other[2],
self[0]*other[1] - self[1]*other[0],
}
}
func (self Point3D) Add(other Point3D) Point3D {
return Point3D{
self[0] + other[0],
self[1] + other[1],
self[2] + other[2],
}
}
func (self Point3D) Sub(other Point3D) Point3D {
return Point3D{
self[0] - other[0],
self[1] - other[1],
self[2] - other[2],
}
}
func (self Point3D) Divide(v float64) Point3D {
return Point3D{
self[0] / v,
self[1] / v,
self[2] / v,
}
}
func (self Point3D) NearZero() bool {
const epsilon = 0.0001
return self[0] < epsilon && self[1] < epsilon && self[2] < epsilon
}
func DegreesToRadians(degrees float64) float64 {
return degrees * (math.Pi / 180)
}
func RandomPoint(min, max float64) Point3D {
return Point3D{
RandFloat64Range(min, max),
RandFloat64Range(min, max),
RandFloat64Range(min, max),
}
}
func RandomInUnitSphere() Point3D {
for {
p := RandomPoint(-1, 1)
if p.LengthSquared() < 1 {
return p
}
}
}
func RandFloat64Range(min, max float64) float64 {
return rand.Float64()*(max-min) + min
}