aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/pathops_sorter.htm
blob: 4fb18fad640b31c6304acefbc5a99a060b1bd137 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
<div style="height:0">

<div id="sect1">
{{{fX=1.97000003 fY=2.00000000 }, {fX=1.97000003 fY=3.97000003 }, {fX=0.000000000 fY=3.97000003 }} fW: 0.707106769
{{{fX=1.99750006 fY=2.00000000 } {fX=1.99750006 fY=2.02749991 }
{{{fX=1.96415257 fY=2.36347127 } {fX=1.95914865 fY=2.39051223 }

</div>

</div>

<script type="text/javascript">

var testDivs = [
sect1,
];

    var decimal_places = 3;

    var tests = [];
    var testTitles = [];
    var testIndex = 0;
    var ctx;

    var subscale = 1;
    var xmin, xmax, ymin, ymax;
    var scale;
    var initScale;
    var mouseX, mouseY;
    var mouseDown = false;
    var srcLeft, srcTop;
    var screenWidth, screenHeight;
    var drawnPts;
    var curveT = 0;
    var curveW = -1;

    var lastX, lastY;
    var activeCurve = [];
    var activePt;
    var ids = [];

    var focus_on_selection = 0;
    var draw_t = false;
    var draw_w = false;
    var draw_closest_t = false;
    var draw_cubic_red = false;
    var draw_derivative = false;
    var draw_endpoints = 2;
    var draw_id = 0;
    var draw_midpoint = 0;
    var draw_mouse_xy = false;
    var draw_order = false;
    var draw_point_xy = false;
    var draw_ray_intersect = false;
    var draw_quarterpoint = 0;
    var draw_tangents = 1;
    var draw_sortpoint = 0;
    var retina_scale = !!window.devicePixelRatio;

    function parse(test, title) {
        var curveStrs = test.split("{{");
        var pattern = /-?\d+\.*\d*e?-?\d*/g;
        var curves = [];
        for (var c in curveStrs) {
            var curveStr = curveStrs[c];
            var idPart = curveStr.split("id=");
            var id = -1;
            if (idPart.length == 2) {
                id = parseInt(idPart[1]);
                curveStr = idPart[0];
            }
            var points = curveStr.match(pattern);
            var pts = [];
            for (var wd in points) {
                var num = parseFloat(points[wd]);
                if (isNaN(num)) continue;
                pts.push(num);
            }
            if (pts.length > 2) {
                curves.push(pts);
            }
            if (id >= 0) {
                ids.push(id);
                ids.push(pts);
            }
        }
        if (curves.length >= 1) {
            tests.push(curves);
            testTitles.push(title);
        }
    }

    function init(test) {
        var canvas = document.getElementById('canvas');
        if (!canvas.getContext) return;
        ctx = canvas.getContext('2d');
        var resScale = retina_scale && window.devicePixelRatio ? window.devicePixelRatio : 1;
        var unscaledWidth = window.innerWidth - 20;
        var unscaledHeight = window.innerHeight - 20;
        screenWidth = unscaledWidth;
        screenHeight = unscaledHeight;
        canvas.width = unscaledWidth * resScale;
        canvas.height = unscaledHeight * resScale;
        canvas.style.width = unscaledWidth + 'px';
        canvas.style.height = unscaledHeight + 'px';
        if (resScale != 1) {
            ctx.scale(resScale, resScale);
        }
        xmin = Infinity;
        xmax = -Infinity;
        ymin = Infinity;
        ymax = -Infinity;
        for (var curves in test) {
            var curve = test[curves];
            var last = curve.length - (curve.length % 2 == 1 ? 1 : 0);
            for (var idx = 0; idx < last; idx += 2) {
                xmin = Math.min(xmin, curve[idx]);
                xmax = Math.max(xmax, curve[idx]);
                ymin = Math.min(ymin, curve[idx + 1]);
                ymax = Math.max(ymax, curve[idx + 1]);
            }
        }
        xmin -= Math.min(1, Math.max(xmax - xmin, ymax - ymin));
        var testW = xmax - xmin;
        var testH = ymax - ymin;
        subscale = 1;
        while (testW * subscale < 0.1 && testH * subscale < 0.1) {
            subscale *= 10;
        }
        while (testW * subscale > 10 && testH * subscale > 10) {
            subscale /= 10;
        }
        setScale(xmin, xmax, ymin, ymax);
        mouseX = (screenWidth / 2) / scale + srcLeft;
        mouseY = (screenHeight / 2) / scale + srcTop;
        initScale = scale;
    }

    function setScale(x0, x1, y0, y1) {
        var srcWidth = x1 - x0;
        var srcHeight = y1 - y0;
        var usableWidth = screenWidth;
        var xDigits = Math.ceil(Math.log(Math.abs(xmax)) / Math.log(10));
        var yDigits = Math.ceil(Math.log(Math.abs(ymax)) / Math.log(10));
        usableWidth -= (xDigits + yDigits) * 10;
        usableWidth -= decimal_places * 10;
        var hscale = usableWidth / srcWidth;
        var vscale = screenHeight / srcHeight;
        scale = Math.min(hscale, vscale);
        var invScale = 1 / scale;
        var sxmin = x0 - invScale * 5;
        var symin = y0 - invScale * 10;
        var sxmax = x1 + invScale * (6 * decimal_places + 10);
        var symax = y1 + invScale * 10;
        srcWidth = sxmax - sxmin;
        srcHeight = symax - symin;
        hscale = usableWidth / srcWidth;
        vscale = screenHeight / srcHeight;
        scale = Math.min(hscale, vscale);
        srcLeft = sxmin;
        srcTop = symin;
    }

function dxy_at_t(curve, t) {
    var dxy = {};
    if (curve.length == 6) {
        var a = t - 1;
        var b = 1 - 2 * t;
        var c = t;
        dxy.x = a * curve[0] + b * curve[2] + c * curve[4];
        dxy.y = a * curve[1] + b * curve[3] + c * curve[5];
    } else if (curve.length == 7) {
        var p20x = curve[4] - curve[0];
        var p20y = curve[5] - curve[1];
        var p10xw = (curve[2] - curve[0]) * curve[6];
        var p10yw = (curve[3] - curve[1]) * curve[6];
        var coeff0x = curve[6] * p20x - p20x;
        var coeff0y = curve[6] * p20y - p20y;
        var coeff1x = p20x - 2 * p10xw;
        var coeff1y = p20y - 2 * p10yw;
        dxy.x = t * (t * coeff0x + coeff1x) + p10xw;
        dxy.y = t * (t * coeff0y + coeff1y) + p10yw;
    } else if (curve.length == 8) {
        var one_t = 1 - t;
        var a = curve[0];
        var b = curve[2];
        var c = curve[4];
        var d = curve[6];
        dxy.x = 3 * ((b - a) * one_t * one_t + 2 * (c - b) * t * one_t + (d - c) * t * t);
        a = curve[1];
        b = curve[3];
        c = curve[5];
        d = curve[7];
        dxy.y = 3 * ((b - a) * one_t * one_t + 2 * (c - b) * t * one_t + (d - c) * t * t);
    }
    return dxy;
}

    var flt_epsilon = 1.19209290E-07;

    function approximately_zero(A) {
        return Math.abs(A) < flt_epsilon;
    }

    function approximately_zero_inverse(A) {
        return Math.abs(A) > (1 / flt_epsilon);
    }

    function quad_real_roots(A, B, C) {
        var s = [];
        var p = B / (2 * A);
        var q = C / A;
        if (approximately_zero(A) && (approximately_zero_inverse(p)
                || approximately_zero_inverse(q))) {
            if (approximately_zero(B)) {
                if (C == 0) {
                    s[0] = 0;
                }
                return s;
            }
            s[0] = -C / B;
            return s;
        }
        /* normal form: x^2 + px + q = 0 */
        var p2 = p * p;
        if (!approximately_zero(p2 - q) && p2 < q) {
            return s;
        }
        var sqrt_D = 0;
        if (p2 > q) {
            sqrt_D = Math.sqrt(p2 - q);
        }
        s[0] = sqrt_D - p;
        var flip = -sqrt_D - p;
        if (!approximately_zero(s[0] - flip)) {
            s[1] = flip;
        }
        return s;
    }

    function cubic_real_roots(A, B, C, D) {
        if (approximately_zero(A)) {  // we're just a quadratic
            return quad_real_roots(B, C, D);
        }
        if (approximately_zero(D)) {  // 0 is one root
            var s = quad_real_roots(A, B, C);
            for (var i = 0; i < s.length; ++i) {
                if (approximately_zero(s[i])) {
                    return s;
                }
            }
            s.push(0);
            return s;
        }
        if (approximately_zero(A + B + C + D)) {  // 1 is one root
            var s = quad_real_roots(A, A + B, -D);
            for (var i = 0; i < s.length; ++i) {
                if (approximately_zero(s[i] - 1)) {
                    return s;
                }
            }
            s.push(1);
            return s;
        }
        var a, b, c;
        var invA = 1 / A;
        a = B * invA;
        b = C * invA;
        c = D * invA;
        var a2 = a * a;
        var Q = (a2 - b * 3) / 9;
        var R = (2 * a2 * a - 9 * a * b + 27 * c) / 54;
        var R2 = R * R;
        var Q3 = Q * Q * Q;
        var R2MinusQ3 = R2 - Q3;
        var adiv3 = a / 3;
        var r;
        var roots = [];
        if (R2MinusQ3 < 0) {   // we have 3 real roots
            var theta = Math.acos(R / Math.sqrt(Q3));
            var neg2RootQ = -2 * Math.sqrt(Q);
            r = neg2RootQ * Math.cos(theta / 3) - adiv3;
            roots.push(r);
            r = neg2RootQ * Math.cos((theta + 2 * Math.PI) / 3) - adiv3;
            if (!approximately_zero(roots[0] - r)) {
                roots.push(r);
            }
            r = neg2RootQ * Math.cos((theta - 2 * Math.PI) / 3) - adiv3;
            if (!approximately_zero(roots[0] - r) && (roots.length == 1
                        || !approximately_zero(roots[1] - r))) {
                roots.push(r);
            }
        } else {  // we have 1 real root
            var sqrtR2MinusQ3 = Math.sqrt(R2MinusQ3);
            var A = Math.abs(R) + sqrtR2MinusQ3;
            A = Math.pow(A, 1/3);
            if (R > 0) {
                A = -A;
            }
            if (A != 0) {
                A += Q / A;
            }
            r = A - adiv3;
            roots.push(r);
            if (approximately_zero(R2 - Q3)) {
                r = -A / 2 - adiv3;
                if (!approximately_zero(roots[0] - r)) {
                    roots.push(r);
                }
            }
        }
        return roots;
    }

    function approximately_zero_or_more(tValue) {
        return tValue >= -flt_epsilon;
    }

    function approximately_one_or_less(tValue) {
        return tValue <= 1 + flt_epsilon;
    }

    function approximately_less_than_zero(tValue) {
        return tValue < flt_epsilon;
    }

    function approximately_greater_than_one(tValue) {
        return tValue > 1 - flt_epsilon;
    }

    function add_valid_ts(s) {
        var t = [];
    nextRoot:
        for (var index = 0; index < s.length; ++index) {
            var tValue = s[index];
            if (approximately_zero_or_more(tValue) && approximately_one_or_less(tValue)) {
                if (approximately_less_than_zero(tValue)) {
                    tValue = 0;
                } else if (approximately_greater_than_one(tValue)) {
                    tValue = 1;
                }
                for (var idx2 = 0; idx2 < t.length; ++idx2) {
                    if (approximately_zero(t[idx2] - tValue)) {
                        continue nextRoot;
                    }
                }
                t.push(tValue);
            }
        }
        return t;
    }

    function quad_roots(A, B, C) {
        var s = quad_real_roots(A, B, C);
        var foundRoots = add_valid_ts(s);
        return foundRoots;
    }

    function cubic_roots(A, B, C, D) {
        var s = cubic_real_roots(A, B, C, D);
        var foundRoots = add_valid_ts(s);
        return foundRoots;
    }

    function ray_curve_intersect(startPt, endPt, curve) {
        var adj = endPt[0] - startPt[0];
        var opp = endPt[1] - startPt[1];
        var r = [];
        var len = (curve.length == 7 ? 6 : curve.length) / 2;
        for (var n = 0; n < len; ++n) {
            r[n] = (curve[n * 2 + 1] - startPt[1]) * adj - (curve[n * 2] - startPt[0]) * opp;
        }
        if (curve.length == 6) {
            var A = r[2];
            var B = r[1];
            var C = r[0];
            A += C - 2 * B;  // A = a - 2*b + c
            B -= C;  // B = -(b - c)
            return quad_roots(A, 2 * B, C);
        }
        if (curve.length == 7) {
            var A = r[2];
            var B = r[1] * curve[6];
            var C = r[0];
            A += C - 2 * B;  // A = a - 2*b + c
            B -= C;  // B = -(b - c)
            return quad_roots(A, 2 * B, C);
        }
        var A = r[3];       // d
        var B = r[2] * 3;   // 3*c
        var C = r[1] * 3;   // 3*b
        var D = r[0];       // a
        A -= D - C + B;     // A =   -a + 3*b - 3*c + d
        B += 3 * D - 2 * C; // B =  3*a - 6*b + 3*c
        C -= 3 * D;         // C = -3*a + 3*b
        return cubic_roots(A, B, C, D);
    }

    function x_at_t(curve, t) {
        var one_t = 1 - t;
        if (curve.length == 4) {
            return one_t * curve[0] + t * curve[2];
        }
        var one_t2 = one_t * one_t;
        var t2 = t * t;
        if (curve.length == 6) {
            return one_t2 * curve[0] + 2 * one_t * t * curve[2] + t2 * curve[4];
        }
        if (curve.length == 7) {
            var numer = one_t2 * curve[0] + 2 * one_t * t * curve[2] * curve[6]
                    + t2 * curve[4];
            var denom = one_t2            + 2 * one_t * t            * curve[6]
                    + t2;
            return numer / denom;
        }
        var a = one_t2 * one_t;
        var b = 3 * one_t2 * t;
        var c = 3 * one_t * t2;
        var d = t2 * t;
        return a * curve[0] + b * curve[2] + c * curve[4] + d * curve[6];
    }

    function y_at_t(curve, t) {
        var one_t = 1 - t;
        if (curve.length == 4) {
            return one_t * curve[1] + t * curve[3];
        }
        var one_t2 = one_t * one_t;
        var t2 = t * t;
        if (curve.length == 6) {
            return one_t2 * curve[1] + 2 * one_t * t * curve[3] + t2 * curve[5];
        }
        if (curve.length == 7) {
            var numer = one_t2 * curve[1] + 2 * one_t * t * curve[3] * curve[6]
                    + t2 * curve[5];
            var denom = one_t2            + 2 * one_t * t            * curve[6]
                    + t2;
            return numer / denom;
        }
        var a = one_t2 * one_t;
        var b = 3 * one_t2 * t;
        var c = 3 * one_t * t2;
        var d = t2 * t;
        return a * curve[1] + b * curve[3] + c * curve[5] + d * curve[7];
    }

    function drawPointAtT(curve) {
        var x = x_at_t(curve, curveT);
        var y = y_at_t(curve, curveT);
        drawPoint(x, y);
    }

    function drawLine(x1, y1, x2, y2) {
        ctx.beginPath();
        ctx.moveTo((x1 - srcLeft) * scale,
                (y1 - srcTop) * scale);
        ctx.lineTo((x2 - srcLeft) * scale,
                (y2 - srcTop) * scale);
        ctx.stroke();
    }

    function drawPoint(px, py) {
        for (var pts = 0; pts < drawnPts.length; pts += 2) {
            var x = drawnPts[pts];
            var y = drawnPts[pts + 1];
            if (px == x && py == y) {
                return;
            }
        }
        drawnPts.push(px);
        drawnPts.push(py);
        var _px = (px - srcLeft) * scale;
        var _py = (py - srcTop) * scale;
        ctx.beginPath();
        ctx.arc(_px, _py, 3, 0, Math.PI * 2, true);
        ctx.closePath();
        ctx.stroke();
        if (draw_point_xy) {
            var label = px.toFixed(decimal_places) + ", " + py.toFixed(decimal_places);
            ctx.font = "normal 10px Arial";
            ctx.textAlign = "left";
            ctx.fillStyle = "black";
            ctx.fillText(label, _px + 5, _py);
        }
    }

    function drawPointSolid(px, py) {
        drawPoint(px, py);
        ctx.fillStyle = "rgba(0,0,0, 0.4)";
        ctx.fill();
    }

    function crossPt(origin, pt1, pt2) {
        return ((pt1[0] - origin[0]) * (pt2[1] - origin[1])
              - (pt1[1] - origin[1]) * (pt2[0] - origin[0])) > 0 ? 0 : 1;
    }

    // may not work well for cubics
    function curveClosestT(curve, x, y) {
        var closest = -1;
        var closestDist = Infinity;
        var l = Infinity, t = Infinity, r = -Infinity, b = -Infinity;
        for (var i = 0; i < 16; ++i) {
            var testX = x_at_t(curve, i / 16);
            l = Math.min(testX, l);
            r = Math.max(testX, r);
            var testY = y_at_t(curve, i / 16);
            t = Math.min(testY, t);
            b = Math.max(testY, b);
            var dx = testX - x;
            var dy = testY - y;
            var dist = dx * dx + dy * dy;
            if (closestDist > dist) {
                closestDist = dist;
                closest = i;
            }
        }
        var boundsX = r - l;
        var boundsY = b - t;
        var boundsDist = boundsX * boundsX + boundsY * boundsY;
        if (closestDist > boundsDist) {
            return -1;
        }
        console.log("closestDist = " + closestDist + " boundsDist = " + boundsDist
                + " t = " + closest / 16);
        return closest / 16;
    }

    var kMaxConicToQuadPOW2 = 5;

    function computeQuadPOW2(curve, tol) {
        var a = curve[6] - 1;
        var k = a / (4 * (2 + a));
        var x = k * (curve[0] - 2 * curve[2] + curve[4]);
        var y = k * (curve[1] - 2 * curve[3] + curve[5]);

        var error = Math.sqrt(x * x + y * y);
        var pow2;
        for (pow2 = 0; pow2 < kMaxConicToQuadPOW2; ++pow2) {
            if (error <= tol) {
                break;
            }
            error *= 0.25;
        }
        return pow2;
    }

    function subdivide_w_value(w) {
        return Math.sqrt(0.5 + w * 0.5);
    }

    function chop(curve, part1, part2) {
        var w = curve[6];
        var scale = 1 / (1 + w);
        part1[0] = curve[0];
        part1[1] = curve[1];
        part1[2] = (curve[0] + curve[2] * w) * scale;
        part1[3] = (curve[1] + curve[3] * w) * scale;
        part1[4] = part2[0] = (curve[0] + (curve[2] * w) * 2 + curve[4]) * scale * 0.5;
        part1[5] = part2[1] = (curve[1] + (curve[3] * w) * 2 + curve[5]) * scale * 0.5;
        part2[2] = (curve[2] * w + curve[4]) * scale;
        part2[3] = (curve[3] * w + curve[5]) * scale;
        part2[4] = curve[4];
        part2[5] = curve[5];
        part1[6] = part2[6] = subdivide_w_value(w);
    }

    function subdivide(curve, level, pts) {
        if (0 == level) {
            pts.push(curve[2]);
            pts.push(curve[3]);
            pts.push(curve[4]);
            pts.push(curve[5]);
        } else {
            var part1 = [], part2 = [];
            chop(curve, part1, part2);
            --level;
            subdivide(part1, level, pts);
            subdivide(part2, level, pts);
        }
    }

    function chopIntoQuadsPOW2(curve, pow2, pts) {
        subdivide(curve, pow2, pts);
        return 1 << pow2;
    }

    function drawConic(curve, srcLeft, srcTop, scale) {
        var tol = 1 / scale;
        var pow2 = computeQuadPOW2(curve, tol);
        var pts = [];
        chopIntoQuadsPOW2(curve, pow2, pts);
        for (var i = 0; i < pts.length; i += 4) {
            ctx.quadraticCurveTo(
                (pts[i + 0] - srcLeft) * scale, (pts[i + 1] - srcTop) * scale,
                (pts[i + 2] - srcLeft) * scale, (pts[i + 3] - srcTop) * scale);
        }
    }

    function draw(test, title) {
        ctx.font = "normal 50px Arial";
        ctx.textAlign = "left";
        ctx.fillStyle = "rgba(0,0,0, 0.1)";
        ctx.fillText(title, 50, 50);
        ctx.font = "normal 10px Arial";
        //  ctx.lineWidth = "1.001"; "0.999";
        var hullStarts = [];
        var hullEnds = [];
        var midSpokes = [];
        var midDist = [];
        var origin = [];
        var shortSpokes = [];
        var shortDist = [];
        var sweeps = [];
        drawnPts = [];
        for (var curves in test) {
            var curve = test[curves];
            origin.push(curve[0]);
            origin.push(curve[1]);
            var startPt = [];
            startPt.push(curve[2]);
            startPt.push(curve[3]);
            hullStarts.push(startPt);
            var endPt = [];
            if (curve.length == 4) {
                endPt.push(curve[2]);
                endPt.push(curve[3]);
            } else if (curve.length == 6 || curve.length == 7) {
                endPt.push(curve[4]);
                endPt.push(curve[5]);
            } else if (curve.length == 8) {
                endPt.push(curve[6]);
                endPt.push(curve[7]);
            }
            hullEnds.push(endPt);
            var sweep = crossPt(origin, startPt, endPt);
            sweeps.push(sweep);
            var midPt = [];
            midPt.push(x_at_t(curve, 0.5));
            midPt.push(y_at_t(curve, 0.5));
            midSpokes.push(midPt);
            var shortPt = [];
            shortPt.push(x_at_t(curve, 0.25));
            shortPt.push(y_at_t(curve, 0.25));
            shortSpokes.push(shortPt);
            var dx = midPt[0] - origin[0];
            var dy = midPt[1] - origin[1];
            var dist = Math.sqrt(dx * dx + dy * dy);
            midDist.push(dist);
            dx = shortPt[0] - origin[0];
            dy = shortPt[1] - origin[1];
            dist = Math.sqrt(dx * dx + dy * dy);
            shortDist.push(dist);
        }
        var intersect = [];
        var useIntersect = false;
        var maxWidth = Math.max(xmax - xmin, ymax - ymin);
        for (var curves in test) {
            var curve = test[curves];
            if (curve.length >= 6 && curve.length <= 8) {
                var opp = curves == 0 || curves == 1 ? 0 : 1;
                var sects = ray_curve_intersect(origin, hullEnds[opp], curve);
                intersect.push(sects);
                if (sects.length > 1) {
                    var intersection = sects[0];
                    if (intersection == 0) {
                        intersection = sects[1];
                    }
                    var ix = x_at_t(curve, intersection) - origin[0];
                    var iy = y_at_t(curve, intersection) - origin[1];
                    var ex = hullEnds[opp][0] - origin[0];
                    var ey = hullEnds[opp][1] - origin[1];
                    if (ix * ex >= 0 && iy * ey >= 0) {
                        var iDist = Math.sqrt(ix * ix + iy * iy);
                        var eDist = Math.sqrt(ex * ex + ey * ey);
                        var delta = Math.abs(iDist - eDist) / maxWidth;
                        if (delta > (curve.length != 8 ? 1e-5 : 1e-4)) {
                            useIntersect ^= true;
                        }
                    }
                }
            }
        }
        var midLeft = curves != 0 ? crossPt(origin, midSpokes[0], midSpokes[1]) : 0;
        var firstInside;
        if (useIntersect) {
            var sect1 = intersect[0].length > 1;
            var sIndex = sect1 ? 0 : 1;
            var sects = intersect[sIndex];
            var intersection = sects[0];
            if (intersection == 0) {
                intersection = sects[1];
            }
            var curve = test[sIndex];
            var ix = x_at_t(curve, intersection) - origin[0];
            var iy = y_at_t(curve, intersection) - origin[1];
            var opp = sect1 ? 1 : 0;
            var ex = hullEnds[opp][0] - origin[0];
            var ey = hullEnds[opp][1] - origin[1];
            var iDist = ix * ix + iy * iy;
            var eDist = ex * ex + ey * ey;
            firstInside = (iDist > eDist) ^ (sIndex == 0) ^ sweeps[0];
//            console.log("iDist=" + iDist + " eDist=" + eDist + " sIndex=" + sIndex
 //                   + " sweeps[0]=" + sweeps[0]);
        } else {
 //           console.log("midLeft=" + midLeft);
            firstInside = midLeft != 0;
        }
        var shorter = midDist[1] < midDist[0];
        var shortLeft = shorter ? crossPt(origin, shortSpokes[0], midSpokes[1])
                : crossPt(origin, midSpokes[0], shortSpokes[1]);
        var startCross = crossPt(origin, hullStarts[0], hullStarts[1]);
        var disallowShort = midLeft == startCross && midLeft == sweeps[0]
                    && midLeft == sweeps[1];

  //      console.log("midLeft=" + midLeft + " startCross=" + startCross);
        var intersectIndex = 0;
        for (var curves in test) {
            var curve = test[draw_id != 2 ? curves : test.length - curves - 1];
            if (curve.length != 4 && curve.length != 6 && curve.length != 7 && curve.length != 8) {
                continue;
            }
            ctx.lineWidth = 1;
            if (draw_tangents != 0) {
                if (draw_cubic_red ? curve.length == 8 : firstInside == curves) {
                    ctx.strokeStyle = "rgba(255,0,0, 0.3)";
                } else {
                    ctx.strokeStyle = "rgba(0,0,255, 0.3)";
                }
                drawLine(curve[0], curve[1], curve[2], curve[3]);
                if (draw_tangents != 2) {
                    if (curve.length > 4) drawLine(curve[2], curve[3], curve[4], curve[5]);
                    if (curve.length == 8) drawLine(curve[4], curve[5], curve[6], curve[7]);
                }
                if (draw_tangents != 1) {
                    if (curve.length == 6 || curve.length == 7) {
                        drawLine(curve[0], curve[1], curve[4], curve[5]);
                    }
                    if (curve.length == 8) drawLine(curve[0], curve[1], curve[6], curve[7]);
                }
            }
            ctx.beginPath();
            ctx.moveTo((curve[0] - srcLeft) * scale, (curve[1] - srcTop) * scale);
            if (curve.length == 4) {
                ctx.lineTo((curve[2] - srcLeft) * scale, (curve[3] - srcTop) * scale);
            } else if (curve.length == 6) {
                ctx.quadraticCurveTo(
                    (curve[2] - srcLeft) * scale, (curve[3] - srcTop) * scale,
                    (curve[4] - srcLeft) * scale, (curve[5] - srcTop) * scale);
            } else if (curve.length == 7) {
                drawConic(curve, srcLeft, srcTop, scale);
            } else {
                ctx.bezierCurveTo(
                    (curve[2] - srcLeft) * scale, (curve[3] - srcTop) * scale,
                    (curve[4] - srcLeft) * scale, (curve[5] - srcTop) * scale,
                    (curve[6] - srcLeft) * scale, (curve[7] - srcTop) * scale);
            }
            if (draw_cubic_red ? curve.length == 8 : firstInside == curves) {
                ctx.strokeStyle = "rgba(255,0,0, 1)";
            } else {
                ctx.strokeStyle = "rgba(0,0,255, 1)";
            }
            ctx.stroke();
            if (draw_endpoints > 0) {
                drawPoint(curve[0], curve[1]);
                if (draw_endpoints > 1 || curve.length == 4) {
                    drawPoint(curve[2], curve[3]);
                }
                if (curve.length == 6 || curve.length == 7 ||
                        (draw_endpoints > 1 && curve.length == 8)) {
                    drawPoint(curve[4], curve[5]);
                }
                if (curve.length == 8) drawPoint(curve[6], curve[7]);
            }
            if (draw_midpoint != 0) {
                if ((curves == 0) == (midLeft == 0)) {
                    ctx.strokeStyle = "rgba(0,180,127, 0.6)";
                } else {
                    ctx.strokeStyle = "rgba(127,0,127, 0.6)";
                }
                var midX = x_at_t(curve, 0.5);
                var midY = y_at_t(curve, 0.5);
                drawPointSolid(midX, midY);
                if (draw_midpoint > 1) {
                    drawLine(curve[0], curve[1], midX, midY);
                }
            }
            if (draw_quarterpoint != 0) {
                if ((curves == 0) == (shortLeft == 0)) {
                    ctx.strokeStyle = "rgba(0,191,63, 0.6)";
                } else {
                    ctx.strokeStyle = "rgba(63,0,191, 0.6)";
                }
                var midT = (curves == 0) == shorter ? 0.25 : 0.5;
                var midX = x_at_t(curve, midT);
                var midY = y_at_t(curve, midT);
                drawPointSolid(midX, midY);
                if (draw_quarterpoint > 1) {
                    drawLine(curve[0], curve[1], midX, midY);
                }
            }
            if (draw_sortpoint != 0) {
                if ((curves == 0) == ((disallowShort == -1 ? midLeft : shortLeft) == 0)) {
                    ctx.strokeStyle = "rgba(0,155,37, 0.6)";
                } else {
                    ctx.strokeStyle = "rgba(37,0,155, 0.6)";
                }
                var midT = (curves == 0) == shorter && disallowShort != curves ? 0.25 : 0.5;
                console.log("curves=" + curves + " disallowShort=" + disallowShort
                        + " midLeft=" + midLeft + " shortLeft=" + shortLeft
                        + " shorter=" + shorter + " midT=" + midT);
                var midX = x_at_t(curve, midT);
                var midY = y_at_t(curve, midT);
                drawPointSolid(midX, midY);
                if (draw_sortpoint > 1) {
                    drawLine(curve[0], curve[1], midX, midY);
                }
            }
            if (draw_ray_intersect != 0) {
                ctx.strokeStyle = "rgba(75,45,199, 0.6)";
                if (curve.length >= 6 && curve.length <= 8) {
                    var intersections = intersect[intersectIndex];
                    for (var i in intersections) {
                        var intersection = intersections[i];
                        var x = x_at_t(curve, intersection);
                        var y = y_at_t(curve, intersection);
                        drawPointSolid(x, y);
                        if (draw_ray_intersect > 1) {
                            drawLine(curve[0], curve[1], x, y);
                        }
                    }
                }
                ++intersectIndex;
            }
            if (draw_order) {
                var px = x_at_t(curve, 0.75);
                var py = y_at_t(curve, 0.75);
                var _px = (px - srcLeft) * scale;
                var _py = (py - srcTop) * scale;
                ctx.beginPath();
                ctx.arc(_px, _py, 15, 0, Math.PI * 2, true);
                ctx.closePath();
                ctx.fillStyle = "white";
                ctx.fill();
                if (draw_cubic_red ? curve.length == 8 : firstInside == curves) {
                    ctx.strokeStyle = "rgba(255,0,0, 1)";
                    ctx.fillStyle = "rgba(255,0,0, 1)";
                } else {
                    ctx.strokeStyle = "rgba(0,0,255, 1)";
                    ctx.fillStyle = "rgba(0,0,255, 1)";
                }
                ctx.stroke();
                ctx.font = "normal 16px Arial";
                ctx.textAlign = "center";
                ctx.fillText(parseInt(curves) + 1, _px, _py + 5);
            }
            if (draw_closest_t) {
                var t = curveClosestT(curve, mouseX, mouseY);
                if (t >= 0) {
                    var x = x_at_t(curve, t);
                    var y = y_at_t(curve, t);
                    drawPointSolid(x, y);
                }
            }
            if (!approximately_zero(scale - initScale)) {
                ctx.font = "normal 20px Arial";
                ctx.fillStyle = "rgba(0,0,0, 0.3)";
                ctx.textAlign = "right";
                ctx.fillText(scale.toFixed(decimal_places) + 'x',
                        screenWidth - 10, screenHeight - 5);
            }
            if (draw_t) {
                drawPointAtT(curve);
            }
            if (draw_id != 0) {
                var id = -1;
                for (var i = 0; i < ids.length; i += 2) {
                    if (ids[i + 1] == curve) {
                        id = ids[i];
                        break;
                    }
                }
                if (id >= 0) {
                    var px = x_at_t(curve, 0.5);
                    var py = y_at_t(curve, 0.5);
                    var _px = (px - srcLeft) * scale;
                    var _py = (py - srcTop) * scale;
                    ctx.beginPath();
                    ctx.arc(_px, _py, 15, 0, Math.PI * 2, true);
                    ctx.closePath();
                    ctx.fillStyle = "white";
                    ctx.fill();
                    ctx.strokeStyle = "rgba(255,0,0, 1)";
                    ctx.fillStyle = "rgba(255,0,0, 1)";
                    ctx.stroke();
                    ctx.font = "normal 16px Arial";
                    ctx.textAlign = "center";
                    ctx.fillText(id, _px, _py + 5);
                }
            }
        }
        if (draw_t) {
            drawCurveTControl();
        }
        if (draw_w) {
            drawCurveWControl();
        }
    }

    function drawCurveTControl() {
        ctx.lineWidth = 2;
        ctx.strokeStyle = "rgba(0,0,0, 0.3)";
        ctx.beginPath();
        ctx.rect(screenWidth - 80, 40, 28, screenHeight - 80);
        ctx.stroke();
        var ty = 40 + curveT * (screenHeight - 80);
        ctx.beginPath();
        ctx.moveTo(screenWidth - 80, ty);
        ctx.lineTo(screenWidth - 85, ty - 5);
        ctx.lineTo(screenWidth - 85, ty + 5);
        ctx.lineTo(screenWidth - 80, ty);
        ctx.fillStyle = "rgba(0,0,0, 0.6)";
        ctx.fill();
        var num = curveT.toFixed(decimal_places);
        ctx.font = "normal 10px Arial";
        ctx.textAlign = "left";
        ctx.fillText(num, screenWidth - 78, ty);
    }

    function drawCurveWControl() {
        var w = -1;
        var choice = 0;
        for (var curves in tests[testIndex]) {
            var curve = tests[testIndex][curves];
            if (curve.length != 7) {
                continue;
            }
            if (choice == curveW) {
                w = curve[6];
                break;
            }
            ++choice;
        }
        if (w < 0) {
            return;
        }
        ctx.lineWidth = 2;
        ctx.strokeStyle = "rgba(0,0,0, 0.3)";
        ctx.beginPath();
        ctx.rect(screenWidth - 40, 40, 28, screenHeight - 80);
        ctx.stroke();
        var ty = 40 + w * (screenHeight - 80);
        ctx.beginPath();
        ctx.moveTo(screenWidth - 40, ty);
        ctx.lineTo(screenWidth - 45, ty - 5);
        ctx.lineTo(screenWidth - 45, ty + 5);
        ctx.lineTo(screenWidth - 40, ty);
        ctx.fillStyle = "rgba(0,0,0, 0.6)";
        ctx.fill();
        var num = w.toFixed(decimal_places);
        ctx.font = "normal 10px Arial";
        ctx.textAlign = "left";
        ctx.fillText(num, screenWidth - 38, ty);
    }

    function ptInTControl() {
        var e = window.event;
        var tgt = e.target || e.srcElement;
        var left = tgt.offsetLeft;
        var top = tgt.offsetTop;
        var x = (e.clientX - left);
        var y = (e.clientY - top);
        if (x < screenWidth - 80 || x > screenWidth - 50) {
            return false;
        }
        if (y < 40 || y > screenHeight - 80) {
            return false;
        }
        curveT = (y - 40) / (screenHeight - 120);
        if (curveT < 0 || curveT > 1) {
            throw "stop execution";
        }
        return true;
    }

    function ptInWControl() {
        var e = window.event;
        var tgt = e.target || e.srcElement;
        var left = tgt.offsetLeft;
        var top = tgt.offsetTop;
        var x = (e.clientX - left);
        var y = (e.clientY - top);
        if (x < screenWidth - 40 || x > screenWidth - 10) {
            return false;
        }
        if (y < 40 || y > screenHeight - 80) {
            return false;
        }
        var w = (y - 40) / (screenHeight - 120);
        if (w < 0 || w > 1) {
            throw "stop execution";
        }
        var choice = 0;
        for (var curves in tests[testIndex]) {
            var curve = tests[testIndex][curves];
            if (curve.length != 7) {
                continue;
            }
            if (choice == curveW) {
                curve[6] = w;
                break;
            }
            ++choice;
        }
        return true;
    }

    function drawTop() {
        init(tests[testIndex]);
        redraw();
    }

    function redraw() {
        if (focus_on_selection > 0) {
            var focusXmin = focusYmin = Infinity;
            var focusXmax = focusYmax = -Infinity;
            var choice = 0;
            for (var curves in tests[testIndex]) {
                if (++choice != focus_on_selection) {
                    continue;
                }
                var curve = tests[testIndex][curves];
                var last = curve.length - (curve.length % 2 == 1 ? 1 : 0);
                for (var idx = 0; idx < last; idx += 2) {
                    focusXmin = Math.min(focusXmin, curve[idx]);
                    focusXmax = Math.max(focusXmax, curve[idx]);
                    focusYmin = Math.min(focusYmin, curve[idx + 1]);
                    focusYmax = Math.max(focusYmax, curve[idx + 1]);
                }
            }
            focusXmin -= Math.min(1, Math.max(focusXmax - focusXmin, focusYmax - focusYmin));
            if (focusXmin < focusXmax && focusYmin < focusYmax) {
                setScale(focusXmin, focusXmax, focusYmin, focusYmax);
            }
        }
        ctx.beginPath();
        ctx.rect(0, 0, ctx.canvas.width, ctx.canvas.height);
        ctx.fillStyle = "white";
        ctx.fill();
        draw(tests[testIndex], testTitles[testIndex]);
    }

    function doKeyPress(evt) {
        var char = String.fromCharCode(evt.charCode);
        var focusWasOn = false;
        switch (char) {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                decimal_places = char - '0';
                redraw();
                break;
            case '-':
                focusWasOn = focus_on_selection;
                if (focusWasOn) {
                    focus_on_selection = false;
                    scale /= 1.2;
                } else {
                    scale /= 2;
                }
                calcLeftTop();
                redraw();
                focus_on_selection = focusWasOn;
                break;
            case '=':
            case '+':
                focusWasOn = focus_on_selection;
                if (focusWasOn) {
                    focus_on_selection = false;
                    scale *= 1.2;
                } else {
                    scale *= 2;
                }
                calcLeftTop();
                redraw();
                focus_on_selection = focusWasOn;
                break;
            case 'b':
                draw_cubic_red ^= true;
                redraw();
                break;
            case 'c':
                drawTop();
                break;
            case 'd':
                var test = tests[testIndex];
                var testClone = [];
                for (var curves in test) {
                    var c = test[curves];
                    var cClone = [];
                    for (var index = 0; index < c.length; ++index) {
                        cClone.push(c[index]);
                    }
                    testClone.push(cClone);
                }
                tests.push(testClone);
                testTitles.push(testTitles[testIndex] + " copy");
                testIndex = tests.length - 1;
                redraw();
                break;
            case 'e':
                draw_endpoints = (draw_endpoints + 1) % 3;
                redraw();
                break;
            case 'f':
                draw_derivative ^= true;
                redraw();
                break;
            case 'i':
                draw_ray_intersect = (draw_ray_intersect + 1) % 3;
                redraw();
                break;
            case 'l':
                var test = tests[testIndex];
                console.log("<div id=\"" + testTitles[testIndex] + "\" >");
                for (var curves in test) {
                    var c = test[curves];
                    var s = "{{";
                    for (var i = 0; i < c.length; i += 2) {
                        s += "{";
                        s += c[i] + "," + c[i + 1];
                        s += "}";
                        if (i + 2 < c.length) {
                            s += ", ";
                        }
                    }
                    console.log(s + "}},");
                }
                console.log("</div>");
                break;
            case 'm':
                draw_midpoint = (draw_midpoint + 1) % 3;
                redraw();
                break;
            case 'N':
                testIndex += 9;
            case 'n':
                testIndex = (testIndex + 1) % tests.length;
                drawTop();
                break;
            case 'o':
                draw_order ^= true;
                redraw();
                break;
            case 'P':
                testIndex -= 9;
            case 'p':
                if (--testIndex < 0)
                    testIndex = tests.length - 1;
                drawTop();
                break;
            case 'q':
                draw_quarterpoint = (draw_quarterpoint + 1) % 3;
                redraw();
                break;
            case 'r':
                for (var i = 0; i < testDivs.length; ++i) {
                    var title = testDivs[i].id.toString();
                    if (title == testTitles[testIndex]) {
                        var str = testDivs[i].firstChild.data;
                        parse(str, title);
                        var original = tests.pop();
                        testTitles.pop();
                        tests[testIndex] = original;
                        break;
                    }
                }
                redraw();
                break;
            case 's':
                draw_sortpoint = (draw_sortpoint + 1) % 3;
                redraw();
                break;
            case 't':
                draw_t ^= true;
                redraw();
                break;
            case 'u':
                draw_closest_t ^= true;
                redraw();
                break;
            case 'v':
                draw_tangents = (draw_tangents + 1) % 4;
                redraw();
                break;
            case 'w':
                ++curveW;
                var choice = 0;
                draw_w = false;
                for (var curves in tests[testIndex]) {
                    var curve = tests[testIndex][curves];
                    if (curve.length != 7) {
                        continue;
                    }
                    if (choice == curveW) {
                        draw_w = true;
                        break;
                    }
                    ++choice;
                }
                if (!draw_w) {
                    curveW = -1;
                }
                redraw();
                break;
            case 'x':
                draw_point_xy ^= true;
                redraw();
                break;
            case 'y':
                draw_mouse_xy ^= true;
                redraw();
                break;
            case '\\':
                retina_scale ^= true;
                drawTop();
                break;
            case '`':
                ++focus_on_selection;
                if (focus_on_selection >= tests[testIndex].length) {
                    focus_on_selection = 0;
                }
                setScale(xmin, xmax, ymin, ymax);
                redraw();
                break;
            case '.':
                draw_id = (draw_id + 1) % 3;
                redraw();
                break;
        }
    }

    function doKeyDown(evt) {
        var char = evt.keyCode;
        var preventDefault = false;
        switch (char) {
            case 37: // left arrow
                if (evt.shiftKey) {
                    testIndex -= 9;
                }
                if (--testIndex < 0)
                    testIndex = tests.length - 1;
                if (evt.ctrlKey) {
                    redraw();
                } else {
                    drawTop();
                }
                preventDefault = true;
                break;
            case 39: // right arrow
                if (evt.shiftKey) {
                    testIndex += 9;
                }
                if (++testIndex >= tests.length)
                    testIndex = 0;
                if (evt.ctrlKey) {
                    redraw();
                } else {
                    drawTop();
                }
                preventDefault = true;
                break;
        }
        if (preventDefault) {
            evt.preventDefault();
            return false;
        }
        return true;
    }

    function calcXY() {
        var e = window.event;
        var tgt = e.target || e.srcElement;
        var left = tgt.offsetLeft;
        var top = tgt.offsetTop;
        mouseX = (e.clientX - left) / scale + srcLeft;
        mouseY = (e.clientY - top) / scale + srcTop;
    }

    function calcLeftTop() {
        srcLeft = mouseX - screenWidth / 2 / scale;
        srcTop = mouseY - screenHeight / 2 / scale;
    }

    function handleMouseClick() {
        if ((!draw_t || !ptInTControl()) && (!draw_w || !ptInWControl())) {
            calcXY();
        } else {
            redraw();
        }
    }

    function initDown() {
        var test = tests[testIndex];
        var bestDistance = 1000000;
        activePt = -1;
        for (var curves in test) {
            var testCurve = test[curves];
            if (testCurve.length != 4 && (testCurve.length < 6 || testCurve.length > 8)) {
                continue;
            }
            var testMax = testCurve.length == 7 ? 6 : testCurve.length;
            for (var i = 0; i < testMax; i += 2) {
                var testX = testCurve[i];
                var testY = testCurve[i + 1];
                var dx = testX - mouseX;
                var dy = testY - mouseY;
                var dist = dx * dx + dy * dy;
                if (dist > bestDistance) {
                    continue;
                }
                activeCurve = testCurve;
                activePt = i;
                bestDistance = dist;
            }
        }
        if (activePt >= 0) {
            lastX = mouseX;
            lastY = mouseY;
        }
    }

    function handleMouseOver() {
        calcXY();
        if (draw_mouse_xy) {
            var num = mouseX.toFixed(decimal_places) + ", " + mouseY.toFixed(decimal_places);
            ctx.beginPath();
            ctx.rect(300, 100, num.length * 6, 10);
            ctx.fillStyle = "white";
            ctx.fill();
            ctx.font = "normal 10px Arial";
            ctx.fillStyle = "black";
            ctx.textAlign = "left";
            ctx.fillText(num, 300, 108);
        }
        if (!mouseDown) {
            activePt = -1;
            return;
        }
        if (activePt < 0) {
            initDown();
            return;
        }
        var deltaX = mouseX - lastX;
        var deltaY = mouseY - lastY;
        lastX = mouseX;
        lastY = mouseY;
        if (activePt == 0) {
            var test = tests[testIndex];
            for (var curves in test) {
                var testCurve = test[curves];
                testCurve[0] += deltaX;
                testCurve[1] += deltaY;
            }
        } else {
            activeCurve[activePt] += deltaX;
            activeCurve[activePt + 1] += deltaY;
        }
        redraw();
    }

    function start() {
        for (var i = 0; i < testDivs.length; ++i) {
            var title = testDivs[i].id.toString();
            var str = testDivs[i].firstChild.data;
            parse(str, title);
        }
        drawTop();
        window.addEventListener('keypress', doKeyPress, true);
        window.addEventListener('keydown', doKeyDown, true);
        window.onresize = function () {
            drawTop();
        }
    }

</script>
</head>

<body onLoad="start();">

<canvas id="canvas" width="750" height="500"
    onmousedown="mouseDown = true"
    onmouseup="mouseDown = false"
    onmousemove="handleMouseOver()"
    onclick="handleMouseClick()"
    ></canvas >
</body>
</html>