-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuples.go
212 lines (182 loc) · 4.2 KB
/
tuples.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package jbtracer
import (
"fmt"
"math"
)
// Epsilon is the expected precision for our floating point operations
const Epsilon = 0.00001
type Tuple struct {
X, Y, Z float64 // 3D coordinates
W float64 // 1.0 when a point, 0.0 when a vector
}
// String returns a string representation of the tuple
func (a *Tuple) String() string {
var types string
if a.IsPoint() {
types = "point"
} else {
types = "vector"
}
return fmt.Sprintf("x=%+2.5f, y=%+2.5f, z=%+2.5f (%s)", a.X, a.Y, a.Z, types)
}
// IsPoint returns true if this Tuple is a point
func (a *Tuple) IsPoint() bool {
return math.Abs(a.W-1.0) < Epsilon
}
// IsVector returns true if this Tuple is a vector
func (a *Tuple) IsVector() bool {
return a.W < Epsilon
}
// Equal determines if two Tuples are the same
func (a *Tuple) Equal(b *Tuple) bool {
return EqualFloat64(a.X, b.X) && EqualFloat64(a.Y, b.Y) && EqualFloat64(a.Z, b.Z) && EqualFloat64(a.W, b.W)
}
// Add adds one tuple to another
func (a *Tuple) Add(b *Tuple) *Tuple {
return &Tuple{
X: a.X + b.X,
Y: a.Y + b.Y,
Z: a.Z + b.Z,
W: a.W + b.W,
}
}
// Add subtracts one tuple from another
func (a *Tuple) Subtract(b *Tuple) *Tuple {
return &Tuple{
X: a.X - b.X,
Y: a.Y - b.Y,
Z: a.Z - b.Z,
W: a.W - b.W,
}
}
// Negate negates a tuple
func (a *Tuple) Negate() *Tuple {
return &Tuple{
X: a.X * -1.0,
Y: a.Y * -1.0,
Z: a.Z * -1.0,
W: a.W * -1.0,
}
}
// Multiply multiplies a tuple by a scalar
func (a *Tuple) Multiply(scalar float64) *Tuple {
return &Tuple{
X: a.X * scalar,
Y: a.Y * scalar,
Z: a.Z * scalar,
W: a.W * scalar,
}
}
// Divide divides a tuple by a scalar
func (a *Tuple) Divide(scalar float64) *Tuple {
return &Tuple{
X: a.X / scalar,
Y: a.Y / scalar,
Z: a.Z / scalar,
W: a.W / scalar,
}
}
// Magnitude returns the magnitude (or length) of the tuple
func (a *Tuple) Magnitude() float64 {
return math.Sqrt(a.X*a.X + a.Y*a.Y + a.Z*a.Z + a.W*a.W)
}
// Normalize returns a normalized unit vector
func (a *Tuple) Normalize() *Tuple {
m := a.Magnitude()
return &Tuple{
X: a.X / m,
Y: a.Y / m,
Z: a.Z / m,
W: a.W / m,
}
}
// Dot returns the dot product of this vector with the provided vector
func (a *Tuple) Dot(b *Tuple) float64 {
return a.X*b.X + a.Y*b.Y + a.Z*b.Z + a.W*b.W
}
// Cross returns the cross product of this vector with the provided vector
func (a *Tuple) Cross(b *Tuple) *Tuple {
return NewVector(
a.Y*b.Z-a.Z*b.Y,
a.Z*b.X-a.X*b.Z,
a.X*b.Y-a.Y*b.X,
)
}
// EqualFloat64 determines if two float64 values are the within Epsilon of each other
func EqualFloat64(a, b float64) bool {
return math.Abs(a-b) < Epsilon
}
// Reflect reflects vector v around the normal n
func (v *Tuple) Reflect(n *Tuple) *Tuple {
return v.Subtract(n.Multiply(2 * v.Dot(n)))
}
// NewPoint creates a new Tuple of type point
func NewPoint(X, Y, Z float64) *Tuple {
point := &Tuple{
X: X,
Y: Y,
Z: Z,
W: 1.0,
}
return point
}
// NewVector creates a new Tuple of type vector
func NewVector(X, Y, Z float64) *Tuple {
vector := &Tuple{
X: X,
Y: Y,
Z: Z,
W: 0.0,
}
return vector
}
type Color struct {
Red, Green, Blue float64
}
var (
Black *Color = &Color{0, 0, 0}
White *Color = &Color{1, 1, 1}
)
func NewColor(red, green, blue float64) *Color {
return &Color{
Red: red,
Green: green,
Blue: blue,
}
}
// Equal determines if two Colors are the same
func (a *Color) Equal(b *Color) bool {
return EqualFloat64(a.Red, b.Red) && EqualFloat64(a.Green, b.Green) && EqualFloat64(a.Blue, b.Blue)
}
// Add adds one Color to another
func (a *Color) Add(b *Color) *Color {
return &Color{
Red: a.Red + b.Red,
Green: a.Green + b.Green,
Blue: a.Blue + b.Blue,
}
}
// Add subtracts one Color from another
func (a *Color) Subtract(b *Color) *Color {
return &Color{
Red: a.Red - b.Red,
Green: a.Green - b.Green,
Blue: a.Blue - b.Blue,
}
}
// Multiply multiplies this Color by another Color
func (a *Color) Multiply(b *Color) *Color {
return &Color{
Red: a.Red * b.Red,
Green: a.Green * b.Green,
Blue: a.Blue * b.Blue,
}
}
// MultiplyScalar multiplies this Color by a scalar
func (a *Color) MultiplyScalar(scalar float64) *Color {
return &Color{
Red: a.Red * scalar,
Green: a.Green * scalar,
Blue: a.Blue * scalar,
}
}