-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.moon
More file actions
1211 lines (990 loc) · 35 KB
/
init.moon
File metadata and controls
1211 lines (990 loc) · 35 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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- @module MoonMath
type = type
table = table
unpack = table.unpack or unpack
-- @local
-- handles variable args funcs, assembles all ars in a table if not already
checkInput = (...) ->
input = {}
if type(...) ~= 'table' then input = {...} else input = ...
input
-- @local
-- verify false false values. (Significant figures)
checkFuzzy = (n1, n2) ->
(n1 - .00001 <= n2 and n2 <= n1 + .00001)
--- Remove multiple occurrences of pairs { {x, y}, {x, y} }.
-- @tparam table t
-- @treturn table newt
removeDuplicatePairs = (t) ->
for i = #t, 1, -1
first = t[i]
for j = #t, 1, -1
second = t[j]
if i ~= j
if type(first[1]) == 'number' and type(second[1]) == 'number' and type(first[2]) == 'number' and type(second[2]) == 'number'
if checkFuzzy(first[1], second[1]) and checkFuzzy(first[2], second[2])
table.remove t, i
elseif first[1] == second[1] and first[2] == second[2]
table.remove t, i
t
--- Remove multiple occurrences of triplets.
-- @tparam table t
-- @treturn table newt
removeDuplicateTriplets = (t) ->
for i = #t, 1, -1
first = t[i]
for j = #t, 1, -1
second = t[j]
if i ~= j
if type(first[1]) == 'number' and type(second[1]) == 'number' and
type(first[2]) == 'number' and type(second[2]) == 'number' and
type(first[3]) == 'number' and type(second[3]) == 'number'
if checkFuzzy(first[1], second[1]) and
checkFuzzy(first[2], second[2]) and
checkFuzzy(first[3], second[3])
table.remove t, i
elseif checkFuzzy(first[1], second[1]) and
checkFuzzy(first[2], second[2]) and
checkFuzzy(first[3], second[3])
table.remove t, i
t
removeDuplicates4Points = (t) ->
for i = #t, 1, -1
first = t[i]
for j = #t, 1, -1
second = t[j]
if i ~= j
if type(first[1]) ~= type(second[1]) then return false
if type(first[2]) == 'number' and type(second[2]) == 'number' and type( first[3] ) == 'number' and type( second[3] ) == 'number'
if checkFuzzy(first[2], second[2]) and
checkFuzzy(first[3], second[3])
table.remove t, i
elseif checkFuzzy(first[1], second[1]) and
checkFuzzy(first[2], second[2]) and
checkFuzzy(first[3], second[3])
table.remove t, i
t
--- Adds a point to a table.
-- @tparam table t
-- @tparam number x
-- @tparam number y
-- @treturn table t
addPoint = (t, x, y) ->
t[#t + 1] = x
t[#t + 1] = y
--- Remove duplicates from a flat table.
-- @tparam table t
-- @treturn table newt
-- Byg Fixed see issue : https://github.com/davisdude/mlib/issues/17
removeDuplicatePointsFlat = (t) ->
for i = #t, 1, -2
for ii = #t - 2, 3, -2
if i ~= ii
x1, y1 = t[i], t[i + 1]
x2, y2 = t[ii], t[ii + 1]
if checkFuzzy(x1, x2) and checkFuzzy(y1, y2)
table.remove t, ii
table.remove t, ii + 1
t
--- checks if a number is valid
-- @tparam number n
-- @treturn bool validity
validateNumber = (n) ->
math = math
if type(n) ~= 'number' then return false
elseif n ~= n then return false
elseif math.abs(n) == math.huge then return false
else return true
-- TODO
cycle = (t, i) -> t[(i - 1) % #t + 1]
--- Gets the greatest/least points with an offset
-- @tparam table points
-- @tparam number offset
-- @treturn table {greatest, least}
getGreatestPoint = (points, offset = 1) ->
start = 2 - offset
greatest = points[start]
least = points[start]
for i = 2, #points / 2
i = i * 2 - offset
if points[i] > greatest then greatest = points[i]
if points[i] < least then least = points[i]
greatest, least
--- Checks if a number is within given bounds
-- @tparam number min
-- @tparam number n
-- @tparam number max
-- @treturn bool
isWithinBounds = (min, n, max) ->
n >= min and n <= max
--- calculates the distance between two points
-- @tparam number x1
-- @tparam number y1
-- @tparam number x2
-- @tparam number y2
-- @treturn number
distance2 = (x1, y1, x2, y2) ->
dx, dy = x1 - x2, y1 - y2
dx * dx + dy * dy
--- Math
--- gets the quadratic roots of an equation
-- @tparam number a
-- @tparam number b
-- @tparam number c
-- @tparam number table
getQuadraticRoots = (a, b, c) ->
math = math
discriminant = b ^ 2 - (4 * a * c)
if discriminant < 0 then return false
discriminant = math.sqrt discriminant
denominator = 2 * a
(-b - discriminant)/denominator, (-b + discriminant)/denominator
--- Points
--- Rotate a point [tested]
-- @tparam number x
-- @tparam number y
-- @tparam number rot
-- @tparam number ox
-- @tparam number oy
-- @treturn table {x, y}
rotatePoint = (x, y, rot, ox = 0, oy = 0) ->
math = math
(x - ox) * math.cos(rot) + ox - (y - oy) * math.sin(rot), (x - ox) * math.sin(rot) + (y - oy) * math.cos(rot) + oy
--- scales a point [tested]
-- @tparam number x
-- @tparam number y
-- @tparam number scale
-- @tparam number ox
-- @tparam number oy
-- @treturn table {x, y}
scalePoint = (x, y, scale, ox = 0, oy = 0) ->
(x - ox) * scale + ox, (y - oy) * scale + oy
--- From polar to cartesian [tested]
-- @tparam number radius
-- @tparam number theta
-- @tparam number offsetRadius
-- @tparam number offsetTheta
-- @treturn table {x, y}
polarToCartesian = (radius, theta, offsetRadius, offsetTheta) ->
math = math
ox, oy = 0, 0
if offsetRadius and offsetTheta
ox, oy = unpack polarToCartesian(offsetRadius, offsetTheta)
x = radius * math.cos(theta)
y = radius * math.sin(theta)
{x + ox, y + oy}
--- From cartesian to polar [tested]
-- @tparam number x
-- @tparam number y
-- @tparam number ox
-- @tparam number oy
-- @treturn table {radius, theta}
cartesianToPolar = (x, y, ox, oy) ->
math = math
x, y = x - (ox or 0), y - (oy or 0)
theta = math.atan2(y, x)
theta = theta > 0 and theta or theta + 2 * math.pi
radius = math.sqrt(x ^ 2 + y ^ 2)
{radius, theta}
--- Lines
--- Gets the leght of a line
-- @tparam number x1
-- @tparam number y1
-- @tparam number x2
-- @tparam number y2
-- @treturn number
getLength = (x1, y1, x2, y2) ->
math = math
dx, dy = x1 - x2, y1 - y2
math.sqrt dx * dx + dy * dy
--- gets the midpoint of a line
-- @tparam number x1
-- @tparam number y1
-- @tparam number x2
-- @tparam number y2
-- @treturn number
getMidPoint = (x1, y1, x2, y2) ->
{(x1 + x2) / 2, (y1 + y2) / 2}
--- gets the slope of a line
-- @tparam number x1
-- @tparam number y1
-- @tparam number x2
-- @tparam number y2
-- @treturn number
getSlope = (x1, y1, x2, y2) ->
if checkFuzzy(x1, x2) then return false
(y1 - y2) / (x1 - x2)
--- gives the perpendicular slope of a line
-- @tparam table input {x1, y1, x2, y2} or slope
-- @treturn slope
getPerpendicularSlope = (...) ->
inpt = checkInput ...
local slope
if #inpt ~= 1
slope = getSlope unpack(inpt)
else
slope = unpack(inpt)
if slope == false then return 0
elseif checkFuzzy(slope, 0) then return false
else return -1 / slope
--- gets the y-intercept of a line
-- @tparam number x
-- @tparam number y
-- @tparam table ... {x2, y2} or slope
-- @treturn table {number, bool}
getYIntercept = (x, y, ...) ->
inpt = checkInput ...
local slope
if #inpt == 1
slope = inpt[1]
else
slope = getSlope(x, y, unpack(inpt))
if slope == false then return {x, true}
{y - slope * x, false}
--- gets the intersection of two lines
-- @tparam table ... {slope1, slope2, x1, y1, x2, y2} or {slope1, intercept1, slope2, intercept2} or {x1, y1, x2, y2, x3, y3, x4, y4}
-- @treturn table {x, y}
getLineLineIntersection = (...) ->
-- DEBUG LAST
inpt = checkInput ...
local x, y, x1, y1, x2, y2, x3, y3, x4, y4
local slope1, intercept1
local slope2, intercept2
if #inpt == 4
slope1, intercept1, slope2, intercept2 = unpack inpt
y1 = slope1 and slope1 * 1 + intercept1 or 1
y2 = slope1 and slope1 * 2 + intercept1 or 2
y3 = slope2 and slope2 * 1 + intercept2 or 1
y4 = slope2 and slope2 * 2 + intercept2 or 2
x1 = slope1 and ( y1 - intercept1 ) / slope1 or intercept1
x2 = slope1 and ( y2 - intercept1 ) / slope1 or intercept1
x3 = slope2 and ( y3 - intercept2 ) / slope2 or intercept2
x4 = slope2 and ( y4 - intercept2 ) / slope2 or intercept2
elseif #inpt == 6
slope1, intercept1 = inpt[1], inpt[2]
slope2 = getSlope inpt[3], inpt[4], inpt[5], inpt[6]
intercept2 = unpack getYIntercept(inpt[3], inpt[4], inpt[5], inpt[6])
y1 = slope1 and slope1 * 1 +intercept1 or 1
y2 = slope1 and slope1 * 2 +intercept1 or 2
y3 = inpt[4]
y4 = inpt[6]
x1 = slope1 and (y1 - intercept1) / slope1 or intercept1
x2 = slope1 and (y2 - intercept1) / slope1 or intercept1
x3 = inpt[3]
x4 = inpt[5]
elseif #inpt == 8
slope1 = getSlope inpt[1], inpt[2], inpt[3], inpt[4]
intercept1 = unpack getYIntercept(inpt[1], inpt[2], inpt[3], inpt[4])
slope2 = getSlope inpt[5], inpt[6], inpt[7], inpt[8]
intercept2 = unpack getYIntercept(inpt[5], inpt[6], inpt[7], inpt[8])
x1, y1, x2, y2, x3, y3, x4, y4 = unpack inpt
if slope1 == false and slope2 == false -- Both are vertical
if x1 == x3 then return true
else return false
elseif slope1 == false
x = x1
y = slope2 and slope2 * x + intercept2 or 1
elseif slope2 == false
x = x3
y = slope1 * x + intercept1
elseif checkFuzzy(slope1, slope2)
if checkFuzzy(intercept1, intercept2) then return true
else return false
else
x = (-intercept1 + intercept2) / (slope1 - slope2)
y = slope1 * x + intercept1
{x, y}
--- gets the closest point on a line to a point
-- @tparam table ... {perpendicularX, perpendicularY, x1, y1, x2, y2} or {perpendicularX, perpendicularY, slope, intercept}
-- @treturn table {x, y}
getClosestPoint = (perpendicularX, perpendicularY, ...) ->
inpt = checkInput ...
local x, y, x1, y1, x2, y2, slope, intercept
if #inpt == 4
x1, y1, x2, y2 = unpack inpt
slope = getSlope x1, y1, x2, y2
intercept = unpack getYIntercept(x1, y1, x2, y2)
elseif #inpt == 2
slope, intercept = unpack inpt
x1, y1 = 1, slope and slope * 1 + intercept or 1
if slope == false
x, y = x1, perpendicularY
elseif checkFuzzy(slope, 0)
x, y = perpendicularX, y1
else
perpendicularSlope = getPerpendicularSlope slope
perpendicularIntercept = unpack getYIntercept(perpendicularX, perpendicularY, perpendicularSlope)
x, y = unpack getLineLineIntersection(slope, intercept, perpendicularSlope, perpendicularIntercept)
{x, y}
--- gets the intersection of a line and a line segment
-- @tparam table ... {x1, y1, x2, y2, x3, y3, x4, y4} or {x1, y1, x2, y2, slope, intercept}
-- @treturn table {x, y}
getLineSegmentIntersection = (x1, y1, x2, y2, ...) ->
inpt = checkInput ...
local slope1, intercept1, x, y, lineX1, lineY1, lineX2, lineY2
slope2 = getSlope(x1, y1, x2, y2)
intercept2 = unpack getYIntercept(x1, y1, x2, y2)
if #inpt == 2
slope1, intercept1 = inpt[1], inpt[2]
lineX1, lineY1 = 1, slope1 and slope1 + intercept1
lineX2, lineY2 = 2, slope1 and slope1 * 2 + intercept1
else
lineX1, lineY1, lineX2, lineY2 = unpack(inpt)
slope1 = getSlope(unpack(inpt))
intercept1 = unpack getYIntercept(unpack(inpt))
if slope1 == false and slope2 == false
if checkFuzzy(x1, lineX1)
return {x1, y1, x2, y2}
else
return false
elseif slope1 == false
x, y = inpt[1], (slope2 * inpt[1] + intercept2)
elseif slope2 == false
x, y = x1, (slope1 * x1 + intercept1)
else
r = getLineLineIntersection(slope1, intercept1, slope2, intercept2)
if type(r) == "boolean"
x = r
else
x, y = unpack r
local length1, length2, distance
if x == true
return {x1, y1, x2, y2}
elseif x
length1, length2 = getLength( x1, y1, x, y ), getLength( x2, y2, x, y )
distance = getLength x1, y1, x2, y2
else
if checkFuzzy intercept1, intercept2
return {x1, y1, x2, y2}
else
return false
if length1 <= distance and length2 <= distance then return {x, y} else return false
--- checks if a point is on a line
-- @tparam number x
-- @tparam number y
-- @tparam number x1
-- @tparam number y1
-- @tparam number x2
-- @tparam number y2
-- @treturn bool
checkLinePoint = (x, y, x1, y1, x2, y2) ->
m = getSlope x1, y1, x2, y2
b = unpack getYIntercept(x1, y1, m)
if m == false then return checkFuzzy x, x1
checkFuzzy y, m * x + b
--- gets the intersection of a line and a circle
-- @tparam number circleX
-- @tparam number circleY
-- @tparam number radius
-- @tparam number x1
-- @tparam number y1
-- @tparam number x2
-- @tparam number y2
-- @treturn table { ratio, ... }
getCircleLineIntersection = (circleX, circleY, radius, x1, y1, x2, y2) ->
slope = getSlope(x1, y1, x2, y2)
intercept = unpack getYIntercept(x1, y1, slope)
if slope
a = (1 + slope ^ 2)
b = (-2 * (circleX) + (2 * slope * intercept) - (2 * circleY * slope))
c = (circleX ^ 2 + intercept ^ 2 - 2 * (circleY) * (intercept) + circleY ^ 2 - radius ^ 2)
x1, x2 = getQuadraticRoots(a, b, c)
if x1 == false then return x1
y1 = slope * x1 + intercept
y2 = slope * x2 + intercept
if checkFuzzy(x1, x2) and checkFuzzy(y1, y2)
return {'tangent', x1, y1}
else
return {'secant', x1, y1, x2, y2}
else
local intercept
lengthToPoint1 = circleX - x1
intercept = math.sqrt(-(lengthToPoint1 ^ 2 - radius ^ 2))
if -(lengthToPoint1 ^ 2 - radius ^ 2) < 0 then return false
bottomX, bottomY = x1, circleY - intercept
topX, topY = x1, circleY + intercept
if topY ~= bottomY
return {'secant', topX, topY, bottomX, bottomY}
else
return {'tangent', topX, topY}
--- checks whether or not a line intersects a polygon
-- @tparam number x1
-- @tparam number y1
-- @tparam number x2
-- @tparam number y2
-- @tparam number ...
-- @treturn bool
getPolygonLineIntersection = (x1, y1, x2, y2, ...) ->
input = checkInput ...
choices = {}
slope = getSlope x1, y1, x2, y2
intercept = unpack getYIntercept(x1, y1, slope)
local x3, y3, x4, y4
if slope
x3, x4 = 1, 2
y3, y4 = slope * x3 + intercept, slope * x4 + intercept
else
x3, x4 = x1, x1
y3, y4 = y1, y2
for i = 1, #input, 2
r = getLineSegmentIntersection(input[i], input[i + 1], cycle(input, i + 2), cycle(input, i + 3), x3, y3, x4, y4)
if type(r) == "boolean"
x1 = r
else
x1, y1, x2, y2 = unpack r
if x1 and not x2 then choices[#choices + 1] = {x1, y1}
elseif x1 and x2 then choices[#choices + 1] = {x1, y1, x2, y2}
final = removeDuplicatePairs choices
#final > 0 and final or false
--- gets the perpendicular bisector of a line (https://www.youtube.com/watch?v=A86COO8KC58)
-- @tparam number x1
-- @tparam number y1
-- @tparam number x2
-- @tparam number y2
-- @treturn midx, midy, perpendicularSlope
getPerpendicularBisector = (x1, y1, x2, y2) ->
slope = getSlope x1, y1, x2, y2
midx, midy = getMidPoint x1, y1, x2, y2
midx, midy, getPerpendicularSlope(slope)
--- checks hether or not a point lies on a line segment
-- @tparam number px
-- @tparam number py
-- @tparam number x1
-- @tparam number y1
-- @tparam number x2
-- @tparam number y2
-- @treturn bool
checkSegmentPoint = ( px, py, x1, y1, x2, y2 ) ->
x = checkLinePoint px, py, x1, y1, x2, y2
if x == false then return false
lenghtX = x2 - x1
lenghtY = y2 - y1
if checkFuzzy(lenghtX ,0)
if checkFuzzy(px ,x1)
local low, high
if y1 > y2
low = y2
high = y1
else
low = y1
high = y2
if py >= low and py <= high then return true
else return false
else
return false
elseif checkFuzzy(lenghtY, 0)
if checkFuzzy(py, y1)
local low, high
if x1 > x2
low = x2
high = x1
else
low = x1
high = x2
if px >= low and px <= high then return true
else return false
else
return false
distToPx = px - x1
distToPy = py - y1
scaleX = distToPx / lenghtX
scaleY = distToPy / lenghtY
if ( scaleX >= 0 and scaleX <= 1 ) and ( scaleY >= 0 and scaleY <= 1 )
return true
return false
--- adds point to a table
-- @tparam table t
-- @tparam number x
-- @tparam number y
addPoints = (t, x, y) ->
t[#t + 1] = x
t[#t + 1] = y
--- gets the point of intersection between two line segments
-- @tparam number x1
-- @tparam number y1
-- @tparam number x2
-- @tparam number y2
-- @tparam number x3
-- @tparam number y3
-- @tparam number x4
-- @tparam number y4
-- @treturn bool
getSegmentSegmentIntersection = (x1, y1, x2, y2, x3, y3, x4, y4 ) ->
slope1, intercept1 = getSlope(x1, y1, x2, y2), unpack getYIntercept(x1, y1, x2, y2)
slope2, intercept2 = getSlope(x3, y3, x4, y4), unpack getYIntercept(x3, y3, x4, y4)
if ((slope1 and slope2) and checkFuzzy(slope1, slope2)) or (slope1 == false and slope2 == false)
if checkFuzzy(intercept1, intercept2)
points = {}
if checkSegmentPoint(x1, y1, x3, y3, x4, y4) then addPoints( points, x1, y1 )
if checkSegmentPoint(x2, y2, x3, y3, x4, y4) then addPoints( points, x2, y2 )
if checkSegmentPoint(x3, y3, x1, y1, x2, y2) then addPoints( points, x3, y3 )
if checkSegmentPoint(x4, y4, x1, y1, x2, y2) then addPoints( points, x4, y4 )
points = removeDuplicatePointsFlat(points)
if #points == 0 then return false
return points
else
return false
x, y = unpack getLineLineIntersection x1, y1, x2, y2, x3, y3, x4, y4
if x and checkSegmentPoint(x, y, x1, y1, x2, y2) and checkSegmentPoint(x, y, x3, y3, x4, y4)
return {x, y}
false
--- checks if a point is on a circle
-- @tparam number x
-- @tparam number y
-- @tparam number circleX
-- @tparam number circleY
-- @tparam number radius
-- @treturn bool
isPointOnCircle = (x, y, circleX, circleY, radius) ->
checkFuzzy getLength(circleX, circleY, x, y), radius
--- checks if a point is within the radius of a circle
-- @tparam number x
-- @tparam number y
-- @tparam number circleX
-- @tparam number circleY
-- @tparam number radius
-- @treturn bool
checkCirclePoint = (x, y, circleX, circleY, radius) ->
getLength(circleX, circleY, x, y) <= radius
--- gets the type of intersection of a line segment
-- @tparam number circleX
-- @tparam number circleY
-- @tparam number radius
-- @tparam number x1
-- @tparam number y1
-- @tparam number x2
-- @tparam number y2
-- @treturn table {circularSeg, ...}
getCircleSegmentIntersection = (circleX, circleY, radius, x1, y1, x2, y2) ->
cIntersection = getCircleLineIntersection(circleX, circleY, radius, x1, y1, x2, y2)
local Type, x3, y3, x4, y4
if type(cIntersection) == 'table'
Type, x3, y3, x4, y4 = unpack cIntersection
elseif type(cIntersection) == 'boolean'
Type = cIntersection
if Type == false then return false
slope, intercept = getSlope(x1, y1, x2, y2), unpack getYIntercept(x1, y1, x2, y2)
if isPointOnCircle(x1, y1, circleX, circleY, radius) and
isPointOnCircle(x2, y2, circleX, circleY, radius)
return {'chord', x1, y1, x2, y2}
if slope
if checkCirclePoint(x1, y1, circleX, circleY, radius) and
checkCirclePoint(x2, y2, circleX, circleY, radius)
return {'enclosed', x1, y1, x2, y2}
elseif x3 and x4
if checkSegmentPoint(x3, y3, x1, y1, x2, y2) and
checkSegmentPoint(x4, y4, x1, y1, x2, y2) == false
return {'tangent', x3, y3}
elseif checkSegmentPoint(x4, y4, x1, y1, x2, y2) and
checkSegmentPoint(x3, y3, x1, y1, x2, y2) == false
return {'tangent', x4, y4}
else
if checkSegmentPoint(x3, y3, x1, y1, x2, y2) and
checkSegmentPoint(x4, y4, x1, y1, x2, y2)
return {'secant', x3, y3, x4, y4}
else
return false
elseif x4 == nil
if checkSegmentPoint(x3, y3, x1, y1, x2, y2)
return {'tangent', x3, y3}
else
local length, distance1, distance2
length = getLength(x1, y1, x2, y2)
distance1 = getLength(x1, y1, x3, y3)
distance2 = getLength(x2, y2, x3, y3)
if length > distance1 or length > distance2 then return false
elseif length < distance1 and length < distance2 then return false
else return {'tangent', x3, y3}
else
math = math
lengthToPoint1 = circleX - x1
remainingDistance = lengthToPoint1 - radius
intercept = math.sqrt(-(lengthToPoint1 ^ 2 - radius ^ 2))
if -(lengthToPoint1 ^ 2 - radius ^ 2) < 0 then return false
topX, topY = x1, circleY - intercept
bottomX, bottomY = x1, circleY + intercept
length = getLength(x1, y1, x2, y2)
distance1 = getLength(x1, y1, topX, topY)
if bottomY ~= topY
if checkSegmentPoint(topX, topY, x1, y1, x2, y2) and
checkSegmentPoint(bottomX, bottomY, x1, y1, x2, y2)
return {'chord', topX, topY, bottomX, bottomY}
elseif checkSegmentPoint(topX, topY, x1, y1, x2, y2)
return {'tangent', topX, topY}
elseif checkSegmentPoint(bottomX, bottomY, x1, y1, x2, y2)
return {'tangent', bottomX, bottomY}
else return false
else
if checkSegmentPoint(topX, topY, x1, y1, x2, y2)
return {'tangent', topX, topY}
else
return false
--- checks if the line segment intersects the polygon
-- @tparam number x1
-- @tparam number y1
-- @tparam number x2
-- @tparam number y2
-- @tparam number ...
-- @treturn bool
getPolygonSegmentIntersection = (x1, y1, x2, y2, ...) ->
input = checkInput ...
choices = {}
for i = 1, #input, 2
local _x1, _y1, _x2, _y2
ssIntersection = getSegmentSegmentIntersection(input[i], input[i + 1], cycle(input, i + 2), cycle(input, i + 3), x1, y1, x2, y2)
if type(ssIntersection) == 'table'
_x1, _y1, _x2, _y2 = unpack ssIntersection
elseif type(ssIntersection) == 'boolean'
_x1 = ssIntersection
if _x1 and _x2 == nil then choices[#choices + 1] = {_x1, _y1}
elseif _x2 then choices[#choices + 1] = {_x1, _y1, _x2, _y2}
final = removeDuplicatePairs choices
#final > 0 and final or false
--- checks if a line-segment is entirely within a circle
-- @tparam number circleX
-- @tparam number circleY
-- @tparam number circleRadius
-- @tparam number x1
-- @tparam number y1
-- @tparam number x2
-- @tparam number y2
-- @treturn bool
isSegmentCompletelyInsideCircle = (circleX, circleY, circleRadius, x1, y1, x2, y2) ->
sIntersection = getCircleSegmentIntersection(circleX, circleY, circleRadius, x1, y1, x2, y2)
Type = unpack(sIntersection) if type(sIntersection) == 'table' else sIntersection
return Type == 'enclosed'
--- checks if the point lies inside the polygon not on the polygon
-- @tparam number px
-- @tparam number py
-- @tparam number ...
-- @treturn bool
checkPolygonPoint = (px, py, ...) ->
points = {unpack(checkInput(...))}
greatest, least = getGreatestPoint(points, 0)
if isWithinBounds(least, py, greatest) == false then return false
greatest, least = getGreatestPoint points
if isWithinBounds(least, px, greatest) == false then return false
count = 0
for i = 1, #points, 2
if checkFuzzy(points[i + 1], py)
points[i + 1] = py + .001
if points[i + 3] and checkFuzzy(points[i + 3], py)
points[i + 3] = py + .001
x1, y1 = points[i], points[i + 1]
x2, y2 = points[i + 2] or points[1], points[i + 3] or points[2]
if getSegmentSegmentIntersection(px, py, greatest, py, x1, y1, x2, y2)
count = count + 1
count and count % 2 ~= 0
--- checks if a segment is completely inside a polygon
-- @tparam number x1
-- @tparam number y1
-- @tparam number x2
-- @tparam number y2
-- @tparam table ...
-- @treturn bool
isSegmentCompletelyInsidePolygon = (x1, y1, x2, y2, ...) ->
polygon = {unpack(checkInput(...))}
if checkPolygonPoint(x1, y1, polygon) == false or
checkPolygonPoint( x2, y2, polygon ) == false or
getPolygonSegmentIntersection(x1, y1, x2, y2, polygon)
return false
true
--- gets the intersection of a line and a line segment
-- @tparam table ... {x1, y1, x2, y2, x3, y3, x4, y4} or {x1, y1, x2, y2, slope, intercept}
-- @treturn table {x, y}
getLineSegmentIntersection = (x1, y1, x2, y2, ...) ->
inpt = {unpack(checkInput(...))}
local slope1, intercept1, x, y, lineX1, lineY1, lineX2, lineY2
slope2 = getSlope(x1, y1, x2, y2)
intercept2 = unpack getYIntercept(x1, y1, x2, y2)
if #inpt == 2
slope1, intercept1 = inpt[1], inpt[2]
lineX1, lineY1 = 1, slope1 and slope1 + intercept1
lineX2, lineY2 = 2, slope1 and slope1 * 2 + intercept1
else
lineX1, lineY1, lineX2, lineY2 = unpack(inpt)
slope1 = getSlope(unpack(inpt))
intercept1 = unpack getYIntercept(unpack(inpt))
if slope1 == false and slope2 == false
if checkFuzzy(x1, lineX1)
return {x1, y1, x2, y2}
else
return false
elseif slope1 == false
x, y = inpt[1], (slope2 * inpt[1] + intercept2)
elseif slope2 == false
x, y = x1, (slope1 * x1 + intercept1)
else
r = getLineLineIntersection(slope1, intercept1, slope2, intercept2)
if type(r) == "boolean"
x = r
else
x, y = unpack r
local length1, length2, distance
if x == true
return {x1, y1, x2, y2}
elseif x
length1, length2 = getLength( x1, y1, x, y ), getLength( x2, y2, x, y )
distance = getLength x1, y1, x2, y2
else
if checkFuzzy intercept1, intercept2
return {x1, y1, x2, y2}
else
return false
if length1 <= distance and length2 <= distance then return {x, y} else return false
getRoot = (number, root) ->
number ^ ( 1 / root )
isPrime = (n) ->
math = math
if n < 2 then return false
for i = 2, math.sqrt(n)
if n % i == 0
return false
true
round = (number, decimals = 0) ->
math = math
pow = 10 ^ decimals
math.floor(number * pow + .5) / pow
getSummation = (start, stop, func) ->
rValues = {}
sum = 0
for i = start, stop
val = func i, rValues
rValues[i] = val
sum += val
sum
getPercentOfChange = (old, new) ->
math = math
if old == 0 and new == 0 then return 0
else return (new - old) / math.abs(old)
getPercenrage = (percent, number) ->
percent * number
getAngle = (x1, y1, x2, y2, x3, y3) ->
a = getLength x3, y3, x2, y2
b = getLength x1, y1, x2, y2
c = getLength x1, y1, x3, y3
math.acos (a * a + b * b - c * c) / (2 * a * b)
getPercentage = (percent, n) ->
percent * n
getCircleArea = (radius) ->
math = math
math.pi * (radius * radius)
getCircumference = (radius) ->
math = math
2 * math.pi * radius
getCircleCircleIntersection = (circle1x, circle1y, radius1, circle2x, circle2y, radius2) ->
length = getLength(circle1x, circle1y, circle2x, circle2y)
if length > radius1 + radius2 then return false
if checkFuzzy(length, 0) and checkFuzzy(radius1, radius2) then return 'equal'
if checkFuzzy(circle1x, circle2x) and checkFuzzy(circle1y, circle2y) then return 'collinear'
a = (radius1 * radius1 - radius2 * radius2 + length * length) / (2 * length)
h = math.sqrt(radius1 * radius1 - a * a)
p2x = circle1x + a * (circle2x - circle1x) / length
p2y = circle1y + a * (circle2y - circle1y) / length
p3x = p2x + h * (circle2y - circle1y) / length
p3y = p2y - h * (circle2x - circle1x) / length
p4x = p2x - h * (circle2y - circle1y) / length
p4y = p2y + h * (circle2x - circle1x) / length
if validateNumber(p3x) == false or validateNumber(p3y) == false or
validateNumber(p4x) == false or
validateNumber( p4y ) == false
return 'inside'
if checkFuzzy(length, radius1 + radius2) or
checkFuzzy(length, math.abs(radius1 - radius2))
return 'tangent', p3x, p3y
return 'intersection', p3x, p3y, p4x, p4y
isCircleCompletelyInsideCircle = (circle1x, circle1y, circle1radius, circle2x, circle2y, circle2radius) ->
if checkCirclePoint(circle1x, circle1y, circle2x, circle2y, circle2radius) == false then return false
Type = getCircleCircleIntersection(circle2x, circle2y, circle2radius, circle1x, circle1y, circle1radius)
if (Type ~= 'tangent' and Type ~= 'collinear' and Type ~= 'inside') then return false
return true
-- POLY
getSignedPolygonArea = (...) ->
points = checkInput(...)
points[#points + 1] = points[1]
points[#points + 1] = points[2]
return (.5 * getSummation( 1, #points / 2,
(index) ->
index = index * 2 - 1
return ((points[index] * cycle(points, index + 3 )) - (cycle(points, index + 2) * points[index + 1]))
))
getPolygonArea = (...) ->
math = math
inp = {...}
math.abs(getSignedPolygonArea( ... ))
getTriangleHeight = (base, ...) ->
input = {unpack(checkInput(...))}
local area
if #input == 1
area = input[1]
else
area = getPolygonArea input