-
Notifications
You must be signed in to change notification settings - Fork 12
/
hershey_kiss.go
50 lines (43 loc) · 1.04 KB
/
hershey_kiss.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
package main
import (
"math"
"github.com/unixpickle/model3d/model3d"
)
const (
HersheyKissRadius = 0.4
HersheyKissHeight = 0.7
HersheyKissMinThickness = 0.05
HersheyKissCurlStart = 0.9
)
type HersheyKissSolid struct {
Center model3d.Coord3D
}
func (h HersheyKissSolid) Min() model3d.Coord3D {
return h.Center.Add(model3d.Coord3D{X: -HersheyKissRadius, Y: -HersheyKissRadius})
}
func (h HersheyKissSolid) Max() model3d.Coord3D {
return h.Center.Add(model3d.Coord3D{
X: HersheyKissRadius,
Y: HersheyKissRadius,
Z: HersheyKissHeight,
})
}
func (h HersheyKissSolid) Contains(c model3d.Coord3D) bool {
if !model3d.InBounds(h, c) {
return false
}
y := (c.Z - h.Center.Z) / HersheyKissHeight
if y > HersheyKissCurlStart {
c.X += 4 * math.Pow(y-HersheyKissCurlStart, 2)
}
x := c.Sub(h.Center).XY().Norm() / HersheyKissRadius
if x > 1 {
return false
} else if x < HersheyKissMinThickness {
// Solid cylinder center.
return true
}
x = 1 - x
hForX := 0.5 + math.Asin(2*math.Pow(x, 0.8)-1)/math.Pi
return y <= hForX
}