-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathTools.hx
758 lines (699 loc) · 24.4 KB
/
Tools.hx
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
package tweenxcore;
import tweenxcore.geom.Matrix;
import tweenxcore.geom.Point;
import tweenxcore.structure.BoundaryMode;
using tweenxcore.Tools;
/**
* It has easing functions used as a curve of motion
*/
class Easing {
static inline var PI = 3.1415926535897932384626433832795;
static inline var PI_H = PI / 2;
static inline var LN_2 = 0.6931471805599453;
static inline var LN_2_10 = 6.931471805599453;
/*
* LINEAR
*/
/** 1-order */
public static inline function linear(t:Float):Float{
return t;
}
/*
* SINE
*/
public static inline function sineIn(t:Float):Float {
return if (t == 0) {
0;
} else if (t == 1) {
1;
} else {
1 - Math.cos(t * PI_H);
}
}
public static inline function sineOut(t:Float):Float {
return if (t == 0) {
0;
} else if (t == 1) {
1;
} else {
Math.sin(t * PI_H);
}
}
public static inline function sineInOut(t:Float):Float {
return if (t == 0) {
0;
} else if (t == 1) {
1;
} else {
-0.5 * (Math.cos(PI * t) - 1);
}
}
public static inline function sineOutIn(t:Float):Float {
return if (t == 0) {
0;
} else if (t == 1) {
1;
} else if (t < 0.5) {
0.5 * Math.sin((t * 2) * PI_H);
} else {
-0.5 * Math.cos((t * 2 - 1) * PI_H) + 1;
}
}
/*
* QUAD EASING
*/
/** 2-order */
public static inline inline function quadIn(t:Float):Float {
return t * t;
}
/** 2-order */
public static inline inline function quadOut(t:Float):Float {
return -t * (t - 2);
}
/** 2-order */
public static inline inline function quadInOut(t:Float):Float {
return (t < 0.5) ? 2 * t * t : -2 * ((t -= 1) * t) + 1;
}
/** 2-order */
public static inline function quadOutIn(t:Float):Float {
return (t < 0.5) ? -0.5 * (t = (t * 2)) * (t - 2) : 0.5 * (t = (t * 2 - 1)) * t + 0.5;
}
/*
* CUBIC EASING
*/
/** 3-order */
public static inline function cubicIn(t:Float):Float {
return t * t * t;
}
/** 3-order */
public static inline function cubicOut(t:Float):Float {
return (t = t - 1) * t * t + 1;
}
/** 3-order */
public static inline function cubicInOut(t:Float):Float {
return ((t *= 2) < 1) ?
0.5 * t * t * t :
0.5 * ((t -= 2) * t * t + 2);
}
/** 3-order */
public static inline function cubicOutIn(t:Float):Float {
return 0.5 * ((t = t * 2 - 1) * t * t + 1);
}
/*
* QUART EASING
*/
/** 4-order */
public static inline function quartIn(t:Float):Float {
return (t *= t) * t;
}
/** 4-order */
public static inline function quartOut(t:Float):Float {
return 1 - (t = (t = t - 1) * t) * t;
}
/** 4-order */
public static inline function quartInOut(t:Float):Float {
return ((t *= 2) < 1) ? 0.5 * (t *= t) * t : -0.5 * ((t = (t -= 2) * t) * t - 2);
}
/** 4-order */
public static inline function quartOutIn(t:Float):Float {
return (t < 0.5) ? -0.5 * (t = (t = t * 2 - 1) * t) * t + 0.5 : 0.5 * (t = (t = t * 2 - 1) * t) * t + 0.5;
}
/*
* QUINT EASING
*/
/** 5-order */
public static inline function quintIn(t:Float):Float {
return t * (t *= t) * t;
}
/** 5-order */
public static inline function quintOut(t:Float):Float {
return (t = t - 1) * (t *= t) * t + 1;
}
/** 5-order */
public static inline function quintInOut(t:Float):Float {
return ((t *= 2) < 1) ? 0.5 * t * (t *= t) * t : 0.5 * (t -= 2) * (t *= t) * t + 1;
}
/** 5-order */
public static inline function quintOutIn(t:Float):Float {
return 0.5 * ((t = t * 2 - 1) * (t *= t) * t + 1);
}
/*
* EXPO EASING
*/
public static inline function expoIn(t:Float):Float {
return t == 0 ? 0 : Math.exp(LN_2_10 * (t - 1));
}
public static inline function expoOut(t:Float):Float {
return t == 1 ? 1 : (1 - Math.exp(-LN_2_10 * t));
}
public static inline function expoInOut(t:Float):Float {
return if (t == 0) {
0;
} else if (t == 1) {
1;
} else if ((t *= 2) < 1) {
0.5 * Math.exp(LN_2_10 * (t - 1));
} else {
0.5 * (2 - Math.exp(-LN_2_10 * (t - 1)));
}
}
public static inline function expoOutIn(t:Float):Float {
return if (t < 0.5) {
0.5 * (1 - Math.exp(-20 * LN_2 * t));
} else if (t == 0.5) {
0.5;
} else {
0.5 * (Math.exp(20 * LN_2 * (t - 1)) + 1);
}
}
/*
* CIRC EASING
*/
public static inline function circIn(t:Float):Float {
return if (t < -1 || 1 < t) 0 else 1 - Math.sqrt(1 - t * t);
}
public static inline function circOut(t:Float):Float {
return if (t < 0 || 2 < t) 0 else Math.sqrt(t * (2 - t));
}
public static inline function circInOut(t:Float):Float {
return if (t < -0.5 || 1.5 < t) 0.5 else if ((t *= 2) < 1)- 0.5 * (Math.sqrt(1 - t * t) - 1) else 0.5 * (Math.sqrt(1 - (t -= 2) * t) + 1);
}
public static inline function circOutIn(t:Float):Float {
return if (t < 0) 0 else if (1 < t) 1 else if (t < 0.5) 0.5 * Math.sqrt(1 - (t = t * 2 - 1) * t) else -0.5 * ((Math.sqrt(1 - (t = t * 2 - 1) * t) - 1) - 1);
}
/*
* BOUNCE EASING
*/
public static inline function bounceIn(t:Float):Float {
return if ((t = 1 - t) < (1 / 2.75)) {
1 - ((7.5625 * t * t));
} else if (t < (2 / 2.75)) {
1 - ((7.5625 * (t -= (1.5 / 2.75)) * t + 0.75));
} else if (t < (2.5 / 2.75)) {
1 - ((7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375));
} else {
1 - ((7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375));
}
}
public static inline function bounceOut(t:Float):Float {
return if (t < (1 / 2.75)) {
(7.5625 * t * t);
} else if (t < (2 / 2.75)) {
(7.5625 * (t -= (1.5 / 2.75)) * t + 0.75);
} else if (t < (2.5 / 2.75)) {
(7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375);
} else {
(7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375);
}
}
public static inline function bounceInOut(t:Float):Float {
return if (t < 0.5) {
if ((t = (1 - t * 2)) < (1 / 2.75)) {
(1 - ((7.5625 * t * t))) * 0.5;
} else if (t < (2 / 2.75)) {
(1 - ((7.5625 * (t -= (1.5 / 2.75)) * t + 0.75))) * 0.5;
} else if (t < (2.5 / 2.75)) {
(1 - ((7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375))) * 0.5;
} else {
(1 - ((7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375))) * 0.5;
}
} else {
if ((t = (t * 2 - 1)) < (1 / 2.75)) {
((7.5625 * t * t)) * 0.5 + 0.5;
} else if (t < (2 / 2.75)) {
((7.5625 * (t -= (1.5 / 2.75)) * t + 0.75)) * 0.5 + 0.5;
} else if (t < (2.5 / 2.75)) {
((7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375)) * 0.5 + 0.5;
} else {
((7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375)) * 0.5 + 0.5;
}
}
}
public static inline function bounceOutIn(t:Float):Float {
return if (t < 0.5) {
if ((t = (t * 2)) < (1 / 2.75)) {
0.5 * (7.5625 * t * t);
} else if (t < (2 / 2.75)) {
0.5 * (7.5625 * (t -= (1.5 / 2.75)) * t + 0.75);
} else if (t < (2.5 / 2.75)) {
0.5 * (7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375);
} else {
0.5 * (7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375);
}
} else {
if ((t = (1 - (t * 2 - 1))) < (1 / 2.75)) {
0.5 - (0.5 * (7.5625 * t * t)) + 0.5;
} else if (t < (2 / 2.75)) {
0.5 - (0.5 * (7.5625 * (t -= (1.5 / 2.75)) * t + 0.75)) + 0.5;
} else if (t < (2.5 / 2.75)) {
0.5 - (0.5 * (7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375)) + 0.5;
} else {
0.5 - (0.5 * (7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375)) + 0.5;
}
}
}
private static inline var overshoot:Float = 1.70158;
/*
* BACK EASING
*/
public static inline function backIn(t:Float):Float {
return if (t == 0) {
0;
} else if (t == 1) {
1;
} else {
t * t * ((overshoot + 1) * t - overshoot);
}
}
public static inline function backOut(t:Float):Float {
return if (t == 0) {
0;
} else if (t == 1) {
1;
} else {
((t = t - 1) * t * ((overshoot + 1) * t + overshoot) + 1);
}
}
public static inline function backInOut(t:Float):Float {
return if (t == 0) {
0;
} else if (t == 1) {
1;
} else if ((t *= 2) < 1) {
0.5 * (t * t * (((overshoot * 1.525) + 1) * t - overshoot * 1.525));
} else {
0.5 * ((t -= 2) * t * (((overshoot * 1.525) + 1) * t + overshoot * 1.525) + 2);
}
}
public static inline function backOutIn(t:Float):Float {
return if (t == 0) {
0;
} else if (t == 1) {
1;
} else if (t < 0.5) {
0.5 * ((t = t * 2 - 1) * t * ((overshoot + 1) * t + overshoot) + 1);
} else {
0.5 * (t = t * 2 - 1) * t * ((overshoot + 1) * t - overshoot) + 0.5;
}
}
/*
* ELASTIC EASING
*/
static inline var amplitude:Float = 1;
static inline var period:Float = 0.0003;
public static inline function elasticIn(t:Float):Float {
return if (t == 0) {
0;
} else if (t == 1) {
1;
} else {
var s:Float = period / 4;
-(amplitude * Math.exp(LN_2_10 * (t -= 1)) * Math.sin((t * 0.001 - s) * (2 * PI) / period));
}
}
public static inline function elasticOut(t:Float):Float {
return if (t == 0) {
0;
} else if (t == 1) {
1;
} else {
var s:Float = period / 4;
Math.exp(-LN_2_10 * t) * Math.sin((t * 0.001 - s) * (2 * PI) / period) + 1;
}
}
public static inline function elasticInOut(t:Float):Float {
return if (t == 0) {
0;
} else if (t == 1) {
1;
} else {
var s:Float = period / 4;
if ((t *= 2) < 1) {
-0.5 * (amplitude * Math.exp(LN_2_10 * (t -= 1)) * Math.sin((t * 0.001 - s) * (2 * PI) / period));
} else {
amplitude * Math.exp(-LN_2_10 * (t -= 1)) * Math.sin((t * 0.001 - s) * (2 * PI) / period) * 0.5 + 1;
}
}
}
public static inline function elasticOutIn(t:Float):Float {
return if (t < 0.5) {
if ((t *= 2) == 0) {
0;
} else {
var s = period / 4;
(amplitude / 2) * Math.exp(-LN_2_10 * t) * Math.sin((t * 0.001 - s) * (2 * PI) / period) + 0.5;
}
} else {
if (t == 0.5) {
0.5;
} else if (t == 1) {
1;
} else {
t = t * 2 - 1;
var s = period / 4;
-((amplitude / 2) * Math.exp(LN_2_10 * (t -= 1)) * Math.sin((t * 0.001 - s) * (2 * PI) / period)) + 0.5;
}
}
}
/*
* WARP EASING
*/
public static inline function warpOut(t:Float):Float {
return t <= 0 ? 0 : 1;
}
public static inline function warpIn(t:Float):Float {
return t < 1 ? 0 : 1;
}
public static inline function warpInOut(t:Float):Float {
return t < 0.5 ? 0 : 1;
}
public static inline function warpOutIn(t:Float):Float {
return if (t <= 0) 0 else if (t < 1) 0.5 else 1;
}
}
/**
* Static extension of Float.
*/
class FloatTools
{
/** same as `1 - rate` */
public static inline function revert(rate:Float):Float
{
return 1 - rate;
}
/* Clamps a `value` between `min` and `max`. */
public static inline function clamp(value:Float, min:Float = 0.0, max:Float = 1.0):Float
{
return if (value <= min) min else if (max <= value) max else value;
}
/** Linear interpolation between `from` and `to` by `rate` */
public static inline function lerp(rate:Float, from:Float, to:Float):Float
{
return from * (1 - rate) + to * rate;
}
/** Normalizes a `value` within the range between `from` and `to` into a value between 0 and 1 */
public static inline function inverseLerp(value:Float, from:Float, to:Float):Float
{
return (value - from) / (to - from);
}
public static inline function repeat(value:Float, from:Float = 0.0, to:Float = 1.0):Float
{
var p = inverseLerp(value, from, to);
return p - Math.floor(p);
}
public static inline function shake(rate:Float, center:Float = 0.0, ?randomFunc:Void->Float):Float
{
if (randomFunc == null) randomFunc = Math.random;
return center + spread(randomFunc(), rate);
}
/** same as `FloatTools.lerp(rate, -scale, scale)` */
public static inline function spread(rate:Float, scale:Float):Float
{
return lerp(rate, -scale, scale);
}
public static inline function sinByRate(rate:Float) {
return Math.sin(rate * 2 * Math.PI);
}
public static inline function cosByRate(rate:Float) {
return Math.cos(rate * 2 * Math.PI);
}
// =================================================
// Round Trip
// =================================================
/** Round trip motion that goes from 0.0 to 1.0 and returns to 0.0 in the reverse playback movement. */
public static inline function yoyo(rate:Float, easing:Float->Float):Float
{
return easing((if (rate < 0.5) rate else (1 - rate)) * 2);
}
/** Round trip motion that goes from 0.0 to 1.0 and returns to 0.0 with the movement in which the moving direction is reversed. */
public static inline function zigzag(rate:Float, easing:Float->Float):Float
{
return if (rate < 0.5) easing(rate * 2) else 1 - easing((rate - 0.5) * 2);
}
// =================================================
// Complex Easing
// =================================================
/** Intermediate easing between the two easings */
public static inline function mixEasing(
rate:Float,
easing1:Float->Float,
easing2:Float->Float,
easing2Strength:Float = 0.5):Float
{
return easing2Strength.lerp(
easing1(rate),
easing2(rate)
);
}
/** Gradually changes to another easing at the beginning and at the end */
public static inline function crossfadeEasing(
rate:Float,
easing1:Float->Float,
easing2:Float->Float,
easing2StrengthEasing:Float->Float,
easing2StrengthStart:Float = 0,
easing2StrengthEnd:Float = 1):Float
{
return easing2StrengthEasing(rate).lerp(
easing2StrengthStart,
easing2StrengthEnd
).lerp(
easing1(rate),
easing2(rate)
);
}
public static inline function connectEasing(
time:Float,
easing1:Float->Float,
easing2:Float->Float,
switchTime:Float = 0.5,
switchValue:Float = 0.5
):Float
{
return if (time < switchTime) {
easing1(time.inverseLerp(0, switchTime)).lerp(0, switchValue);
} else {
easing2(time.inverseLerp(switchTime, 1)).lerp(switchValue, 1);
}
}
public static inline function oneTwoEasing(
time:Float,
easingOne:Float->Float,
easingTwo:Float->Float,
switchTime:Float = 0.5):Float
{
return if (time < switchTime) {
easingOne(time.inverseLerp(0, switchTime));
} else {
easingTwo(time.inverseLerp(switchTime, 1));
}
}
// =================================================
// Float Array
// =================================================
/**
* @param sortedValues must be sorted
* @param value
* @param boundaryMode
* @return 0 to sortedValues.length integer
*/
public static inline function binarySearch(sortedValues:Array<Float>, value:Float, boundaryMode:BoundaryMode = BoundaryMode.Low):Int
{
var min = 0;
var max = sortedValues.length;
if (boundaryMode == BoundaryMode.Low) {
while (true) {
var next = Std.int((max - min) / 2) + min;
var dv = sortedValues[next];
if (dv <= value) {
min = next + 1;
} else {
max = next;
}
if (min == max) break;
}
} else {
while (true) {
var next = Std.int((max - min) / 2) + min;
var dv = sortedValues[next];
if (dv < value) {
min = next + 1;
} else {
max = next;
}
if (min == max) break;
}
}
return min;
}
// =================================================
// Polyline
// =================================================
public static inline function polyline(rate:Float, values:Array<Float>):Float
{
return if (values.length < 2) {
throw "points length must be more than 2";
} else {
var max = values.length - 1;
var scaledRate = rate * max;
var index = Math.floor(clamp(scaledRate, 0, max - 1));
var innerRate = scaledRate - index;
lerp(innerRate, values[index], values[index + 1]);
}
}
// =================================================
// Bernstein Polynomial
// =================================================
/** Quadratic Bernstein polynomial */
public static inline function bezier2(rate:Float, from:Float, control:Float, to:Float):Float
{
return lerp(rate, lerp(rate, from, control), lerp(rate, control, to));
}
/** Cubic Bernstein polynomial */
public static inline function bezier3(rate:Float, from:Float, control1:Float, control2:Float, to:Float):Float
{
return bezier2(rate, lerp(rate, from, control1), lerp(rate, control1, control2), lerp(rate, control2, to));
}
/** Bernstein polynomial, which is the mathematical basis for Bézier curve */
public static inline function bezier(rate:Float, values:Array<Float>):Float
{
return if (values.length < 2) {
throw "points length must be more than 2";
} else if (values.length == 2) {
lerp(rate, values[0], values[1]);
} else if (values.length == 3) {
bezier2(rate, values[0], values[1], values[2]);
} else {
_bezier(rate, values);
}
}
static function _bezier(rate:Float, values:Array<Float>)
{
if (values.length == 4) {
return bezier3(rate, values[0], values[1], values[2], values[3]);
}
return _bezier(rate, [for (i in 0...values.length - 1) lerp(rate, values[i], values[i + 1])]);
}
/**
* Uniform Quadratic B-spline
*/
public static inline function uniformQuadraticBSpline(rate:Float, values:Array<Float>):Float
{
return if (values.length < 2) {
throw "points length must be more than 2";
} else if (values.length == 2) {
lerp(rate, values[0], values[1]);
} else {
var max = values.length - 2;
var scaledRate = rate * max;
var index = Math.floor(clamp(scaledRate, 0, max - 1));
var innerRate = scaledRate - index;
var p0 = values[index];
var p1 = values[index + 1];
var p2 = values[index + 2];
innerRate * innerRate * (p0 / 2 - p1 + p2 / 2) + innerRate * (-p0 + p1) + p0 / 2 + p1 / 2;
}
}
// =================================================
// Converter
// =================================================
public static inline function frameToSecond(frame:Float, fps:Float):Float {
return frame / fps;
}
public static inline function secondToFrame(second:Float, fps:Float):Float {
return second * fps;
}
public static inline function degreeToRate(degree:Float) {
return degree / 360;
}
public static inline function rateToDegree(rate:Float) {
return rate * 360;
}
public static inline function radianToRate(radian:Float) {
return radian / (2 * Math.PI);
}
public static inline function rateToRadian(rate:Float) {
return rate * 2 * Math.PI;
}
public static inline function millisecondToBeat(millisecond:Float, bpm:Float):Float
{
return millisecond * bpm / 60000;
}
public static inline function beatToMillisecond(beat:Float, bpm:Float):Float
{
return beat * 60000 / bpm;
}
}
/**
* Static extension of point on XY coordinates. For example, Bezier curve. It can be used not only for the Point class in Flash, but also for Point types in various libraries.
*/
class PointTools {
// =================================================
// Polyline
// =================================================
public static inline function polyline(outputPoint:Point, rate:Float, points:Iterable<Point>):Void {
var xs = [];
var ys = [];
for (p in points) {
xs.push(p.x);
ys.push(p.y);
}
outputPoint.x = rate.polyline(xs);
outputPoint.y = rate.polyline(ys);
}
// =================================================
// Bézier Curve
// =================================================
/** Quadratic Bernstein polynomial */
public static inline function bezier2(outputPoint:Point, rate:Float, from:Point, control:Point, to:Point):Void {
outputPoint.x = rate.bezier2(from.x, control.x, from.x);
outputPoint.y = rate.bezier2(from.y, control.y, from.y);
}
/** Cubic Bernstein polynomial */
public static inline function bezier3(outputPoint:Point, rate:Float, from:Point, control1:Point, control2:Point, to:Point):Void {
outputPoint.x = rate.bezier3(from.x, control1.x, control2.x, from.x);
outputPoint.y = rate.bezier3(from.y, control1.y, control2.y, from.y);
}
/** Bernstein polynomial, which is the mathematical basis for Bézier curve */
public static inline function bezier(outputPoint:Point, rate:Float, points:Iterable<Point>):Void {
var xs = [];
var ys = [];
for (p in points) {
xs.push(p.x);
ys.push(p.y);
}
outputPoint.x = rate.bezier(xs);
outputPoint.y = rate.bezier(ys);
}
// =================================================
// B-spline Curve
// =================================================
/** Uniform Quadratic B-spline */
public static inline function uniformQuadraticBSpline(outputPoint:Point, rate:Float, points:Iterable<Point>):Void {
var xs = [];
var ys = [];
for (p in points) {
xs.push(p.x);
ys.push(p.y);
}
outputPoint.x = rate.uniformQuadraticBSpline(xs);
outputPoint.y = rate.uniformQuadraticBSpline(ys);
}
}
/**
* Extension of matrix of affine transformation of XY coordinates. It adds a function for similarity transformation of two-dimensional motion. It can be used not only for the Flash Matrix class but also for the Matrix type of other library with similar interface.
*/
class MatrixTools {
public static inline function createSimilarityTransform(outputMatrix:Matrix, fromX:Float, fromY:Float, toX:Float, toY:Float) {
var dx = toX - fromX;
var dy = toY - fromY;
var rot = Math.atan2(dy, dx);
var d = Math.sqrt(dx * dx + dy * dy);
outputMatrix.a = d * Math.cos(rot);
outputMatrix.b = d * Math.sin(rot);
outputMatrix.c = -d * Math.sin(rot);
outputMatrix.d = d * Math.cos(rot);
outputMatrix.tx = fromX;
outputMatrix.ty = fromY;
}
}