-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrect.go
98 lines (76 loc) · 1.44 KB
/
rect.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
package quadtree
const half = 2.0
type rect struct {
X0, Y0, X1, Y1 float64
}
func rc(x, y, w, h float64) (r rect) {
return rect{
X0: x,
Y0: y,
X1: x + w,
Y1: y + h,
}
}
func (r rect) Pad(d float64) (rv rect) {
return rect{
X0: r.X0 - d,
Y0: r.Y0 - d,
X1: r.X1 + d,
Y1: r.Y1 + d,
}
}
func (r rect) Clip(b rect) (rv rect) {
if r.X0 < b.X0 {
r.X0 = b.X0
}
if r.Y0 < b.Y0 {
r.Y0 = b.Y0
}
if r.X1 > b.X1 {
r.X1 = b.X1
}
if r.Y1 > b.Y1 {
r.Y1 = b.Y1
}
return r
}
func (r *rect) Center() (x, y float64) {
dx, dy := r.Width()/half, r.Heigth()/half
return r.X0 + dx, r.Y0 + dy
}
func (r *rect) Width() float64 {
return r.X1 - r.X0
}
func (r *rect) Heigth() float64 {
return r.Y1 - r.Y0
}
func (r *rect) ContainsPoint(x, y float64) (yes bool) {
if x < r.X0 || y < r.Y0 {
return
}
if x > r.X1 || y > r.Y1 {
return
}
return true
}
func (r *rect) ContainsRect(v rect) (yes bool) {
if !r.ContainsPoint(v.X0, v.Y0) {
return
}
if !r.ContainsPoint(v.X1, v.Y1) {
return
}
return true
}
func (r *rect) Overlaps(v rect) (yes bool) {
return r.X0 < v.X1 && r.X1 >= v.X0 && r.Y0 < v.Y1 && r.Y1 >= v.Y0
}
func (r *rect) Split() (rv [4]rect) {
dx, dy := r.Width()/half, r.Heigth()/half
return [4]rect{
{r.X0, r.Y0, r.X0 + dx, r.Y0 + dy}, // upper left
{r.X0 + dx, r.Y0, r.X1, r.Y1 - dy}, // upper right
{r.X0, r.Y0 + dy, r.X0 + dx, r.Y1}, // lower left
{r.X0 + dx, r.Y0 + dy, r.X1, r.Y1}, // lower right
}
}