/
BoundingBox.jl
611 lines (499 loc) · 16 KB
/
BoundingBox.jl
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
mutable struct BoundingBox
corner1::Point
corner2::Point
end
function Base.show(io::IO, bbox::BoundingBox)
print(io, " ⤡ ", bbox.corner1, " : ", bbox.corner2)
end
"""
`BoundingBox()` with no arguments returns a BoundingBox that
includes the current drawing.
The default `BoundingBox(;centered=true)` returns a
BoundingBox the same size and position as the current
drawing, and assumes the origin (0, 0) is at the center.
If the `centered` option is `false`, the function assumes
that the origin is at the top left of the drawing. So this
function doesn't really work if the current matrix has been
modified (by `translate()`, `scale()`, `rotate()` etc.)
An instance of the BoundingBox type holds two Points,
`corner1` and `corner2`.
BoundingBox(;centered = true) # the bounding box of the Drawing
BoundingBox(s::AbstractString) # the bounding box of a text string at the origin
BoundingBox(pt::Array) # the bounding box of a polygon
BoundingBox(circle(O, 100)) # the bounding box of a path added by circle()
BoundingBox(path::Path) # the bounding box of a Path
You can use `BoundingBox()` with the functions that add
graphic shapes to the current path (eg `box()`, `circle()`,
`star()`, `ngon()`). But note that eg `BoundingBox(box(O, 100, 100))` adds a shape to the current path as well as
returning a bounding box.
"""
function BoundingBox(;
centered = true)
if currentdrawing() == false
# return a default bounding box of 600×600
r = BoundingBox(Point(-300, -300), Point(300, 300))
elseif centered
# ignore current matrix
r = BoundingBox(
Point(
-_current_width() / 2,
-_current_height() / 2,
),
Point(
_current_width() / 2,
_current_height() / 2,
),
)
else
b = getmatrix()
setmatrix([1.0, 0.0, 0.0, 1.0, 0.0, 0.0])
r = BoundingBox(
Point(0, 0),
Point(_current_width(), _current_height()),
)
setmatrix(b)
end
return r
end
"""
BoundingBox(pointlist::Array)
Return the BoundingBox of a polygon (array of points).
"""
function BoundingBox(pointlist::Array{Point,1})
lowx, lowy = pointlist[1].x, pointlist[1].y
highx, highy = pointlist[end].x, pointlist[end].y
for p in pointlist
p.x < lowx && (lowx = p.x)
p.y < lowy && (lowy = p.y)
p.x > highx && (highx = p.x)
p.y > highy && (highy = p.y)
end
return BoundingBox(
Point(lowx, lowy),
Point(highx, highy))
end
BoundingBox(pts::NTuple{2,Point}) = BoundingBox(pts...)
"""
BoundingBox(str::AbstractString)
Return a BoundingBox that just encloses a text string, given the current font
selection. Uses the Toy text API (ie `text()` and `textextents()`).
Text is assumed to be placed at the origin (`0/0`).
"""
function BoundingBox(str::AbstractString)
xbearing, ybearing, width, height, xadvance, yadvance =
textextents(str)
lcorner = Point(xbearing, ybearing)
ocorner = Point(lcorner.x + width, lcorner.y + height)
return BoundingBox(lcorner, ocorner)
end
#state is either 1 or 2!
function Base.iterate(bbox::BoundingBox)
return (bbox.corner1, 2)
end
function Base.iterate(bbox::BoundingBox, state)
if state > 2
return
end
state == 1 ? (bbox.corner1, state + 1) :
state == 2 ? (bbox.corner2, state + 1) :
error("this should never happen")
end
Base.last(bbox::BoundingBox) = bbox.corner2
Base.size(bbox::BoundingBox) = (2)
function Base.length(bbox::BoundingBox)
return 2
end
unsafe_length(bbox::BoundingBox) = length(bbox)
Base.eltype(::Type{BoundingBox}) = Point
Base.lastindex(bbox::BoundingBox) = 2
function Base.getindex(bbox::BoundingBox, i::Int)
if i == 1
return bbox.corner1
else
return bbox.corner2
end
end
Base.getindex(bbox::BoundingBox, i::Number) =
bbox[convert(Int, i)]
Base.getindex(bbox::BoundingBox, I) = [bbox[i] for i in I]
function Base.setindex(bbox::BoundingBox, v::Point, i::Number)
if i == 1
return BoundingBox(v, bbox.corner2)
else
return BoundingBox(bbox.corner1, v)
end
end
# basics
# adding a number to a box makes the box larger all round
+(z::Number, bb1::BoundingBox) =
BoundingBox(bb1.corner1 - z, bb1.corner2 + z)
+(bb1::BoundingBox, z::Number) =
BoundingBox(bb1.corner1 - z, bb1.corner2 + z)
# subtracting a number makes the box smaller all round
-(z::Number, bb1::BoundingBox) =
BoundingBox(bb1.corner1 + z, bb1.corner2 - z)
-(bb1::BoundingBox, z::Number) =
BoundingBox(bb1.corner1 + z, bb1.corner2 - z)
function (*)(bb::BoundingBox, s::Real)
c = midpoint(bb...)
newcorner1 = between(c, bb.corner1, s)
newcorner2 = between(c, bb.corner2, s)
return BoundingBox(newcorner1, newcorner2)
end
(*)(s::Real, bb::BoundingBox) = bb * s
function (/)(bb::BoundingBox, s::Real)
c = midpoint(bb...)
newcorner1 = between(c, bb.corner1, 1 / s)
newcorner2 = between(c, bb.corner2, 1 / s)
return BoundingBox(newcorner1, newcorner2)
end
(/)(s::Real, bb::BoundingBox) = bb / s
# modifying with tuples shifts the box in x and y
+(bb1::BoundingBox, shift::NTuple{2,Real}) =
BoundingBox(
Point(
bb1.corner1.x + shift[1],
bb1.corner1.y + shift[2]),
Point(
bb1.corner2.x + shift[1],
bb1.corner2.y + shift[2]))
-(bb1::BoundingBox, shift::NTuple{2,Real}) =
BoundingBox(
Point(
bb1.corner1.x - shift[1],
bb1.corner1.y - shift[2]),
Point(
bb1.corner2.x - shift[1],
bb1.corner2.y - shift[2]))
# also add/subtract a point to a bounding box shifts the box
+(bb1::BoundingBox, pt::Point) =
+(bb1::BoundingBox, (pt.x, pt.y))
+(pt::Point, bb1::BoundingBox) = +(bb1, pt)
-(bb1::BoundingBox, pt::Point) =
-(bb1::BoundingBox, (pt.x, pt.y))
-(pt::Point, bb1::BoundingBox) = -(bb1, pt)
# if you add two bounding boxes, you get the biggest enclosing box
function +(bb1::BoundingBox, bb2::BoundingBox)
pts = [bb1.corner1, bb1.corner2, bb2.corner1, bb2.corner2]
lox, hix = sort!(pts, by = a -> a.x, lt = (a, b) -> a < b)[[1, end]]
loy, hiy = sort!(pts, by = a -> a.y, lt = (a, b) -> a < b)[[1, end]]
return BoundingBox(
Point(lox.x, loy.y),
Point(hix.x, hiy.y))
end
"""
boxwidth(bb::BoundingBox=BoundingBox())
Return the width of bounding box `bb`.
"""
boxwidth(bb::BoundingBox = BoundingBox()) =
abs(bb.corner1.x - bb.corner2.x)
"""
boxheight(bb::BoundingBox=BoundingBox())
Return the height of bounding box `bb`.
"""
boxheight(bb::BoundingBox = BoundingBox()) =
abs(bb.corner1.y - bb.corner2.y)
"""
boxdiagonal(bb::BoundingBox=BoundingBox())
Return the length of the diagonal of bounding box `bb`.
"""
boxdiagonal(bb::BoundingBox = BoundingBox()) =
hypot(boxwidth(bb), boxheight(bb))
"""
boxaspectratio(bb::BoundingBox=BoundingBox())
Return the aspect ratio (the height divided by the width) of bounding box `bb`.
"""
boxaspectratio(bb::BoundingBox = BoundingBox()) =
boxheight(bb) / boxwidth(bb)
"""
convert(Point, bbox::BoundingBox)
Convert a BoundingBox to a four-point clockwise polygon.
convert(Vector{Point}, BoundingBox())
"""
function Base.convert(::Type{Vector{Point}}, bbox::BoundingBox)
return [
bbox.corner1,
Point(bbox.corner2.x, bbox.corner1.y),
bbox.corner2,
Point(bbox.corner1.x, bbox.corner2.y)]
end
"""
box(bbox::BoundingBox;
action = :none,
vertices = false)
box(bbox::BoundingBox, action::Symbol;
vertices=false)
Define a box using the bounds in `bbox`.
Use `vertices = true` to return an array of the four corner points: bottom left,
top left, top right, bottom right.
"""
function box(bbox::BoundingBox;
action = :none,
vertices = false)
if vertices || action == :none
botleft = Point(bbox.corner1.x, bbox.corner2.y)
topleft = bbox.corner1
topright = Point(bbox.corner2.x, bbox.corner1.y)
botright = bbox.corner2
return [botleft, topleft, topright, botright]
else
box(bbox.corner1, bbox.corner2, action)
end
end
box(bbox::BoundingBox, action::Symbol) =
box(bbox, action = action, vertices = false)
"""
poly(bbox::BoundingBox, :action; kwargs...)
Make a polygon around the BoundingBox in `bbox`.
"""
poly(bbox::BoundingBox, action::Symbol = :none; kwargs...) =
poly(convert(Vector{Point}, bbox), action; kwargs...)
"""
prettypoly(bbox::BoundingBox, action; kwargs...)
Make a decorated polygon around the BoundingBox in `bbox`. The vertices are in
the order: bottom left, top left, top right, and bottom right.
"""
prettypoly(bbox::BoundingBox, a::Symbol = :none;
action = a,
close = false,
reversepath = false,
vertexlabels = (n, l) -> ()) =
prettypoly(convert(Vector{Point}, bbox),
action = action,
close = close,
reversepath = reversepath,
vertexlabels = vertexlabels)
"""
boundingboxesintersect(bbox1::BoundingBox, bbox2::BoundingBox)
boundingboxesintersect(acorner1::Point, acorner2::Point, bcorner1::Point, bcorner2::Point)
Return true if the two bounding boxes intersect.
"""
function boundingboxesintersect(acorner1::Point,
acorner2::Point,
bcorner1::Point,
bcorner2::Point)
minax, maxax = minmax(acorner1.x, acorner2.x)
minay, maxay = minmax(acorner1.y, acorner2.y)
minbx, maxbx = minmax(bcorner1.x, bcorner2.x)
minby, maxby = minmax(bcorner1.y, bcorner2.y)
maxax < minbx && return false
maxbx < minax && return false
maxay < minby && return false
maxby < minay && return false
return true # boxes overlap
end
boundingboxesintersect(bbox1::BoundingBox, bbox2::BoundingBox) =
boundingboxesintersect(bbox1.corner1,
bbox1.corner2,
bbox2.corner1,
bbox2.corner2)
"""
intersectboundingboxes(bb1::BoundingBox, bb2::BoundingBox)
Return a BoundingBox that's an intersection of the two bounding boxes.
"""
function intersectboundingboxes(bb1::BoundingBox, bb2::BoundingBox)
!boundingboxesintersect(bb1, bb2) && return BoundingBox(O, O)
minax, maxax = minmax(bb1.corner1.x, bb1.corner2.x)
minay, maxay = minmax(bb1.corner1.y, bb1.corner2.y)
minbx, maxbx = minmax(bb2.corner1.x, bb2.corner2.x)
minby, maxby = minmax(bb2.corner1.y, bb2.corner2.y)
return BoundingBox(
Point(max(minax, minbx), max(minay, minby)),
Point(min(maxax, maxbx), min(maxay, maxby)))
end
"""
isinside(p::Point, bb:BoundingBox)
Returns `true` if `pt` is inside bounding box `bb`.
"""
isinside(p::Point, bb::BoundingBox) =
(bb.corner1.x <= p.x <= bb.corner2.x) &&
(bb.corner1.y <= p.y <= bb.corner2.y)
"""
midpoint(bb::BoundingBox=BoundingBox())
Returns the point midway between the two points of the BoundingBox. This should
also be the center, unless I've been very stupid...
"""
midpoint(bb::BoundingBox = BoundingBox()) = midpoint(bb...)
"""
between(bb::BoundingBox, x)
Find a point between the two corners of a BoundingBox corresponding to `x`,
where `x` is typically between 0 and 1.
"""
between(bb::BoundingBox, k = 0.5) = between(bb[1], bb[2], k)
"""
boxtopleft(bb::BoundingBox=BoundingBox())
Return the point at the top left of the BoundingBox `bb`, defaulting to the drawing extent.
```
■ ⋅ ⋅
⋅ ⋅ ⋅
⋅ ⋅ ⋅
```
"""
boxtopleft(bb::BoundingBox = BoundingBox()) = bb[1]
"""
boxtopcenter(bb::BoundingBox=BoundingBox())
Return the point at the top center of the BoundingBox `bb`, defaulting to the drawing extent.
```
⋅ ■ ⋅
⋅ ⋅ ⋅
⋅ ⋅ ⋅
```
"""
boxtopcenter(bb::BoundingBox = BoundingBox()) =
midpoint(bb.corner1, bb.corner2) - (0, boxheight(bb) / 2)
"""
boxtopright(bb::BoundingBox=BoundingBox())
Return the point at the top right of the BoundingBox `bb`, defaulting to the drawing extent.
```
⋅ ⋅ ■
⋅ ⋅ ⋅
⋅ ⋅ ⋅
```
"""
boxtopright(bb::BoundingBox = BoundingBox()) =
Point(bb[2].x, bb[1].y)
"""
boxmiddleleft(bb::BoundingBox=BoundingBox())
Return the point at the middle left of the BoundingBox `bb`, defaulting to the drawing extent.
```
⋅ ⋅ ⋅
■ ⋅ ⋅
⋅ ⋅ ⋅
```
"""
boxmiddleleft(bb::BoundingBox = BoundingBox()) =
Point(bb[1].x, midpoint(bb[1], bb[2]).y)
"""
boxmiddlecenter(bb::BoundingBox=BoundingBox())
Return the point at the center of the BoundingBox `bb`, defaulting to the drawing extent.
```
⋅ ⋅ ⋅
⋅ ■ ⋅
⋅ ⋅ ⋅
```
"""
boxmiddlecenter(bb::BoundingBox = BoundingBox()) =
midpoint(bb[1], bb[2])
"""
boxmiddleright(bb::BoundingBox=BoundingBox())
Return the point at the midde right of the BoundingBox `bb`, defaulting to the drawing extent.
```
⋅ ⋅ ⋅
⋅ ⋅ ■
⋅ ⋅ ⋅
```
"""
boxmiddleright(bb::BoundingBox = BoundingBox()) =
Point(bb[2].x, midpoint(bb[1], bb[2]).y)
"""
boxbottomleft(bb::BoundingBox=BoundingBox())
Return the point at the bottom left of the BoundingBox `bb`, defaulting to the drawing extent.
```
⋅ ⋅ ⋅
⋅ ⋅ ⋅
■ ⋅ ⋅
```
"""
boxbottomleft(bb::BoundingBox = BoundingBox()) =
Point(bb[1].x, bb[2].y)
"""
boxbottomcenter(bb::BoundingBox=BoundingBox())
Return the point at the bottom center of the BoundingBox `bb`, defaulting to the drawing extent.
```
⋅ ⋅ ⋅
⋅ ⋅ ⋅
⋅ ■ ⋅
```
"""
boxbottomcenter(bb::BoundingBox = BoundingBox()) =
midpoint(bb.corner1, bb.corner2) +
(0, boxheight(bb) / 2)
"""
boxbottomright(bb::BoundingBox=BoundingBox())
Return the point at the bottom right of the BoundingBox `bb`, defaulting to the drawing extent.
```
⋅ ⋅ ⋅
⋅ ⋅ ⋅
⋅ ⋅ ■
```
"""
boxbottomright(bb::BoundingBox = BoundingBox()) = bb[2]
# legacy defs will be deprecated
boxtop(bb::BoundingBox) = boxtopcenter(bb)
boxbottom(bb::BoundingBox) = boxbottomcenter(bb)
"""
pointcrossesboundingbox(pt, bbox::BoundingBox)
Find and return the point where a line from the center of bounding box
`bbox` to point `pt` would, if continued, cross the edges of the box.
"""
function pointcrossesboundingbox(pt, bbox::BoundingBox)
minpt, maxpt = extrema(bbox)
mp = midpoint(minpt, maxpt)
midX, midY = mp.x, mp.y
m = (midY - pt.y) / (midX - pt.x)
if pt.x <= midX # left
min_Xy = m * (minpt.x - pt.x) + pt.y
if minpt.y <= min_Xy && min_Xy <= maxpt.y
return Point(minpt.x, min_Xy)
end
end
if pt.x >= midX # right
max_Xy = m * (maxpt.x - pt.x) + pt.y
if minpt.y <= max_Xy && max_Xy <= maxpt.y
return Point(maxpt.x, max_Xy)
end
end
if pt.y <= midY # top
min_Yx = (minpt.y - pt.y) / m + pt.x
if minpt.x <= min_Yx && min_Yx <= maxpt.x
return Point(min_Yx, minpt.y)
end
end
if pt.y >= midY # bottom
max_Yx = (maxpt.y - pt.y) / m + pt.x
if minpt.x <= max_Yx && max_Yx <= maxpt.x
return Point(max_Yx, maxpt.y)
end
end
if pt.x == midX && pt.y == midY
return Point(pt.x, pt.y)
end
error("oh dear, something has gone wrong but I don't know what")
end
"""
rand(bbox::BoundingBox)
Return a random `Point` that lies inside `bbox`.
"""
Base.rand(bb::BoundingBox) =
Point((bb.corner2.x - bb.corner1.x) * rand() + bb.corner1.x,
(bb.corner2.y - bb.corner1.y) * rand() + bb.corner1.y)
"""
in(pt, bbox::BoundingBox)
Test whether `pt` is inside `bbox`.
"""
Base.in(pt::Point, bbox::BoundingBox) =
isinside(pt, bbox::BoundingBox)
# for BoundingBox(path::Path) -> Path.jl
"""
box(bbox::BoundingBox, cornerradii::Array;
action = :none)
Make a box that is the size of the BoundingBox `bbox` with curved corners.
"""
function box(bbox::BoundingBox, cornerradii::Array;
action = :none)
centerpoint = boxmiddlecenter(bbox)
width = boxwidth(bbox)
height = boxheight(bbox)
box(centerpoint, width, height, cornerradii::Array;
action = action)
end
"""
box(bbox::BoundingBox, cornerradius::Real;
action = :none)
Make a box that is the size of the BoundingBox `bbox` with curved corners.
"""
function box(bbox::BoundingBox, cornerradius::Real;
action = :none)
box(bbox, fill(cornerradius, 4),
action = action)
end