-
Notifications
You must be signed in to change notification settings - Fork 0
/
box2.go
143 lines (118 loc) · 2.75 KB
/
box2.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
package math
type Box2 struct {
min Vector2
max Vector2
}
// Equivalent to makeEmpty
func NewDefaultBox2() *Box2 {
return NewBox2(
NewVector2Inf(1),
NewVector2Inf(-1),
)
}
func NewBox2(min *Vector2, max *Vector2) *Box2 {
return &Box2{
min: Vector2{X: min.X, Y: min.Y},
max: Vector2{X: max.X, Y: max.Y},
}
}
func NewBox2FromComponents(minX, minY, maxX, maxY float32) *Box2 {
return &Box2{
min: Vector2{X: minX, Y: minY},
max: Vector2{X: maxX, Y: maxY},
}
}
func NewBox2FromCenterAndSize(center *Vector2, size *Vector2) *Box2 {
halfSize := size.Clone()
halfSize.MultiplyScalar(0.5)
nb := &Box2{
min: *center.Clone(),
max: *center.Clone(),
}
nb.min.Sub(halfSize)
nb.max.Add(halfSize)
return nb
}
func (box *Box2) Clone() *Box2 {
return &Box2{
min: *box.min.Clone(),
max: *box.max.Clone(),
}
}
func (box *Box2) Copy(source *Box2) {
box.min.Copy(&source.min)
box.max.Copy(&source.max)
}
func (box *Box2) IsEmpty() bool {
return box.max.X < box.min.X || box.max.Y < box.min.Y
}
func (box *Box2) GetCenter() *Vector2 {
if box.IsEmpty() {
return NewVector2(0, 0)
} else {
c := NewVector2(0, 0)
c.SetAddVectors(&box.min, &box.max)
c.MultiplyScalar(0.5)
return c
}
}
func (box *Box2) GetSize() *Vector2 {
if box.IsEmpty() {
return NewVector2(0, 0)
} else {
c := NewVector2(0, 0)
c.SetSubVectors(&box.min, &box.max)
return c
}
}
func (box *Box2) ExpandByPoint(point *Vector2) {
box.min.Min(point)
box.max.Max(point)
}
func (box *Box2) ExpandByVector(vector *Vector2) {
box.min.Sub(vector)
box.max.Add(vector)
}
func (box *Box2) ExpandByScalar(scalar float32) {
box.min.SubScalar(scalar)
box.max.AddScalar(scalar)
}
func (box *Box2) ContainsPoint(point *Vector2) bool {
return !(point.X < box.min.X || point.X > box.max.X ||
point.Y < box.min.Y || point.Y > box.max.Y)
}
func (box *Box2) ContainsBox(b *Box2) bool {
return box.min.X <= b.min.X && b.max.X <= box.max.X &&
box.min.Y <= b.min.Y && b.max.Y <= box.max.Y
}
func (box *Box2) IntersectsBox(b *Box2) bool {
return !(b.max.X < box.min.X || b.min.X > box.max.X ||
b.max.Y < box.min.Y || b.min.Y > box.max.Y)
}
func (box *Box2) ClampPoint(point *Vector2) *Vector2 {
target := NewDefaultVector2()
target.Copy(point)
target.Clamp(&box.min, &box.max)
return target
}
func (box *Box2) DistanceToPoint(point *Vector2) float32 {
v1 := point.Clone()
v1.Clamp(&box.min, &box.max)
v1.Sub(point)
return v1.GetLength()
}
func (box *Box2) Intersect(b *Box2) {
box.min.Max(&b.min)
box.max.Min(&b.max)
}
func (box *Box2) Union(b *Box2) {
box.min.Min(&b.min)
box.max.Max(&b.max)
}
func (box *Box2) Translate(offset *Vector2) {
box.min.Add(offset)
box.max.Add(offset)
}
func (box *Box2) Equals(b *Box2) bool {
return box.min.Equals(&b.min) && box.max.Equals(&b.max)
}