/
shapes.jl
594 lines (491 loc) · 18.3 KB
/
shapes.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
"""
rect(xmin, ymin, w, h; action=:none)
rect(xmin, ymin, w, h, action)
Create a rectangle with one corner at (`xmin`/`ymin`) with
width `w` and height `h`, and add it to the current path.
Then apply `action`.
Returns a tuple of two points, the corners of a bounding box
that encloses the rectangle.
See `box()` for more ways to do similar things, such as
supplying two opposite corners, placing by centerpoint and
dimensions.
"""
function rect(xmin::Real, ymin::Real, w::Real, h::Real;
action = :none)
if action != :path
newpath()
end
Cairo.rectangle(_get_current_cr(), xmin, ymin, w, h)
do_action(action)
return Point(xmin, ymin), Point(xmin + w, ymin + h)
end
rect(xmin::Real, ymin::Real, w::Real, h::Real, action::Symbol) = rect(xmin, ymin, w, h, action = action)
"""
rect(cornerpoint, w, h; action = none, reversepath=false,
vertices=false)
rect(cornerpoint, w, h, action; reversepath=false,
vertices=false)
Create a rectangle with one corner at `cornerpoint` with
width `w` and height `h`, and add it to the current path.
Then apply `action`.
Use `vertices=true` to return an array of the four corner
points: bottom left, top left, top right, bottom right.
`reversepath` reverses the direction of the path (and
returns points in the order: bottom left, bottom right, top
right, top left).
Returns the four corner vertices.
"""
function rect(cornerpoint::Point, w::Real, h::Real;
action = :none,
reversepath = false,
vertices = false)
pts = [
Point(cornerpoint.x, cornerpoint.y + h),
Point(cornerpoint.x, cornerpoint.y),
Point(cornerpoint.x + w, cornerpoint.y),
Point(cornerpoint.x + w, cornerpoint.y + h)
]
if reversepath == true
pts = pts[[1, 4, 3, 2]]
end
if vertices == false && action != :none
move(pts[1])
line(pts[2])
line(pts[3])
line(pts[4])
closepath()
do_action(action)
end
return pts
end
rect(cornerpoint::Point, w::Real, h::Real, action::Symbol;
reversepath = false,
vertices = false) = rect(cornerpoint, w, h;
action = action,
reversepath = reversepath,
vertices = vertices)
"""
box(cornerpoint1, cornerpoint2; action=:none, vertices=false, reversepath=false)
box(cornerpoint1, cornerpoint2, action; vertices=false, reversepath=false)
Create a box (rectangle) between two points and add it to
the current path. Then apply `action`.
Use `vertices=true` to return an array of the four corner
points: bottom left, top left, top right, bottom right
rather than execute action.
`reversepath` reverses the direction of the path (and
returns points in the order: bottom left, bottom right, top
right, top left).
"""
function box(corner1::Point, corner2::Point;
action::Symbol=:none,
reversepath=false,
vertices=false)
# rearrange to get topleft -> bottom right
# I'm not sure whether this is worth doing just
# to get the order of vertices correct
xmin, xmax = extrema((corner1.x, corner2.x))
ymin, ymax = extrema((corner1.y, corner2.y))
c1 = Point(xmin, ymin)
c2 = Point(xmax, ymax)
pts = [
Point(c1.x, c2.y),
Point(c1.x, c1.y),
Point(c2.x, c1.y),
Point(c2.x, c2.y)
]
if reversepath == true
pts = pts[[1, 4, 3, 2]]
end
if vertices == false && action != :none
move(pts[1])
line(pts[2])
line(pts[3])
line(pts[4])
closepath()
do_action(action)
end
return pts
end
box(corner1::Point, corner2::Point, action::Symbol;
reversepath=false,
vertices=false) = box(corner1, corner2;
action=action,
reversepath=reversepath,
vertices=vertices)
"""
box(points::Array; action=:none,
reversepath=reversepath,
vertices=vertices)
box(points::Array; action=:none,
reversepath=reversepath,
vertices=vertices)
Create a box/rectangle using the first two points of an
array of Points to defined opposite corners, and add it to
the current path. Then apply `action`.
Use `vertices=true` to return an array of the four corner
points: bottom left, top left, top right, bottom right
rather than execute action.
"""
box(bbox::Array, action::Symbol; kwargs...) =
box(bbox[1], bbox[2], action; kwargs...)
box(bbox::Array; kwargs...) =
box(bbox[1], bbox[2]; kwargs...)
"""
box(pt::Point, width, height; action=:none, vertices=false)
box(pt::Point, width, height, action=:none; vertices=false)
Create a box/rectangle centered at point `pt` with width and
height. Use `vertices=true` to return an array of the four
corner points rather than apply the action.
`reversepath` reverses the direction of the path.
"""
function box(pt::Point, width, height;
action = :none,
reversepath = false,
vertices = false)
pts = [
Point(pt.x - width/2, pt.y + height/2),
Point(pt.x - width/2, pt.y - height/2),
Point(pt.x + width/2, pt.y - height/2),
Point(pt.x + width/2, pt.y + height/2)
]
if reversepath == true
pts = pts[[1, 4, 3, 2]]
end
if vertices == false && action != :none
move(pts[1])
line(pts[2])
line(pts[3])
line(pts[4])
closepath()
do_action(action)
end
return pts
end
box(pt::Point, width, height, action::Symbol;
reversepath = false,
vertices = false) = box(pt, width, height;
action = action,
reversepath = reversepath,
vertices = vertices)
box(x::Real, y::Real, width::Real, height::Real, action::Symbol) =
rect(x - width/2.0, y - height/2.0, width, height, action)
box(x::Real, y::Real, width::Real, height::Real; action::Symbol=:none) =
rect(x - width/2.0, y - height/2.0, width, height, action)
"""
box(pt, width, height, cornerradius, action=:none)
box(pt, width, height, cornerradius; action=:none)
Draw a box/rectangle centered at point `pt` with `width` and `height` and
round each corner by `cornerradius`.
The constructed path consists of arcs and straight lines.
"""
box(centerpoint::Point, width, height, cornerradius::Real, action::Symbol) =
box(centerpoint, width, height, fill(cornerradius, 4), action=action)
box(centerpoint::Point, width, height, cornerradius::Real; action=:none) =
box(centerpoint, width, height, fill(cornerradius, 4), action=action)
"""
box(pt, width, height, cornerradii::Array; action=:none)
box(pt, width, height, cornerradii::Array, action=:none)
Draw a box/rectangle centered at point `pt` with `width` and `height` and
round each corner by the corresponding value in the array `cornerradii`.
The constructed path consists of arcs and straight lines.
The first corner is the one at the bottom left, the second at
the top left, and so on.
## Example
```
@draw begin
box(O, 120, 120, [0, 20, 40, 60], :fill)
end
```
"""
function box(centerpoint::Point, width, height, cornerradii::Array;
action=:none)
gsave()
translate(centerpoint)
length(cornerradii) != 4 && throw(error("box() must have four values to specify rounded corners"))
# bottom left
p1start = Point(O.x - width/2 + cornerradii[1], O.y + height/2)
p1center = Point(O.x - width/2 + cornerradii[1], O.y + height/2 - cornerradii[1])
p1end = Point(O.x - width/2, O.y + height/2 - cornerradii[1])
# top left
p2start = Point(O.x - width/2, O.y - height/2 + cornerradii[2])
p2center = Point(O.x - width/2 + cornerradii[2], O.y - height/2 + cornerradii[2])
p2end = Point(O.x - width/2 + cornerradii[2], O.y - height/2)
# top right
p3start = Point(O.x + width/2 - cornerradii[3], O.y - height/2)
p3center = Point(O.x + width/2 - cornerradii[3], O.y - height/2 + cornerradii[3])
p3end = Point(O.x + width/2, O.y - height/2 + cornerradii[3])
# bottom right
p4start = Point(O.x + width/2, O.y + height/2 - cornerradii[4])
p4center = Point(O.x + width/2 - cornerradii[4], O.y + height/2 - cornerradii[4])
p4end = Point(O.x + width/2 - cornerradii[4], O.y + height/2)
# start at bottom center then bottomleft→topleft→topright→bottomright
newpath()
move(Point(O.x, O.y + height/2))
line(p1start)
arc(p1center, cornerradii[1], π/2, π, :none)
line(p1end)
line(p2start)
arc(p2center, cornerradii[2], π, (3π)/2, :none)
line(p2end)
line(p3start)
arc(p3center, cornerradii[3], 3π/2, 0, :none)
line(p3end)
line(p4start)
arc(p4center, cornerradii[4], 0, π/2, :none)
line(p4end)
closepath()
grestore()
do_action(action)
return Point(centerpoint.x - width/2, centerpoint.y - height/2),
Point(centerpoint.x + width/2, centerpoint.y + height/2)
end
box(centerpoint::Point, width, height, cornerradii::Array, action::Symbol) =
box(centerpoint::Point, width, height, cornerradii::Array, action=action)
"""
ngon(x, y, radius, sides=5, orientation=0;
action = :none,
vertices = false,
reversepath = false)
Draw a regular polygon centered at point `Point(x,y)`.
"""
function ngon(x::Real, y::Real, radius::Real, sides::Int=5, orientation=0.0;
action=:none,
vertices=false,
reversepath=false)
ptlist = [Point(x+cos(orientation + n * 2pi/sides) * radius,
y+sin(orientation + n * 2pi/sides) * radius) for n in 1:sides]
if !vertices
poly(ptlist, action=action, close=true, reversepath=reversepath)
end
if reversepath
return reverse(ptlist)
else
return ptlist
end
end
ngon(x::Real, y::Real, radius::Real, sides::Int, action::Symbol) =
ngon(x, y, radius, sides, action=action, vertices=false, reversepath=false)
ngon(x::Real, y::Real, radius::Real, sides::Int, orientation::Real, action::Symbol) =
ngon(x, y, radius, sides, orientation, action=action, vertices=false, reversepath=false)
# Point version
"""
ngon(centerpos, radius, sides=5, orientation=0;
action=:none,
vertices=false,
reversepath=false)
Draw a regular polygon centered at point `centerpos`.
Find the vertices of a regular n-sided polygon centered at `x`, `y` with
circumradius `radius`.
The polygon is constructed counterclockwise, starting with the first vertex
drawn below the positive x-axis.
If you just want the raw points, use keyword argument `vertices=true`, which
returns the array of points. Compare:
```julia
ngon(0, 0, 4, 4, 0, vertices=true) # returns the polygon's points:
4-element Array{Luxor.Point, 1}:
Luxor.Point(2.4492935982947064e-16, 4.0)
Luxor.Point(-4.0, 4.898587196589413e-16)
Luxor.Point(-7.347880794884119e-16, -4.0)
Luxor.Point(4.0, -9.797174393178826e-16)
```
whereas
```
ngon(0, 0, 4, 4, 0, :close) # draws a polygon
```
"""
ngon(centerpoint::Point, radius, sides::Int=5, orientation=0.0;
action=:none, vertices=false, reversepath=false) = ngon(centerpoint.x, centerpoint.y, radius, sides, orientation,
action=action, vertices=vertices, reversepath=reversepath)
# action as argument
# this is a bit untidy?
ngon(centerpoint::Point, radius::Real, sides::Int, orientation::Real, a::Symbol;
action=:none, vertices=false, reversepath=false) = ngon(centerpoint, radius, sides, orientation,
action=a, vertices=vertices, reversepath=reversepath)
"""
ngonside(centerpoint::Point, sidelength::Real, sides::Int=5, orientation=0;
action=:none,
vertices=false,
reversepath=false)
ngonside(centerpoint::Point, sidelength::Real, sides::Int, orientation, action;
vertices=false,
reversepath=false)
Draw a regular polygon centered at `centerpoint` with `sides` sides of length `sidelength`.
"""
function ngonside(centerpoint::Point, sidelength::Real, sides::Int=5, orientation=0;
action=:none,
vertices=false,
reversepath=false)
radius = 0.5 * sidelength * csc(pi/sides)
ngon(centerpoint, radius, sides, orientation, action=action,
vertices=vertices,
reversepath=reversepath)
end
ngonside(centerpoint::Point, sidelength::Real, sides::Int, orientation::Real, action::Symbol;
vertices=false,
reversepath=false) = ngonside(centerpoint, sidelength, sides, orientation, action=action,
vertices=vertices, reversepath=reversepath)
ngonside(centerpoint::Point, sidelength::Real, sides::Int, action::Symbol;
vertices=false,
reversepath=false) = ngonside(centerpoint, sidelength, sides, action=action,
vertices=vertices, reversepath=reversepath)
function star(x::Real, y::Real, radius::Real, npoints::Int=5, ratio::Real=0.5, orientation=0;
action=:none,
vertices = false,
reversepath=false)
outerpoints = [Point(x+cos(orientation + n * 2pi/npoints) * radius,
y+sin(orientation + n * 2pi/npoints) * radius) for n in 1:npoints]
innerpoints = [Point(x+cos(orientation + (n + 1/2) * 2pi/npoints) * (radius * ratio),
y+sin(orientation + (n + 1/2) * 2pi/npoints) * (radius * ratio)) for n in 1:npoints]
result = Point[]
for i in eachindex(outerpoints)
push!(result, outerpoints[i])
push!(result, innerpoints[i])
end
if reversepath
result = reverse(result)
end
if !vertices
poly(result, action=action, close=true)
end
return result
end
"""
star(center, radius, npoints, ratio=0.5, orientation, action=:none;
vertices = false, reversepath=false)
star(center, radius, npoints, ratio=0.5, orientation=0.0;
action=:none, vertices = false, reversepath=false)
Make a star centered at `center` with `npoints` sections oriented by `orientation`.
`ratio` specifies the height of the smaller radius of the
star relative to the larger.
Returns the vertices of the star.
Use `vertices=true` to only return the vertices of a star
instead of making it.
## Examples
```julia
star(O, 120, 5, 0.5, 0.0, :fill,
vertices = false,
reversepath=false)
star(O, 220, 5, 0.5;
action=:stroke,
vertices = false,
reversepath=false)
```
"""
function star(centerpoint::Point, radius::Real, npoints::Int=5, ratio::Real=0.5, orientation=0;
action=:none,
vertices=false,
reversepath=false)
star(centerpoint.x, centerpoint.y, radius, npoints, ratio, orientation, action=action,
vertices = vertices, reversepath=reversepath)
end
star(x::Real, y::Real, radius::Real, npoints::Int, ratio::Real, orientation, a::Symbol;
action=:none,
vertices = false,
reversepath=false) = star(Point(x, y), radius, npoints, ratio, orientation,
action=a, vertices = vertices, reversepath=reversepath)
star(pt::Point, radius::Real, npoints::Int, ratio::Real, orientation, a::Symbol;
action=:none,
vertices = false,
reversepath=false) = star(pt, radius, npoints, ratio, orientation,
action=a, vertices = vertices, reversepath=reversepath)
"""
cropmarks(center, width, height)
Draw cropmarks (also known as trim marks). Use current color.
"""
function cropmarks(center, width, height)
gap = 5
crop = 15
gsave()
setline(0.5)
setdash("solid")
# horizontal top left
line(Point(-width/2 - gap - crop, -height/2),
Point(-width/2 - gap, -height/2),
:stroke)
# horizontal bottom left
line(Point(-width/2 - gap - crop, height/2),
Point(-width/2 - gap, height/2),
:stroke)
# horizontal top right
line(Point(width/2 + gap, -height/2),
Point(width/2 + gap + crop, -height/2),
:stroke)
# horizontal bottom right
line(Point(width/2 + gap, height/2),
Point(width/2 + gap + crop, height/2),
:stroke)
# vertical top left
line(Point(-width/2, -height/2 - gap - crop),
Point(-width/2, -height/2 - gap),
:stroke)
# vertical bottom left
line(Point(-width/2, height/2 + gap),
Point(-width/2, height/2 + gap + crop),
:stroke)
# vertical top right
line(Point(width/2, -height/2 - gap - crop),
Point(width/2, -height/2 - gap),
:stroke)
# vertical bottom right
line(Point(width/2, height/2 + gap),
Point(width/2, height/2 + gap + crop),
:stroke)
grestore()
end
"""
polycross(pt::Point, radius, npoints::Int, ratio=0.5, orientation=0.0;
action = :none,
splay = 0.5,
vertices = false,
reversepath = false)
polycross(pt::Point, radius, npoints::Int, ratio=0.5, orientation=0.0, action;
splay = 0.5,
vertices = false,
reversepath = false)
Make a cross-shaped polygon with `npoints` arms to fit
inside a circle of radius `radius` centered at `pt`.
`ratio` specifies the ratio of the two sides of each arm.
`splay` makes the arms ... splayed.
Use `vertices=true` to return the vertices of the shape
instead of executing the action.
(Adapted from Compose.jl.xgon()))
## Examples
```julia
polycross(O, 100, 5,
action = :fill,
splay = 0.5)
polycross(O, 120, 5, 0.5, 0.0, :stroke,
splay = 0.5)
```
"""
function polycross(pt::Point, radius::Real, npoints::Int, ratio=0.5, orientation=0.0;
action = :none,
splay = .5,
vertices = false,
reversepath = false)
# adapted from: Compose.jl, https://github.com/GiovineItalia/Compose.jl/src/form.jl
# original author: mattriks
ratio = clamp(ratio, 0.0, 1.0)
θ₁ = range(π/2 + orientation + 0, stop = π/2 + orientation + 2π, length = npoints + 1)[1:end-1]
width = 2radius * ratio * sin(π/npoints)
dₒ = asin(clamp(splay * width/radius, -1.0, 1.0))
dᵢ = asin(0.5 * width/(radius * ratio))
r₂ = repeat([radius * ratio, radius, radius], outer = npoints)
θ₂ = vec([mod2pi(θ + x) for x in [-dᵢ, -dₒ, dₒ], θ in θ₁])
pts = @. Point.(pt.x .+ r₂ .* cos.(θ₂), pt.y .+ r₂ .* sin.(θ₂))
if reversepath
reverse!(pts)
end
if !vertices
poly(pts, action=action, close=true)
end
return pts
end
polycross(pt::Point, radius::Real, npoints::Int, ratio, orientation::Real, action::Symbol;
splay = .5,
vertices = false,
reversepath = false) = polycross(pt, radius, npoints, ratio, orientation,
action = action,
splay = splay,
vertices = vertices,
reversepath = reversepath)