-
Notifications
You must be signed in to change notification settings - Fork 4
/
rect.go
53 lines (42 loc) · 835 Bytes
/
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
package tulib
type Rect struct {
X int
Y int
Width int
Height int
}
func (r Rect) IsValid() bool {
return r.Width >= 0 && r.Height >= 0
}
func (r Rect) IsEmpty() bool {
return r.Width <= 0 || r.Height <= 0
}
func (a Rect) FitsIn(b Rect) bool {
return a == a.Intersection(b)
}
func (a Rect) Intersection(b Rect) Rect {
// no intersection cases
if a.X >= b.X+b.Width || a.Y >= b.Y+b.Height {
return Rect{0, 0, 0, 0}
}
if a.X+a.Width <= b.X || a.Y+a.Height <= b.Y {
return Rect{0, 0, 0, 0}
}
// adjust X or Width
if a.X+a.Width > b.X+b.Width {
a.Width = (b.X + b.Width) - a.X
}
if a.X < b.X {
a.Width -= b.X - a.X
a.X = b.X
}
// adjust Y or Height
if a.Y+a.Height > b.Y+b.Height {
a.Height = (b.Y + b.Height) - a.Y
}
if a.Y < b.Y {
a.Height -= b.Y - a.Y
a.Y = b.Y
}
return a
}