-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdraft.lua
More file actions
816 lines (724 loc) · 23.5 KB
/
draft.lua
File metadata and controls
816 lines (724 loc) · 23.5 KB
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
--[[
Title: draft
Use: drafting module for the LÖVE 2D game engine
Author: Pierre-Emmanuel Lévesque
Email: pierre.e.levesque@gmail.com
Created: July 26th, 2012
Copyright: Copyright 2012, Pierre-Emmanuel Lévesque
License: MIT license - @see LICENSE.md
--]]
----------------------------------------------------
-- Module setup
----------------------------------------------------
local draft = {}
draft.__index = draft
--[[
New (constructor)
@param string drawMode (fill, line, false) [def: fill]
@return table metatable
--]]
local function new(mode)
if mode == nil then mode = 'fill' end
return setmetatable({
mode = mode
}, draft)
end
----------------------------------------------------
-- Getters and setters
----------------------------------------------------
function draft:getMode() return self.mode end
function draft:setMode(mode) self.mode = mode end
----------------------------------------------------
-- Utilities
----------------------------------------------------
--[[
Appends vertices to another set of vertices
@param table vertices {x1, y1, x2, y2, ...}
@param table new vertices {x1, y1, x2, y2, ...}
@return table merged vertices {x1, y1, x2, y2, ...}
--]]
local function appendVertices(vertices, newVertices)
for _,v in ipairs(newVertices) do
table.insert(vertices, v)
end
return vertices
end
----------------------------------------------------
-- Primary shapes (using core drawing functions)
----------------------------------------------------
--[[
Draws lines between points
@param table points {x1, y1, x2, y2, ...}
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table points {x1, y1, x2, y2, ...}
--]]
function draft:line(points, mode)
if mode == nil then mode = self.mode end
if mode then
love.graphics.line(points)
end
return points
end
--[[
Draws an isosceles triangle
@param number center x
@param number center y
@param number width of the base
@param number height
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y2, x2, y2, x3, y3}
--]]
function draft:triangleIsosceles(cx, cy, width, height, mode)
if mode == nil then mode = self.mode end
local widthRadius = width / 2
local heightRadius = height / 2
local x1 = cx
local y1 = cy - heightRadius
local x2 = cx + widthRadius
local y2 = cy + heightRadius
local x3 = cx - widthRadius
local y3 = y2
if mode then
love.graphics.polygon(mode, x1, y1, x2, y2, x3, y3)
end
return {x1, y1, x2, y2, x3, y3}
end
--[[
Draws a right triangle
@param number center x
@param number center y
@param number width of the base (a minus width flips the triangle)
@param number height
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y2, x2, y2, x3, y3}
--]]
function draft:triangleRight(cx, cy, width, height, mode)
if mode == nil then mode = self.mode end
local widthRadius = width / 2
local heightRadius = height / 2
local x1 = cx - widthRadius
local y1 = cy - heightRadius
local x2 = cx + widthRadius
local y2 = cy + heightRadius
local x3 = x1
local y3 = y2
if mode then
love.graphics.polygon(mode, x1, y1, x2, y2, x3, y3)
end
return {x1, y1, x2, y2, x3, y3}
end
--[[
Draws a rectangle
@param number center x
@param number center y
@param number width
@param number height
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y2, x2, y2, x3, y3, x4, y4}
--]]
function draft:rectangle(cx, cy, width, height, mode)
if mode == nil then mode = self.mode end
local widthRadius = width / 2
local heightRadius = height / 2
local left = cx - widthRadius
local right = cx + widthRadius
local top = cy - heightRadius
local bottom = cy + heightRadius
local vertices = {left, top, right, top, right, bottom, left, bottom}
if mode then
love.graphics.rectangle(mode, vertices[1], vertices[2], width, height)
end
return vertices
end
--[[
Draws a polygon
@param table vertices {x1, y1, x2, y2, ...}
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y1, x2, y2, ...}
--]]
function draft:polygon(vertices, mode)
if mode == nil then mode = self.mode end
if mode then
if (mode == 'fill' and not love.math.isConvex(vertices)) then
local triangles = love.math.triangulate(vertices)
for _, v in pairs(triangles) do
love.graphics.polygon('fill', v)
end
else
love.graphics.polygon(mode, vertices)
end
end
return vertices
end
----------------------------------------------------
-- Secondary shapes (using primary shape functions)
----------------------------------------------------
--[[
Draws an equilateral triangle
@param number center x
@param number center y
@param number width of the base
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y2, x2, y2, x3, y3}
@uses self:triangleIsosceles()
--]]
function draft:triangleEquilateral(cx, cy, width, mode)
local height = math.sqrt(math.pow(width, 2) - math.pow(width / 2, 2))
return self:triangleIsosceles(cx, cy, width, height, mode)
end
--[[
Draws a square
@param number center x
@param number center y
@param number length of one side (width or height)
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y2, x2, y2, x3, y3, x4, y4}
@uses self:rectangle()
--]]
function draft:square(cx, cy, length, mode)
return self:rectangle(cx, cy, length, length, mode)
end
--[[
Draws a trapezoid
@param number center x
@param number center y
@param number width (bottom)
@param number height
@param number top width
@param number top width offset (to the left)
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y2, x2, y2, x3, y3, x4, y4}
@uses self:polygon()
--]]
function draft:trapezoid(cx, cy, width, height, widthTop, widthTopOffset, mode)
local widthRadius = width / 2
local heightRadius = height / 2
local widthTopRadiusOffsetted = widthTop / 2 + widthTopOffset
local vertices = {
cx - widthTopRadiusOffsetted, cy - heightRadius,
cx + widthTopRadiusOffsetted, cy - heightRadius,
cx + widthRadius, cy + heightRadius,
cx - widthRadius, cy + heightRadius
}
return self:polygon(vertices, mode)
end
--[[
Draws a rhombus
@param number center x
@param number center y
@param number width
@param number height
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y2, x2, y2, x3, y3, x4, y4}
@uses self:polygon()
--]]
function draft:rhombus(cx, cy, width, height, mode)
local widthRadius = width / 2
local heightRadius = height / 2
local vertices = {
cx - widthRadius, cy,
cx, cy - heightRadius,
cx + widthRadius, cy,
cx, cy + heightRadius
}
return self:polygon(vertices, mode)
end
--[[
Draws a trapezium (no parallel sides)
@param number center x
@param number center y
@param number width left
@param number width right
@param number height
@param number depth
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y2, x2, y2, x3, y3, x4, y4}
@uses self:polygon()
--]]
function draft:trapezium(cx, cy, widthLeft, widthRight, height, depth, mode)
local vertices = {
cx - widthLeft, cy,
cx, cy - height,
cx + widthRight, cy,
cx, cy + depth
}
return self:polygon(vertices, mode)
end
--[[
Draws a gem
@param number center x
@param number center y
@param number top width
@param number middle width
@param number height
@param number depth
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y2, x2, y2, x3, y3, x4, y4, x5, y5}
@uses self:polygon()
--]]
function draft:gem(cx, cy, widthTop, widthMiddle, height, depth, mode)
local widthTopRadius = widthTop / 2
local widthMiddleRadius = widthMiddle / 2
local vertices = {
cx - widthTopRadius, cy - height,
cx + widthTopRadius, cy - height,
cx + widthMiddleRadius, cy,
cx, cy + depth,
cx - widthMiddleRadius, cy
}
return self:polygon(vertices, mode)
end
--[[
Draws a diamond
@param number center x
@param number center y
@param number width
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y2, x2, y2, x3, y3, x4, y4, x5, y5}
@uses self:polygon()
--]]
function draft:diamond(cx, cy, width, mode)
local widthRadius = width / 2
local depth = math.sqrt(math.pow(width, 2) - math.pow(widthRadius, 2)) / 2
local height = depth / 2
local topOffset = widthRadius / 3 * 2
local vertices = {
cx - widthRadius, cy,
cx - topOffset, cy - height,
cx + topOffset, cy - height,
cx + widthRadius, cy,
cx, cy + depth
}
return self:polygon(vertices, mode)
end
----------------------------------------------------
-- Tertiary shapes (using secondary shape functions)
----------------------------------------------------
--[[
Draws an equiangular rhombus (square rotated 45˚)
@param number center x
@param number center y
@param number length of one side (width or height)
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y2, x2, y2, x3, y3, x4, y4}
@uses self:rhombus()
--]]
function draft:rhombusEquilateral(cx, cy, length, mode)
return self:rhombus(cx, cy, length, length, mode)
end
--[[
Draws a lozenge (rhombus with 45 degree angles)
@param number center x
@param number center y
@param number width
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y2, x2, y2, x3, y3, x4, y4}
@uses self:rhombus()
--]]
function draft:lozenge(cx, cy, width, mode)
return self:rhombus(cx, cy, width, width * 2, mode)
end
--[[
Draws a kite
@param number center x
@param number center y
@param number width
@param number height
@param number depth
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y2, x2, y2, x3, y3, x4, y4}
@uses self:trapezium()
--]]
function draft:kite(cx, cy, width, height, depth, mode)
return self:trapezium(cx, cy, width, width, height, depth, mode)
end
--[[
Draws an isosceles trapezoid
@param number center x
@param number center y
@param number width (bottom)
@param number height
@param number top width
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y2, x2, y2, x3, y3, x4, y4}
@uses self:trapezoid()
--]]
function draft:trapezoidIsosceles(cx, cy, width, height, widthTop, mode)
return self:trapezoid(cx, cy, width, height, widthTop, 0, mode)
end
--[[
Draws a parallelogram
@param number center x
@param number center y
@param number width
@param number height
@param number width offset (to the left)
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y2, x2, y2, x3, y3, x4, y4}
@uses self:trapezoid()
--]]
function draft:parallelogram(cx, cy, width, height, widthOffset, mode)
return self:trapezoid(cx, cy, width, height, width, widthOffset, mode)
end
----------------------------------------------------
-- Curved shapes
----------------------------------------------------
--[[
Draws all kinds of curved shapes
Based on code from: http://slabode.exofire.net/circle_draw.shtml
@param number center x
@param number center y
@param number width
@param number arc angle
@param number start angle [def: 0]
@param number number of segments [def: math.floor(10 * math.sqrt(radius))]
@param bool wrap the arc upon itself [def: false]
@param mixed scale (table {x, y} or function (x, y, segmentNum, numSegments)) [def: nil]
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y1, x2, y2, ...}
@uses self:polygon(), self:line()
--]]
function draft:compass(
cx, cy, width, arcAngle, startAngle, numSegments, wrap, scale, mode
)
if mode == nil then mode = self.mode end
local radius = width / 2
startAngle = startAngle or 0
numSegments = numSegments or math.floor(10 * math.sqrt(radius))
if wrap == true then wrap = 0 else wrap = -1 end
local theta = arcAngle / (numSegments - wrap)
local cosine = math.cos(theta)
local sine = math.sin(theta)
local x = radius * math.cos(startAngle)
local y = radius * math.sin(startAngle)
local vertices = {}
for i = 1, numSegments, 1 do
local vx, vy
if type(scale) == 'function' then
vx, vy = scale(x,y,i,numSegments)
elseif type(scale) == 'table' then
vx = x * scale[1]
vy = y * scale[2]
else
vx = x
vy = y
end
table.insert(vertices, vx + cx)
table.insert(vertices, vy + cy)
local t = x
x = (cosine * x) - (sine * y)
y = (sine * t) + (cosine * y)
end
if mode then
if wrap == 0 then
self:polygon(vertices, mode)
else
self:line(vertices, mode)
end
end
return vertices
end
--[[
Draws a circle (regular polygon)
@param number center x
@param number center y
@param number radius
@param number number of segments [def: 10 * math.sqrt(radius)]
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y1, x2, y2, ...}
@uses self:compass()
--]]
function draft:circle(cx, cy, radius, numSegments, mode)
return self:compass(
cx, cy, radius, 2 * math.pi, 0, numSegments, true, nil, mode
)
end
--[[
Draws an arc
@param number center x
@param number center y
@param number radius
@param number arc angle
@param number start angle [def: 0]
@param number number of segments [def: 10 * math.sqrt(radius)]
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y1, x2, y2, ...}
@uses self:compass()
--]]
function draft:arc(cx, cy, radius, arcAngle, startAngle, numSegments, mode)
return self:compass(
cx, cy, radius, arcAngle, startAngle, numSegments, false, nil, mode
)
end
--[[
Draws a bow (closed arc)
@param number center x
@param number center y
@param number radius
@param number arc angle
@param number start angle [def: 0]
@param number number of segments [def: 10 * math.sqrt(radius)]
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y1, x2, y2, ...}
@uses self:compass()
--]]
function draft:bow(cx, cy, radius, arcAngle, startAngle, numSegments, mode)
return self:compass(
cx, cy, radius, arcAngle, startAngle, numSegments, true, nil, mode
)
end
--[[
Draws a pie
@param number center x
@param number center y
@param number radius
@param number arc angle
@param number start angle [def: 0]
@param number number of segments [def: 10 * math.sqrt(radius)]
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y1, x2, y2, ...}
@uses self:compass(), appendVertices(), self:polygon()
--]]
function draft:pie(cx, cy, radius, arcAngle, startAngle, numSegments, mode)
local vertices = self:compass(
cx, cy, radius, arcAngle, startAngle, numSegments, false, nil, false
)
vertices = appendVertices(vertices, {cx, cy})
return self:polygon(vertices, mode)
end
--[[
Draws an ellipse
@param number center x
@param number center y
@param number width
@param number height
@param number number of segments [def: 10 * math.sqrt(radius)]
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y1, x2, y2, ...}
@uses self:compass()
--]]
function draft:ellipse(cx, cy, width, height, numSegments, mode)
return self:compass(
cx, cy, width, 2 * math.pi, 0, numSegments, true, {1, height / width}, mode
)
end
--[[
Draws an elliptic arc
@param number center x
@param number center y
@param number width
@param number height
@param number arc angle
@param number start angle [def: 0]
@param number number of segments [def: 10 * math.sqrt(radius)]
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y1, x2, y2, ...}
@uses self:compass()
--]]
function draft:ellipticArc(cx, cy, width, height, arcAngle, startAngle, numSegments, mode)
return self:compass(
cx, cy, width, arcAngle, startAngle, numSegments, false, {1, height / width}, mode
)
end
--[[
Draws an elliptic bow (closed elliptic arc)
@param number center x
@param number center y
@param number width
@param number height
@param number arc angle
@param number start angle [def: 0]
@param number number of segments [def: 10 * math.sqrt(radius)]
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y1, x2, y2, ...}
@uses self:compass()
--]]
function draft:ellipticBow(cx, cy, width, height, arcAngle, startAngle, numSegments, mode)
return self:compass(
cx, cy, width, arcAngle, startAngle, numSegments, true, {1, height / width}, mode
)
end
--[[
Draws an elliptic pie
@param number center x
@param number center y
@param number width
@param number height
@param number arc angle
@param number start angle [def: 0]
@param number number of segments [def: 10 * math.sqrt(radius)]
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y1, x2, y2, ...}
@uses self:compass(), appendVertices(), self:polygon()
--]]
function draft:ellipticPie(cx, cy, width, height, arcAngle, startAngle, numSegments, mode)
local vertices = self:compass(
cx, cy, width, arcAngle, startAngle, numSegments, false, {1, height / width}, false
)
vertices = appendVertices(vertices, {cx, cy})
return self:polygon(vertices, mode)
end
--[[
Draws a semicircle
@param number center x
@param number center y
@param number width
@param number start angle
@param number number of segments [def: 10 * math.sqrt(radius)]
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y1, x2, y2, ...}
@uses self:compass()
--]]
function draft:semicircle(cx, cy, width, startAngle, numSegments, mode)
return self:compass(
cx, cy, width / 2, math.rad(180), startAngle, numSegments, false, nil, mode
)
end
--[[
Draws a dome (closed semicircle)
@param number center x
@param number center y
@param number width
@param number start angle
@param number number of segments [def: 10 * math.sqrt(radius)]
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y1, x2, y2, ...}
@uses self:compass()
--]]
function draft:dome(cx, cy, width, startAngle, numSegments, mode)
return self:compass(
cx, cy, width / 2, math.rad(180), startAngle, numSegments, true, nil, mode
)
end
----------------------------------------------------
-- Complex shapes
----------------------------------------------------
--[[
Draws a star
@param number center x
@param number center y
@param number width
@param number inner width
@param number number of points [def: 5]
@param number start angle [def: 0]
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y1, x2, y2, ...}
@uses self:compass()
--]]
function draft:star(cx, cy, width, widthInner, numPoints, startAngle, mode)
widthInner = widthInner or width / 2
numPoints = numPoints or 5
local scale = function(cx, cy, segmentNum)
if segmentNum % 2 == 0 then
cx = cx * (widthInner / width)
cy = cy * (widthInner / width)
end
return cx, cy
end
return self:compass(
cx, cy, width, 2 * math.pi, startAngle, numPoints * 2, true, scale, mode
)
end
--[[
Draws an egg
@param number center x
@param number center y
@param number width
@param number scale y bottom [def: 0.8]
@param number scale y top [def: 2]
@param number number of segments [def: math.floor(10 * math.sqrt(radius))]
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table vertices {x1, y1, x2, y2, ...}
@uses self:compass()
--]]
function draft:egg(cx, cy, width, syBottom, syTop, numSegments, mode)
syBottom = syBottom or 0.8
syTop = syTop or 2
local scale = function(cx, cy, segmentNum, numSegments)
if segmentNum <= numSegments / 2 then
cy = cy * syBottom
else
cy = cy * syTop
end
return cx, cy
end
return self:compass(cx, cy, width, 2 * math.pi, 0, numSegments, true, scale, mode)
end
----------------------------------------------------
-- Linkers (draw lines between points)
----------------------------------------------------
--[[
Draws lines between two sets of vertices in ladder style
Ladder style means point A->A, B->B, C->C, etc...
Note: The sets of vertices must be the same size.
@param table vertices 1
@param table vertices 2
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table lines {{x1, y1, x2, y2}, {x1, x2, y1, y2}, …}
@uses self:line()
--]]
function draft:linkLadder(v1, v2, mode)
local lines = {}
for i=1, #v1, 2 do
table.insert(lines, self:line({v1[i], v1[i+1], v2[i], v2[i+1]}, mode))
end
return lines
end
--[[
Draws lines between two sets of vertices in tangle style
Tangle style means each point of one set of vertices
to all the points of the other set of vertices.
@param table vertices 1
@param table vertices 2
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table lines {{x1, y1, x2, y2}, {x1, x2, y1, y2}, …}
@uses self:line()
--]]
function draft:linkTangle(v1, v2, mode)
local lines = {}
for i=1, #v1, 2 do
for j=1, #v2, 2 do
table.insert(lines, self:line({v1[i], v1[i+1], v2[j], v2[j+1]}, mode))
end
end
return lines
end
--[[
Draws lines between vertices in web style
Web style means each point to all other points.
@param table vertices
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table lines {{x1, y1, x2, y2}, {x1, x2, y1, y2}, …}
@uses self:line()
--]]
function draft:linkWeb(v, mode)
local lines = {}
local limit = #v - 2
for i=1, #v-4, 2 do
if i == 3 then limit = limit + 2 end
for j=i+4, limit, 2 do
table.insert(lines, self:line({v[i], v[i+1], v[j], v[j+1]}, mode))
end
end
return lines
end
--[[
Draws a linkTangle and linkWeb between two sets of vertices
@param table vertices 1
@param table vertices 2
@param mixed drawMode (fill, line, false) [def: self.mode]
@return table lines {{x1, y1, x2, y2}, {x1, x2, y1, y2}, …}
@uses self:linkTangle(), self:linkWeb()
--]]
function draft:linkTangleWebs(v1, v2, mode)
local lines = {}
table.insert(lines, self:linkTangle(v1, v2, mode))
table.insert(lines, self:linkWeb(v1, mode))
table.insert(lines, self:linkWeb(v2, mode))
return lines
end
----------------------------------------------------
-- Module
----------------------------------------------------
return setmetatable({new = new},
{__call = function(_, ...) return new(...) end})