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
144
145
146
147
148
149
150
151
152
|
package layout
type Size struct {
Width, Height int
}
type Rect struct {
X, Y int
Width, Height int
}
func NewRect(x, y, width, height int) Rect {
return Rect{X: x, Y: y, Width: width, Height: height}
}
func (r Rect) Right() int {
return r.X + r.Width
}
func (r Rect) Bottom() int {
return r.Y + r.Height
}
func (r Rect) IsEmpty() bool {
return r.Width <= 0 || r.Height <= 0
}
func (r Rect) Area() int {
if r.Width <= 0 || r.Height <= 0 {
return 0
}
return r.Width * r.Height
}
func (r Rect) Contains(x, y int) bool {
return x >= r.X && x < r.Right() && y >= r.Y && y < r.Bottom()
}
func (r Rect) ContainsRect(other Rect) bool {
if other.IsEmpty() {
return true
}
if r.IsEmpty() {
return false
}
return other.X >= r.X && other.Y >= r.Y &&
other.Right() <= r.Right() && other.Bottom() <= r.Bottom()
}
func (r Rect) Inset(edges Edges) Rect {
return Rect{
X: r.X + edges.Left,
Y: r.Y + edges.Top,
Width: r.Width - edges.Left - edges.Right,
Height: r.Height - edges.Top - edges.Bottom,
}
}
func (r Rect) Outset(edges Edges) Rect {
return Rect{
X: r.X - edges.Left,
Y: r.Y - edges.Top,
Width: r.Width + edges.Left + edges.Right,
Height: r.Height + edges.Top + edges.Bottom,
}
}
func (r Rect) Translate(dx, dy int) Rect {
return Rect{X: r.X + dx, Y: r.Y + dy, Width: r.Width, Height: r.Height}
}
func (r Rect) Intersect(other Rect) Rect {
x := max(r.X, other.X)
y := max(r.Y, other.Y)
right := min(r.Right(), other.Right())
bottom := min(r.Bottom(), other.Bottom())
width := right - x
height := bottom - y
if width <= 0 || height <= 0 {
return Rect{}
}
return Rect{X: x, Y: y, Width: width, Height: height}
}
func (r Rect) Union(other Rect) Rect {
if r.IsEmpty() {
return other
}
if other.IsEmpty() {
return r
}
x := min(r.X, other.X)
y := min(r.Y, other.Y)
right := max(r.Right(), other.Right())
bottom := max(r.Bottom(), other.Bottom())
return Rect{X: x, Y: y, Width: right - x, Height: bottom - y}
}
func (r Rect) Intersects(other Rect) bool {
return !r.Intersect(other).IsEmpty()
}
func (r Rect) Clamp(x, y int) (int, int) {
if r.IsEmpty() {
return r.X, r.Y
}
if x < r.X {
x = r.X
} else if x >= r.Right() {
x = r.Right() - 1
}
if y < r.Y {
y = r.Y
} else if y >= r.Bottom() {
y = r.Bottom() - 1
}
return x, y
}
|