-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpos.go
89 lines (74 loc) · 1.44 KB
/
pos.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
package types
import (
"fmt"
"math"
)
type Pos [3]int
// zero position
var PosZero = Pos{0, 0, 0}
// size of a mapblock
var MapBlockSize = Pos{16, 16, 16}
func NewPos(x, y, z int) *Pos {
return &Pos{x, y, z}
}
func NewPosFromIndex(i int) *Pos {
x := i % 16
i /= 16
y := i % 16
i /= 16
z := i % 16
return NewPos(x, y, z)
}
func SortPos(p1, p2 *Pos) (*Pos, *Pos) {
return &Pos{
min(p1[0], p2[0]),
min(p1[1], p2[1]),
min(p1[2], p2[2]),
}, &Pos{
max(p1[0], p2[0]),
max(p1[1], p2[1]),
max(p1[2], p2[2]),
}
}
func (p *Pos) X() int { return p[0] }
func (p *Pos) Y() int { return p[1] }
func (p *Pos) Z() int { return p[2] }
func (p *Pos) String() string {
return fmt.Sprintf("Pos{%d,%d,%d}", p.X(), p.Y(), p.Z())
}
func (p1 *Pos) Add(p2 *Pos) *Pos {
return &Pos{
p1[0] + p2[0],
p1[1] + p2[1],
p1[2] + p2[2],
}
}
func (p1 *Pos) Subtract(p2 *Pos) *Pos {
return &Pos{
p1[0] - p2[0],
p1[1] - p2[1],
p1[2] - p2[2],
}
}
func (p *Pos) Divide(n float64) *Pos {
return &Pos{
int(math.Floor(float64(p[0]) / n)),
int(math.Floor(float64(p[1]) / n)),
int(math.Floor(float64(p[2]) / n)),
}
}
func (p1 *Pos) Multiply(n int) *Pos {
return &Pos{
p1[0] * n,
p1[1] * n,
p1[2] * n,
}
}
func (p *Pos) IsWithin(min, max *Pos) bool {
return p[0] >= min[0] && p[0] <= max[0] &&
p[1] >= min[1] && p[1] <= max[1] &&
p[2] >= min[2] && p[2] <= max[2]
}
func (p *Pos) Index() int {
return p[0] + (p[1] * 16) + (p[2] * 256)
}