-
Notifications
You must be signed in to change notification settings - Fork 0
/
rectangle.go
164 lines (147 loc) · 3.96 KB
/
rectangle.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package topdown
// Rectangle is a generic version of image.Rectangle.
type Rectangle[T Number] struct {
Min, Max Point[T]
}
func Rect[T Number](x0, y0, x1, y1 T) Rectangle[T] {
return Rectangle[T]{
Min: Pt(x0, y0),
Max: Pt(x1, y1),
}
}
// String returns a string representation of r like "(3,4)-(6,5)".
func (r Rectangle[T]) String() string {
return r.Min.String() + "-" + r.Max.String()
}
// Dx returns r's width.
func (r Rectangle[T]) Dx() T {
return r.Max.X - r.Min.X
}
// Dy returns r's height.
func (r Rectangle[T]) Dy() T {
return r.Max.Y - r.Min.Y
}
// Size returns the rectangle center point.
func (r Rectangle[T]) Center() Point[T] {
return Pt((r.Min.X+r.Max.X)/2.0, (r.Min.Y+r.Max.Y)/2.0)
}
// Size returns r's width and height.
func (r Rectangle[T]) Size() Size[T] {
return Sz(r.Dx(), r.Dy())
}
// Add returns the rectangle r translated by p.
func (r Rectangle[T]) Add(p Point[T]) Rectangle[T] {
return Rectangle[T]{
Point[T]{r.Min.X + p.X, r.Min.Y + p.Y},
Point[T]{r.Max.X + p.X, r.Max.Y + p.Y},
}
}
// Sub returns the rectangle r translated by -p.
func (r Rectangle[T]) Sub(p Point[T]) Rectangle[T] {
return Rectangle[T]{
Point[T]{r.Min.X - p.X, r.Min.Y - p.Y},
Point[T]{r.Max.X - p.X, r.Max.Y - p.Y},
}
}
// Inset returns the rectangle r inset by n, which may be negative. If either
// of r's dimensions is less than 2*n then an empty rectangle near the center
// of r will be returned.
func (r Rectangle[T]) Inset(n T) Rectangle[T] {
if r.Dx() < 2*n {
r.Min.X = (r.Min.X + r.Max.X) / 2
r.Max.X = r.Min.X
} else {
r.Min.X += n
r.Max.X -= n
}
if r.Dy() < 2*n {
r.Min.Y = (r.Min.Y + r.Max.Y) / 2
r.Max.Y = r.Min.Y
} else {
r.Min.Y += n
r.Max.Y -= n
}
return r
}
// Intersect returns the largest rectangle contained by both r and s. If the
// two rectangles do not overlap then the zero rectangle will be returned.
func (r Rectangle[T]) Intersect(s Rectangle[T]) Rectangle[T] {
if r.Min.X < s.Min.X {
r.Min.X = s.Min.X
}
if r.Min.Y < s.Min.Y {
r.Min.Y = s.Min.Y
}
if r.Max.X > s.Max.X {
r.Max.X = s.Max.X
}
if r.Max.Y > s.Max.Y {
r.Max.Y = s.Max.Y
}
// Letting r0 and s0 be the values of r and s at the time that the method
// is called, this next line is equivalent to:
//
// if max(r0.Min.X, s0.Min.X) >= min(r0.Max.X, s0.Max.X) || likewiseForY { etc }
if r.Empty() {
return Rectangle[T]{}
}
return r
}
// Union returns the smallest rectangle that contains both r and s.
func (r Rectangle[T]) Union(s Rectangle[T]) Rectangle[T] {
if r.Empty() {
return s
}
if s.Empty() {
return r
}
if r.Min.X > s.Min.X {
r.Min.X = s.Min.X
}
if r.Min.Y > s.Min.Y {
r.Min.Y = s.Min.Y
}
if r.Max.X < s.Max.X {
r.Max.X = s.Max.X
}
if r.Max.Y < s.Max.Y {
r.Max.Y = s.Max.Y
}
return r
}
// Empty reports whether the rectangle contains no points.
func (r Rectangle[T]) Empty() bool {
return r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y
}
// Eq reports whether r and s contain the same set of points. All empty
// rectangles are considered equal.
func (r Rectangle[T]) Eq(s Rectangle[T]) bool {
return r == s || r.Empty() && s.Empty()
}
// Overlaps reports whether r and s have a non-empty intersection.
func (r Rectangle[T]) Overlaps(s Rectangle[T]) bool {
return !r.Empty() && !s.Empty() &&
r.Min.X < s.Max.X && s.Min.X < r.Max.X &&
r.Min.Y < s.Max.Y && s.Min.Y < r.Max.Y
}
// In reports whether every point in r is in s.
func (r Rectangle[T]) In(s Rectangle[T]) bool {
if r.Empty() {
return true
}
// Note that r.Max is an exclusive bound for r, so that r.In(s)
// does not require that r.Max.In(s).
return s.Min.X <= r.Min.X && r.Max.X <= s.Max.X &&
s.Min.Y <= r.Min.Y && r.Max.Y <= s.Max.Y
}
// Canon returns the canonical version of r. The returned rectangle has minimum
// and maximum coordinates swapped if necessary so that it is well-formed.
func (r Rectangle[T]) Canon() Rectangle[T] {
if r.Max.X < r.Min.X {
r.Min.X, r.Max.X = r.Max.X, r.Min.X
}
if r.Max.Y < r.Min.Y {
r.Min.Y, r.Max.Y = r.Max.Y, r.Min.Y
}
return r
}