-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrect.go
130 lines (100 loc) · 2.11 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
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
package bento
type Rect struct {
X, Y int
Width, Height int
}
func NewRect(width, height int) Rect {
return Rect{Width: width, Height: height}
}
func (r Rect) Positioned(x, y int) Rect {
r.X = x
r.Y = y
return r
}
func (r Rect) IsEmpty() bool {
return r.Width == 0 || r.Height == 0
}
func (r Rect) Area() int {
return r.Width * r.Height
}
func (r Rect) Left() int {
return r.X
}
func (r Rect) Right() int {
return r.X + r.Width
}
func (r Rect) Top() int {
return r.Y
}
func (r Rect) Bottom() int {
return r.Y + r.Height
}
func (r Rect) Position() Position {
return Position{
X: r.X,
Y: r.Y,
}
}
func (r Rect) Inner(padding Padding) Rect {
horizontal := padding.Right + padding.Left
vertical := padding.Top + padding.Bottom
if r.Width < horizontal || r.Height < vertical {
return Rect{}
}
return Rect{
X: r.X + padding.Left,
Y: r.Y + padding.Top,
Width: max(0, r.Width-horizontal),
Height: max(0, r.Height-vertical),
}
}
func (r Rect) Intersection(other Rect) Rect {
x1 := max(r.X, other.X)
y1 := max(r.Y, other.Y)
x2 := min(r.Right(), other.Right())
y2 := min(r.Bottom(), other.Bottom())
return Rect{
X: x1,
Y: y1,
Width: max(0, x2-x1),
Height: max(0, y2-y1),
}
}
func (r Rect) Contains(position Position) bool {
return position.X >= r.X && position.X < r.Right() && position.Y >= r.Y && position.Y < r.Bottom()
}
func (r Rect) Rows() []Rect {
currentRowFwd := r.Y
currentRowBack := r.Bottom()
var rows []Rect
for currentRowFwd < currentRowBack {
row := Rect{
X: r.X,
Y: currentRowFwd,
Width: r.Width,
Height: 1,
}
currentRowFwd++
rows = append(rows, row)
}
return rows
}
func (r Rect) IndentX(offset int) Rect {
return Rect{
X: r.X + offset,
Y: r.Y,
Width: max(0, r.Width-offset),
Height: r.Height,
}
}
func (r Rect) Columns() []Rect {
currentColumnFwd := r.X
currentColumnBack := r.Right()
var columns []Rect
for currentColumnFwd < currentColumnBack {
column := NewRect(1, r.Height).Positioned(currentColumnFwd, r.Y)
currentColumnFwd++
columns = append(columns, column)
}
return columns
}