/
rect.go
342 lines (305 loc) · 8.25 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package floatgeom
// A Rect2 represents a span from one point in 2D space to another.
// If Min is less than max on any axis, it will return undefined results
// for methods.
type Rect2 struct {
Min, Max Point2
}
// MaxDimensions reports that a Rect2 has only two dimensions of definition.
func (r Rect2) MaxDimensions() int {
return 2
}
// A Rect3 represents a span from one point in 3D space to another.
// If Min is less than Max on any axis, it will return undefined results
// for methods.
type Rect3 struct {
Min, Max Point3
}
// MaxDimensions reports that a Rect3 has only three dimensions of definition.
func (r Rect3) MaxDimensions() int {
return 3
}
// NewRect2 returns an (X,Y):(X2,Y2) rectangle. This enforces that
// x < x2 and y < y2, and will swap the inputs if that is not true.
// If that enforcement is not desired, construct the struct manually.
func NewRect2(x, y, x2, y2 float64) Rect2 {
if x > x2 {
x, x2 = x2, x
}
if y > y2 {
y, y2 = y2, y
}
return Rect2{
Min: Point2{x, y},
Max: Point2{x2, y2},
}
}
// NewRect2WH returns an (X,Y):(X+W,Y+H) rectangle. This enforces that
// w and h are positive, and will decrease x and y respectively if that is not true.
func NewRect2WH(x, y, w, h float64) Rect2 {
if w < 0 {
x += w
w *= -1
}
if h < 0 {
y += h
h *= -1
}
return Rect2{
Min: Point2{x, y},
Max: Point2{x + w, y + h},
}
}
// NewBoundingRect2 will produce the minimal rectangle that contains all of
// the input points.
func NewBoundingRect2(pts ...Point2) Rect2 {
return Rect2{
Min: pts[0].LesserOf(pts...),
Max: pts[0].GreaterOf(pts...),
}
}
// NewRect3 returns an (X,Y,Z):(X2,Y2,Z2) rectangle. This enforces that
// x < x2, y < y2, and z < z2, and will swap the inputs if that is not true.
func NewRect3(x, y, z, x2, y2, z2 float64) Rect3 {
if x > x2 {
x, x2 = x2, x
}
if y > y2 {
y, y2 = y2, y
}
if z > z2 {
z, z2 = z2, z
}
return Rect3{
Min: Point3{x, y, z},
Max: Point3{x2, y2, z2},
}
}
// NewRect3WH returns an (X,Y,Z):(X+W,Y+H,Z+D) rectangle. This enforces that
// w, h, and d and positive, and will decrease x, y, and z respectively if that
// is not true.
func NewRect3WH(x, y, z, w, h, d float64) Rect3 {
if w < 0 {
x += w
w *= -1
}
if h < 0 {
y += h
h *= -1
}
if d < 0 {
z += d
d *= -1
}
return Rect3{
Min: Point3{x, y, z},
Max: Point3{x + w, y + h, z + d},
}
}
// NewBoundingRect3 will produce the minimal rectangle that contains all of
// the input points.
func NewBoundingRect3(pts ...Point3) Rect3 {
return Rect3{
Min: pts[0].LesserOf(pts...),
Max: pts[0].GreaterOf(pts...),
}
}
// Shift moves the rectangle by a point returns a new instance
func (r Rect2) Shift(p Point2) Rect2 {
r.Min = r.Min.Add(p)
r.Max = r.Max.Add(p)
return r
}
// Shift moves the rectangle by a point returns a new instance
func (r Rect3) Shift(p Point3) Rect3 {
r.Min = r.Min.Add(p)
r.Max = r.Max.Add(p)
return r
}
// Area returns W * H.
func (r Rect2) Area() float64 {
return r.W() * r.H()
}
// Span returns the span on this rectangle's ith axis.
func (r Rect2) Span(i int) float64 {
return r.Max[i] - r.Min[i]
}
// W returns the width of this rectangle.
func (r Rect2) W() float64 {
return r.Span(0)
}
// H returns the height of this rectangle.
func (r Rect2) H() float64 {
return r.Span(1)
}
// Space returns W * H * D
func (r Rect3) Space() float64 {
return r.W() * r.H() * r.D()
}
// Span returns the span on this rectangle's ith axis.
func (r Rect3) Span(i int) float64 {
return r.Max[i] - r.Min[i]
}
// W returns the width of this rectangle.
func (r Rect3) W() float64 {
return r.Span(0)
}
// H returns the height of this rectangle.
func (r Rect3) H() float64 {
return r.Span(1)
}
// D returns the depth of this rectangle.
func (r Rect3) D() float64 {
return r.Span(2)
}
// Midpoint returns the midpoint of this rectangle's span over a given dimension.
func (r Rect2) Midpoint(i int) float64 {
return (r.Min[i] + r.Max[i]) / 2
}
// Midpoint returns the midpoint of this rectangle's span over a given dimension.
func (r Rect3) Midpoint(i int) float64 {
return (r.Min[i] + r.Max[i]) / 2
}
// Center returns the center of this rectangle
func (r Rect2) Center() Point2 {
return Point2{
r.Midpoint(0),
r.Midpoint(1),
}
}
// Center returns the center of this rectangle
func (r Rect3) Center() Point3 {
return Point3{
r.Midpoint(0),
r.Midpoint(1),
r.Midpoint(2),
}
}
// Perimeter computes the sum of the edge lengths of a rectangle.
func (r Rect2) Perimeter() float64 {
// The number of edges in an n-dimensional rectangle is n * 2^(n-1)
// (http://en.wikipedia.org/wiki/Hypercube_graph). Thus the number
// of edges of length (ai - bi), where the rectangle is determined
// by p = (a1, a2, ..., an) and q = (b1, b2, ..., bn), is 2^(n-1).
//
// The margin of the rectangle, then, is given by the formula
// 2^(n-1) * [(b1 - a1) + (b2 - a2) + ... + (bn - an)].
return 2 * (r.W() + r.H())
}
// Margin computes the sum of the edge lengths of a rectangle.
func (r Rect3) Margin() float64 {
return 4 * (r.W() + r.H() + r.D())
}
// Contains tests whether p is located inside or on the boundary of r.
func (r Rect2) Contains(p Point2) bool {
return (p.X() >= r.Min.X() &&
p.X() <= r.Max.X() &&
p.Y() >= r.Min.Y() &&
p.Y() <= r.Max.Y())
}
// Contains tests whether p is located inside or on the boundary of r.
func (r Rect3) Contains(p Point3) bool {
return (p.X() >= r.Min.X() &&
p.X() <= r.Max.X() &&
p.Y() >= r.Min.Y() &&
p.Y() <= r.Max.Y() &&
p.Z() >= r.Min.Z() &&
p.Z() <= r.Max.Z())
}
// ContainsRect tests whether r2 is is located inside r1.
func (r Rect2) ContainsRect(r2 Rect2) bool {
return (r2.Min.X() >= r.Min.X() &&
r2.Max.X() <= r.Max.X() &&
r2.Min.Y() >= r.Min.Y() &&
r2.Max.Y() <= r.Max.Y())
}
// ContainsRect tests whether r2 is is located inside r1.
func (r Rect3) ContainsRect(r2 Rect3) bool {
return (r2.Min.X() >= r.Min.X() &&
r2.Max.X() <= r.Max.X() &&
r2.Min.Y() >= r.Min.Y() &&
r2.Max.Y() <= r.Max.Y() &&
r2.Min.Z() >= r.Min.Z() &&
r2.Max.Z() <= r.Max.Z())
}
// GreaterOf returns a rectangle formed of the lowest values on each
// dimension for Min, and the highest for Max.
func (r Rect2) GreaterOf(r2 Rect2) Rect2 {
r.Min = r.Min.LesserOf(r2.Min)
r.Max = r.Max.GreaterOf(r2.Max)
return r
}
// GreaterOf returns a rectangle formed of the lowest values on each
// dimension for Min, and the highest for Max.
func (r Rect3) GreaterOf(r2 Rect3) Rect3 {
r.Min = r.Min.LesserOf(r2.Min)
r.Max = r.Max.GreaterOf(r2.Max)
return r
}
// Intersects returns whether the two rectangles intersect.
func (r Rect3) Intersects(r2 Rect3) bool {
// There are four cases of overlap:
//
// 1. a1------------b1
// a2------------b2
// p--------q
//
// 2. a1------------b1
// a2------------b2
// p--------q
//
// 3. a1-----------------b1
// a2-------b2
// p--------q
//
// 4. a1-------b1
// a2-----------------b2
// p--------q
//
// Thus there are only two cases of non-overlap:
//
// 1. a1------b1
// a2------b2
//
// 2. a1------b1
// a2------b2
//
// Enforced by constructor: a1 <= b1 and a2 <= b2. So we can just
// check the endpoints.
return !((r2.Max.X() <= r.Min.X() || r.Max.X() <= r2.Min.X()) ||
(r2.Max.Y() <= r.Min.Y() || r.Max.Y() <= r2.Min.Y()) ||
(r2.Max.Z() <= r.Min.Z() || r.Max.Z() <= r2.Min.Z()))
}
// Intersects returns whether the two rectangles intersect.
func (r Rect2) Intersects(r2 Rect2) bool {
// There are four cases of overlap:
//
// 1. a1------------b1
// a2------------b2
// p--------q
//
// 2. a1------------b1
// a2------------b2
// p--------q
//
// 3. a1-----------------b1
// a2-------b2
// p--------q
//
// 4. a1-------b1
// a2-----------------b2
// p--------q
//
// Thus there are only two cases of non-overlap:
//
// 1. a1------b1
// a2------b2
//
// 2. a1------b1
// a2------b2
//
// Enforced by constructor: a1 <= b1 and a2 <= b2. So we can just
// check the endpoints.
return !((r2.Max.X() <= r.Min.X() || r.Max.X() <= r2.Min.X()) ||
(r2.Max.Y() <= r.Min.Y() || r.Max.Y() <= r2.Min.Y()))
}