-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.go
41 lines (34 loc) · 875 Bytes
/
utility.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
package main
import (
"math"
"math/rand"
)
func randomDouble() float64 {
return rand.Float64()
}
func randomDoubleMinMax(min, max float64) float64 {
return min + (max-min)*randomDouble()
}
func sampleSquare() Vec3 {
return Vec3{randomDouble() - 0.5, randomDouble() - 0.5, 0}
}
func randomVec(min, max float64) Vec3 {
return Vec3{randomDoubleMinMax(min, max), randomDoubleMinMax(min, max), randomDoubleMinMax(min, max)}
}
func randomUnitVector() Vec3 {
for {
p := randomVec(-1, 1)
//1e160 check for floating point overflow
if p.lengthSquared() >= 1 && 1e160 > p.lengthSquared() {
continue
}
return p.divide(math.Sqrt(p.lengthSquared()))
}
}
func (normal Vec3) randomOnHemisphere() Vec3 {
onUnitSphere := randomUnitVector()
if normal.dot(onUnitSphere) > 0.0 { // Same hemisphere as normal
return onUnitSphere
}
return onUnitSphere.scale(-1)
}