All files / lib/path Curve.ts

91.56% Statements 1237/1351
97.11% Branches 471/485
75.7% Functions 81/107
91.56% Lines 1237/1351

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457                              1x 1x 1x 1x 1x 1x 1x 1x                               1x 1x 1x     1x                                                               1x 11813x 11813x 11813x 11813x 11813x 11813x 11813x       11813x     11760x 11760x 11760x 11813x     53x     7x 1x 1x 7x           6x     6x 6x 6x 6x 6x 53x   4x 4x 46x 42x 42x 42x 42x 42x                     11813x 11813x 11813x   1x                       1x   12491x 12491x             1x             1x 17x 17x 17x 17x 17x 17x                             1x 7x 7x             1x 136x 136x 136x 136x 136x 136x 136x 136x 136x               1x 7779x 7779x   1x                   1x 3247x 3247x   1x                   1x       1x                   1x       1x                   1x 20x 20x               1x 20x 20x               1x 2x 2x               1x 14419x 14419x                 1x 13070x 13070x 13070x                 1x 15857x 15857x 15857x   15857x             1x                 1x                     1x                 1x                                   1x 111312x 111312x                     1x             1x 1x             1x 16242x 16242x 16242x               1x                 1x 3852x 3852x                         1x         1x 1893x 1893x                                 1x     5x 5x                           1x   1268x 1268x 1268x 1268x 1268x 1268x 1268x 1268x 1268x 1268x 1268x 1268x       1249x 1249x 1249x   1268x 1268x 1268x 1268x 1268x 1268x 1268x   1268x       1266x   1266x 1268x   2x 2x 2x 2x 1268x 1268x 1268x                       1x 19x 19x 19x                       1x 19x 19x             1x                 1x                   1x               1x 1362x 1362x 1362x   1x 1x 119283x 119283x 119283x 119283x 119283x 119283x 119283x 119283x 119283x 29x 119254x 119283x 119283x 119283x   1x 191799x 191799x 191799x 191799x 191799x 191799x 191799x 191799x 191799x   191799x   191799x 191799x 191799x 191799x 191799x 191799x   191799x 191799x 191799x 191799x   191799x 191799x   191799x 191799x 191799x 191799x 191799x                           1x 5540x   5540x 5540x 5540x 5540x 5540x 5540x     4951x 5538x 589x 589x 589x 589x 589x 589x 589x 589x 338x 348x 251x 251x 251x 251x 251x 23x 23x 23x 23x 251x 251x 589x 5540x 5540x       1x 20179x 20179x 20179x 20179x 20179x   20179x 11205x 11205x 11205x 11205x 11205x 20179x 20179x   1x     14720x 14720x 14720x 14720x 14720x 14720x     7475x 7475x 7475x 13990x 13990x 4979x 4979x 4979x 13990x 7475x     14720x 14720x   1x 112x 112x 112x 112x 112x 112x 112x 112x   112x     112x 112x 44x 68x 43x 25x 112x                                                 112x     1x 108720x 108720x 36x 36x 36x 36x 108720x   108720x   108720x 108720x                       1x     31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x   1x   7965x 7965x 7965x 7965x 7965x 7965x 7965x 7965x 7965x 7965x 7965x 7965x 7965x 7965x 7965x 7965x 7965x   7965x   1x                           1x     22692x 20290x 20290x 20290x 20290x 20290x   22692x 22692x 22692x     22692x 22692x 18181x 16973x 15231x 14974x 12286x 10521x 8833x 22692x 14005x           4841x 4841x 13993x       9164x 9164x 9164x 9164x       9164x 9164x   9164x 9164x 15396x 15396x   15396x   15396x 15396x 9164x 14005x 22692x 1x 1x 1x 1x           1x 108x                   108x 1x                                                         1x 1x 1x 1x     1x 51069x   31229x 51069x 19840x 19840x   122x 19840x       200x 200x 200x   200x 200x 200x 200x 200x 200x 19840x 19518x 51069x     1x 3x 3x 3x 1x 1x   72x 4988x 4988x 4988x 4988x     72x 46084x 46084x 46084x 46084x 46084x 46084x 46084x 46084x 46084x 46084x 46084x 46084x 72x 1x 1x                         1x 8004x 8004x                 1x 3246x 3246x                                                           1x 2482x 2482x             1x                 1x     1x 1x 1x       1x                   1x   151x 151x                 1x 210x 210x                             1x 2635x 2635x           1x                   1x 16x 16x 16x                 1x 28x 28x                 1x                     1x                           1x 206x 206x           1x                   1x 112x 112x 112x 112x 112x 112x                   1x                                                                                                                                                                                                                                                                   1x   1x   36x 36x 36x 36x             216x 5725x 5725x 5725x   216x 18929x 18929x 216x 36x 36x 36x 36x 36x 36x 1x   1x     36x   11589x 11589x 11589x 11589x 11589x 11589x 11589x 11589x 11589x 11589x 11589x 11589x 11589x 11589x   11589x   243894x 243894x 243894x 243894x 11589x     36x       31359x 31359x   36x   39282x 39281x 39281x 39281x 39281x 39281x 39281x 39281x 39281x 39281x     39282x 23477x 23477x 23477x 39282x 23483x 23483x 23483x   39281x 39281x 39281x 39281x 39281x 39281x 39281x 39281x 39282x         22563x 22563x 39282x       16718x 16718x           16718x 4662x 4662x 16710x 4676x 4676x 12056x 7380x 7380x 7380x 16718x       16678x 5869x 5869x 5869x   16678x 16678x 16570x 16570x 16570x 16678x 16718x         20x   20x 20x     20x 20x 20x 16718x   39282x 39282x   36x 36x 36x                               7449x 7449x 7449x 7449x 7449x 7449x 7449x 7449x     7449x 7449x 7449x 7449x 7449x 7449x   7449x 7449x 7449x 7449x 7449x 7449x 7449x   7449x 7449x 7449x 7449x     7449x 2177x 2177x 2177x 7449x 7449x 7449x 7449x 945x 12x   12x 933x 6504x 7449x 7449x   7449x 4351x   4327x   24x 4351x 3098x 7449x   86x 86x 7449x 7449x 7449x 7449x 7449x 7449x 7449x 7449x   36x 20758x 20758x 20758x     13859x 13859x 2628x 2628x 2628x 13859x 1447x 1447x   13859x 13859x 13859x 13859x 20758x 20758x   36x 6929x 6929x     6914x 6914x 6914x 6929x 6929x     6929x   6929x 6929x 6929x   35x 6927x   3x 3x     6876x 6876x         6876x   24460x 24460x 24460x 24460x     6876x 6929x   36x 22564x 22564x   36x 7453x 7453x   36x 10x 10x   36x 9225x 9225x   36x 10x 10x   36x 20x 20x                             36x 3675x 3675x 3675x 3675x 3675x 3675x 3675x 3675x 3675x 3675x 3675x 3675x 3675x 3675x 3675x 3675x 3675x 3675x 3675x 3675x 3675x 3675x   3675x 3675x 3675x 3675x 3675x 3675x 36x 36x 1x   1x     36x               8281x 8281x 8281x 8281x               8281x 2268x 2251x 2251x   2251x 2251x 2251x 1344x 1344x 2251x 2268x 8281x   36x         152230x     149756x   149756x 149756x 149756x 149756x 149756x     149756x 149756x 152230x 152230x 152230x       152230x 152230x 152230x 152230x   152230x 152230x 152230x 152230x 152230x   152230x 152230x     149646x 110088x   152230x     110088x 110088x 152230x   1552x 1552x   1552x 152230x   108536x 108536x 108536x   41500x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 4950x 41500x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 36550x 108536x         67036x 66857x 66857x 66857x 66857x 66857x 66857x 66857x 66857x 66857x 66857x 66857x 66857x 66857x 66857x 66857x 67036x     179x 179x 179x 179x 179x 179x 179x 179x 179x 179x 179x 179x 179x 179x 179x 179x 67036x 108536x 110088x 152230x                               36x 149756x 149756x 149756x 149756x   149756x 149756x 149756x   149756x         609x 609x 609x 609x 149756x             149147x 149147x   149147x 278x   148869x 278x   148591x   149147x 149147x 149147x   149756x 149756x         36x 259734x     81201x 259734x     104735x 178533x   73798x 73798x 259734x   36x 185936x 185936x 185936x 305864x 305864x 305864x 146378x 146378x 159486x 159486x 159486x   39558x 185936x               36x 1322x 1322x             1322x 1322x 1322x     1322x 1322x 1322x 5288x 5288x 5288x 5288x     1322x 1322x 1322x   36x       1322x 1322x 1322x 1322x 1322x     1322x     1094x 1094x 1094x 1094x       846x 846x 1094x 1322x   36x 1993x 1993x   1687x 1687x 1993x   36x   5819x 5819x 5819x   5819x 5819x 5780x 5751x 5735x 5819x   5727x 5727x 218x 436x 436x 436x 5727x 5509x 5509x 5509x 5509x 5509x     5509x 5509x 5509x 5509x 5509x 5509x 5509x 5509x       5509x 5509x 5509x 5509x 5509x 5509x 5509x         5509x 5267x 21068x 21068x 21068x 21068x 21068x 21068x 21068x   3752x 3752x 21068x 5267x 5509x 5727x 5819x 5819x   36x 3172x 3172x 8x   8x 8x 3172x 3172x   36x 573x 573x 573x 573x 573x 573x 573x 573x   573x 4875x 4875x 573x 201x 1493x 1493x 201x   573x 573x 4863x 4863x 4863x   3172x 3172x   4863x 4863x 4863x     12862x 12857x 12862x 5815x 5815x 5815x 5815x 12862x 4863x 4863x 568x 573x           36x               5906x 11812x 11812x 11812x 11812x   5906x 5906x 5906x 5906x 5906x 5906x 5906x 5906x 5906x 5906x   5906x 5906x 5906x 5906x       5906x 5906x 2517x 5906x     458x 458x 143x 60x 60x 40x 458x 37x 37x 5906x     1979x 1979x 5905x     1314x 1314x   2613x 2613x     5906x 10021x 10021x 10021x 10021x 10021x   4233x   4233x 2390x 2390x 4233x   10021x 10021x 5903x 2252x 5903x     92x 92x   92x 92x 80x 65x 64x   92x 92x 2613x 5906x                       36x   15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 3x 3x 3x 3x 3x 3x 15x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 15x 15x   36x                     36x 4x 4x 4x   4x     4x   36x 36x   36x 36x 36x 36x 36x 1x 1x   1x  
/*
 * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
 * http://paperjs.org/
 *
 * Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
 * http://juerglehni.com/ & https://puckey.studio/
 *
 * Distributed under the MIT license. See LICENSE file for details.
 *
 * All rights reserved.
 */
 
// TODO: remove eslint-disable comment and deal with errors over time
/* eslint-disable */
 
import { ref } from '~/globals';
import { Base } from '~/straps';
import { Point } from '~/basic/Point';
import { Rectangle } from '~/basic/Rectangle';
import { Numerical } from '~/util/Numerical';
import { CollisionDetection } from '~/util/CollisionDetection';
import { Line } from '~/basic/Line';
import { Segment } from './Segment';
 
/**
 * @name Curve
 *
 * @class The Curve object represents the parts of a path that are connected by
 * two following {@link Segment} objects. The curves of a path can be accessed
 * through its {@link Path#curves} array.
 *
 * While a segment describe the anchor point and its incoming and outgoing
 * handles, a Curve object describes the curve passing between two such
 * segments. Curves and segments represent two different ways of looking at the
 * same thing, but focusing on different aspects. Curves for example offer many
 * convenient ways to work with parts of the path, finding lengths, positions or
 * tangents at given offsets.
 */
export const Curve = Base.extend(
  /** @lends Curve# */ {
    _class: 'Curve',
    // Enforce creation of beans, as some bean getters have hidden parameters.
    // See #getValues() below.
    beans: true,
 
    /**
     * Creates a new curve object.
     *
     * @name Curve#initialize
     * @param {Segment} segment1
     * @param {Segment} segment2
     */
    /**
     * Creates a new curve object.
     *
     * @name Curve#initialize
     * @param {Point} point1
     * @param {Point} handle1
     * @param {Point} handle2
     * @param {Point} point2
     */
    /**
     * Creates a new curve object.
     *
     * @name Curve#initialize
     * @ignore
     * @param {Number} x1
     * @param {Number} y1
     * @param {Number} handle1x
     * @param {Number} handle1y
     * @param {Number} handle2x
     * @param {Number} handle2y
     * @param {Number} x2
     * @param {Number} y2
     */
    initialize: function Curve(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) {
      var count = arguments.length,
        seg1,
        seg2,
        point1,
        point2,
        handle1,
        handle2;
      // The following code has to either set seg1 & seg2,
      // or point1, point2, handle1 & handle2. At the end, the internal
      // segments are created accordingly.
      if (count === 3) {
        // Undocumented internal constructor, used by Path#getCurves()
        // new Segment(path, segment1, segment2);
        this._path = arg0;
        seg1 = arg1;
        seg2 = arg2;
      } else if (!count) {
        seg1 = new Segment();
        seg2 = new Segment();
      } else if (count === 1) {
        // new Segment(segment);
        // NOTE: This copies from existing segments through bean getters
        if ('segment1' in arg0) {
          seg1 = new Segment(arg0.segment1);
          seg2 = new Segment(arg0.segment2);
        } else if ('point1' in arg0) {
          // As printed by #toString()
          point1 = arg0.point1;
          handle1 = arg0.handle1;
          handle2 = arg0.handle2;
          point2 = arg0.point2;
        } else if (Array.isArray(arg0)) {
          // Convert getValues() array back to points and handles so we
          // can create segments for those.
          point1 = [arg0[0], arg0[1]];
          point2 = [arg0[6], arg0[7]];
          handle1 = [arg0[2] - arg0[0], arg0[3] - arg0[1]];
          handle2 = [arg0[4] - arg0[6], arg0[5] - arg0[7]];
        }
      } else if (count === 2) {
        // new Segment(segment1, segment2);
        seg1 = new Segment(arg0);
        seg2 = new Segment(arg1);
      } else if (count === 4) {
        point1 = arg0;
        handle1 = arg1;
        handle2 = arg2;
        point2 = arg3;
      } else if (count === 8) {
        // Convert getValues() array from arguments list back to points and
        // handles so we can create segments for those.
        // NOTE: This could be merged with the above code after the array
        // check through the `arguments` object, but it would break JS
        // optimizations.
        point1 = [arg0, arg1];
        point2 = [arg6, arg7];
        handle1 = [arg2 - arg0, arg3 - arg1];
        handle2 = [arg4 - arg6, arg5 - arg7];
      }
      this._segment1 = seg1 || new Segment(point1, null, handle1);
      this._segment2 = seg2 || new Segment(point2, handle2, null);
    },
 
    _serialize: function (options, dictionary) {
      // If it has no handles, only serialize points, otherwise handles too.
      return Base.serialize(
        this.hasHandles()
          ? [this.getPoint1(), this.getHandle1(), this.getHandle2(), this.getPoint2()]
          : [this.getPoint1(), this.getPoint2()],
        options,
        true,
        dictionary
      );
    },
 
    _changed: function () {
      // Clear cached values.
      this._length = this._bounds = undefined;
    },
 
    /**
     * Returns a copy of the curve.
     *
     * @return {Curve}
     */
    clone: function () {
      return new Curve(this._segment1, this._segment2);
    },
 
    /**
     * @return {String} a string representation of the curve
     */
    toString: function () {
      var parts = ['point1: ' + this._segment1._point];
      if (!this._segment1._handleOut.isZero()) parts.push('handle1: ' + this._segment1._handleOut);
      if (!this._segment2._handleIn.isZero()) parts.push('handle2: ' + this._segment2._handleIn);
      parts.push('point2: ' + this._segment2._point);
      return '{ ' + parts.join(', ') + ' }';
    },
 
    /**
     * Determines the type of cubic Bézier curve via discriminant
     * classification, as well as the curve-time parameters of the associated
     * points of inflection, loops, cusps, etc.
     *
     * @return {Object} the curve classification information as an object, see
     *     options
     * @result info.type {String} the type of Bézier curve, possible values are:
     *     {@values 'line', 'quadratic', 'serpentine', 'cusp', 'loop', 'arch'}
     * @result info.roots {Number[]} the curve-time parameters of the
     *     associated points of inflection for serpentine curves, loops, cusps,
           etc
     */
    classify: function () {
      return Curve.classify(this.getValues());
    },
 
    /**
     * Removes the curve from the path that it belongs to, by removing its
     * second segment and merging its handle with the first segment.
     * @return {Boolean} {@true if the curve was removed}
     */
    remove: function () {
      var removed = false;
      if (this._path) {
        var segment2 = this._segment2,
          handleOut = segment2._handleOut;
        removed = segment2.remove();
        if (removed) this._segment1._handleOut.set(handleOut);
      }
      return removed;
    },
 
    /**
     * The first anchor point of the curve.
     *
     * @bean
     * @type Point
     */
    getPoint1: function () {
      return this._segment1._point;
    },
 
    setPoint1: function (/* point */) {
      this._segment1._point.set(Point.read(arguments));
    },
 
    /**
     * The second anchor point of the curve.
     *
     * @bean
     * @type Point
     */
    getPoint2: function () {
      return this._segment2._point;
    },
 
    setPoint2: function (/* point */) {
      this._segment2._point.set(Point.read(arguments));
    },
 
    /**
     * The handle point that describes the tangent in the first anchor point.
     *
     * @bean
     * @type Point
     */
    getHandle1: function () {
      return this._segment1._handleOut;
    },
 
    setHandle1: function (/* point */) {
      this._segment1._handleOut.set(Point.read(arguments));
    },
 
    /**
     * The handle point that describes the tangent in the second anchor point.
     *
     * @bean
     * @type Point
     */
    getHandle2: function () {
      return this._segment2._handleIn;
    },
 
    setHandle2: function (/* point */) {
      this._segment2._handleIn.set(Point.read(arguments));
    },
 
    /**
     * The first segment of the curve.
     *
     * @bean
     * @type Segment
     */
    getSegment1: function () {
      return this._segment1;
    },
 
    /**
     * The second segment of the curve.
     *
     * @bean
     * @type Segment
     */
    getSegment2: function () {
      return this._segment2;
    },
 
    /**
     * The path that the curve belongs to.
     *
     * @bean
     * @type Path
     */
    getPath: function () {
      return this._path;
    },
 
    /**
     * The index of the curve in the {@link Path#curves} array.
     *
     * @bean
     * @type Number
     */
    getIndex: function () {
      return this._segment1._index;
    },
 
    /**
     * The next curve in the {@link Path#curves} array that the curve
     * belongs to.
     *
     * @bean
     * @type Curve
     */
    getNext: function () {
      var curves = this._path && this._path._curves;
      return (curves && (curves[this._segment1._index + 1] || (this._path._closed && curves[0]))) || null;
    },
 
    /**
     * The previous curve in the {@link Path#curves} array that the curve
     * belongs to.
     *
     * @bean
     * @type Curve
     */
    getPrevious: function () {
      var curves = this._path && this._path._curves;
      return (
        (curves && (curves[this._segment1._index - 1] || (this._path._closed && curves[curves.length - 1]))) || null
      );
    },
 
    /**
     * Checks if the this is the first curve in the {@link Path#curves} array.
     *
     * @return {Boolean} {@true if this is the first curve}
     */
    isFirst: function () {
      return !this._segment1._index;
    },
 
    /**
     * Checks if the this is the last curve in the {@link Path#curves} array.
     *
     * @return {Boolean} {@true if this is the last curve}
     */
    isLast: function () {
      var path = this._path;
      return (path && this._segment1._index === path._curves.length - 1) || false;
    },
 
    /**
     * Specifies whether the points and handles of the curve are selected.
     *
     * @bean
     * @type Boolean
     */
    isSelected: function () {
      return (
        this.getPoint1().isSelected() &&
        this.getHandle1().isSelected() &&
        this.getHandle2().isSelected() &&
        this.getPoint2().isSelected()
      );
    },
 
    setSelected: function (selected) {
      this.getPoint1().setSelected(selected);
      this.getHandle1().setSelected(selected);
      this.getHandle2().setSelected(selected);
      this.getPoint2().setSelected(selected);
    },
 
    /**
     * An array of 8 float values, describing this curve's geometry in four
     * absolute x/y pairs (point1, handle1, handle2, point2). This format is
     * used internally for efficient processing of curve geometries, e.g. when
     * calculating intersections or bounds.
     *
     * Note that the handles are converted to absolute coordinates.
     *
     * @bean
     * @type Number[]
     */
    getValues: function (matrix) {
      return Curve.getValues(this._segment1, this._segment2, matrix);
    },
 
    /**
     * An array of 4 point objects, describing this curve's geometry in absolute
     * coordinates (point1, handle1, handle2, point2).
     *
     * Note that the handles are converted to absolute coordinates.
     *
     * @bean
     * @type Point[]
     */
    getPoints: function () {
      // Convert to array of absolute points
      var coords = this.getValues(),
        points = [];
      for (var i = 0; i < 8; i += 2) points.push(new Point(coords[i], coords[i + 1]));
      return points;
    },
  },
  /** @lends Curve# */ {
    /**
     * The approximated length of the curve.
     *
     * @bean
     * @type Number
     */
    getLength: function () {
      if (this._length == null) this._length = Curve.getLength(this.getValues(), 0, 1);
      return this._length;
    },
 
    /**
     * The area that the curve's geometry is covering.
     *
     * @bean
     * @type Number
     */
    getArea: function () {
      return Curve.getArea(this.getValues());
    },
 
    /**
     * @bean
     * @type Line
     * @private
     */
    getLine: function () {
      return new Line(this._segment1._point, this._segment2._point);
    },
 
    /**
     * Creates a new curve as a sub-curve from this curve, its range defined by
     * the given curve-time parameters. If `from` is larger than `to`, then
     * the resulting curve will have its direction reversed.
     *
     * @param {Number} from the curve-time parameter at which the sub-curve
     * starts
     * @param {Number} to the curve-time parameter at which the sub-curve
     * ends
     * @return {Curve} the newly create sub-curve
     */
    getPart: function (from, to) {
      return new Curve(Curve.getPart(this.getValues(), from, to));
    },
 
    // DOCS: Curve#getPartLength(from, to)
    getPartLength: function (from, to) {
      return Curve.getLength(this.getValues(), from, to);
    },
 
    // TODO: adjustThroughPoint
 
    /**
     * Divides the curve into two curves at the given offset or location. The
     * curve itself is modified and becomes the first part, the second part is
     * returned as a new curve. If the curve belongs to a path item, a new
     * segment is inserted into the path at the given location, and the second
     * part becomes a part of the path as well.
     *
     * @param {Number|CurveLocation} location the offset or location on the
     *     curve at which to divide
     * @return {Curve} the second part of the divided curve if the location is
     *     valid, {code null} otherwise
     * @see #divideAtTime(time)
     */
    divideAt: function (location) {
      // Accept offsets and CurveLocation objects, as well as objects that act
      // like them.
      return this.divideAtTime(location && location.curve === this ? location.time : this.getTimeAt(location));
    },
 
    /**
     * Divides the curve into two curves at the given curve-time parameter. The
     * curve itself is modified and becomes the first part, the second part is
     * returned as a new curve. If the modified curve belongs to a path item,
     * the second part is also added to the path.
     *
     * @param {Number} time the curve-time parameter on the curve at which to
     *     divide
     * @return {Curve} the second part of the divided curve, if the offset is
     *     within the valid range, {code null} otherwise.
     * @see #divideAt(offset)
     */
    divideAtTime: function (time, _setHandles) {
      // Only divide if not at the beginning or end.
      var tMin = /*#=*/ Numerical.CURVETIME_EPSILON,
        tMax = 1 - tMin,
        res = null;
      if (time >= tMin && time <= tMax) {
        var parts = Curve.subdivide(this.getValues(), time),
          left = parts[0],
          right = parts[1],
          setHandles = _setHandles || this.hasHandles(),
          seg1 = this._segment1,
          seg2 = this._segment2,
          path = this._path;
        if (setHandles) {
          // Adjust the handles on the existing segments. The new segment
          // will be inserted between the existing segment1 and segment2:
          // Convert absolute -> relative
          seg1._handleOut._set(left[2] - left[0], left[3] - left[1]);
          seg2._handleIn._set(right[4] - right[6], right[5] - right[7]);
        }
        // Create the new segment:
        var x = left[6],
          y = left[7],
          segment = new Segment(
            new Point(x, y),
            setHandles && new Point(left[4] - x, left[5] - y),
            setHandles && new Point(right[2] - x, right[3] - y)
          );
        // Insert it in the segments list, if needed:
        if (path) {
          // By inserting at seg1.index + 1, we make sure to insert at
          // the end if this curve is a closing curve of a closed path,
          // as with segment2.index it would be inserted at 0.
          path.insert(seg1._index + 1, segment);
          // The newly inserted segment is the start of the next curve:
          res = this.getNext();
        } else {
          // otherwise create it from the result of split
          this._segment2 = segment;
          this._changed();
          res = new Curve(segment, seg2);
        }
      }
      return res;
    },
 
    /**
     * Splits the path this curve belongs to at the given offset. After
     * splitting, the path will be open. If the path was open already, splitting
     * will result in two paths.
     *
     * @param {Number|CurveLocation} location the offset or location on the
     *     curve at which to split
     * @return {Path} the newly created path after splitting, if any
     * @see Path#splitAt(offset)
     */
    splitAt: function (location) {
      var path = this._path;
      return path ? path.splitAt(location) : null;
    },
 
    /**
     * Splits the path this curve belongs to at the given offset. After
     * splitting, the path will be open. If the path was open already, splitting
     * will result in two paths.
     *
     * @param {Number} time the curve-time parameter on the curve at which to
     *     split
     * @return {Path} the newly created path after splitting, if any
     * @see Path#splitAt(offset)
     */
    splitAtTime: function (time) {
      return this.splitAt(this.getLocationAtTime(time));
    },
 
    // TODO: Remove in 1.0.0? (deprecated January 2016):
    /**
     * @deprecated use use {@link #divideAt(offset)} or
     * {@link #divideAtTime(time)} instead.
     */
    divide: function (offset, isTime) {
      return this.divideAtTime(offset === undefined ? 0.5 : isTime ? offset : this.getTimeAt(offset));
    },
 
    // TODO: Remove in 1.0.0? (deprecated January 2016):
    /**
     * @deprecated use use {@link #splitAt(offset)} or
     * {@link #splitAtTime(time)} instead.
     */
    split: function (offset, isTime) {
      return this.splitAtTime(offset === undefined ? 0.5 : isTime ? offset : this.getTimeAt(offset));
    },
 
    /**
     * Returns a reversed version of the curve, without modifying the curve
     * itself.
     *
     * @return {Curve} a reversed version of the curve
     */
    reversed: function () {
      return new Curve(this._segment2.reversed(), this._segment1.reversed());
    },
 
    /**
     * Clears the curve's handles by setting their coordinates to zero,
     * turning the curve into a straight line.
     */
    clearHandles: function () {
      this._segment1._handleOut._set(0, 0);
      this._segment2._handleIn._set(0, 0);
    },
 
    statics: /** @lends Curve */ {
      getValues: function (segment1, segment2, matrix, straight) {
        var p1 = segment1._point,
          h1 = segment1._handleOut,
          h2 = segment2._handleIn,
          p2 = segment2._point,
          x1 = p1.x,
          y1 = p1.y,
          x2 = p2.x,
          y2 = p2.y,
          values = straight
            ? [x1, y1, x1, y1, x2, y2, x2, y2]
            : [x1, y1, x1 + h1._x, y1 + h1._y, x2 + h2._x, y2 + h2._y, x2, y2];
        if (matrix) matrix._transformCoordinates(values, values, 4);
        return values;
      },
 
      subdivide: function (v, t) {
        var x0 = v[0],
          y0 = v[1],
          x1 = v[2],
          y1 = v[3],
          x2 = v[4],
          y2 = v[5],
          x3 = v[6],
          y3 = v[7];
        if (t === undefined) t = 0.5;
        // Triangle computation, with loops unrolled.
        var u = 1 - t,
          // Interpolate from 4 to 3 points
          x4 = u * x0 + t * x1,
          y4 = u * y0 + t * y1,
          x5 = u * x1 + t * x2,
          y5 = u * y1 + t * y2,
          x6 = u * x2 + t * x3,
          y6 = u * y2 + t * y3,
          // Interpolate from 3 to 2 points
          x7 = u * x4 + t * x5,
          y7 = u * y4 + t * y5,
          x8 = u * x5 + t * x6,
          y8 = u * y5 + t * y6,
          // Interpolate from 2 points to 1 point
          x9 = u * x7 + t * x8,
          y9 = u * y7 + t * y8;
        // We now have all the values we need to build the sub-curves:
        return [
          [x0, y0, x4, y4, x7, y7, x9, y9], // left
          [x9, y9, x8, y8, x6, y6, x3, y3], // right
        ];
      },
 
      /**
       * Splits the specified curve values into curves that are monotone in the
       * specified coordinate direction.
       *
       * @param {Number[]} v the curve values, as returned by {@link Curve#values}
       * @param {Boolean} [dir=false] the direction in which the curves should be
       *     monotone, `false`: in x-direction, `true`: in y-direction
       * @return {Number[][]} an array of curve value arrays of the resulting
       *     monotone curve. If the original curve was already monotone, an array
       *     only containing its values are returned.
       * @private
       */
      getMonoCurves: function (v, dir) {
        var curves = [],
          // Determine the ordinate index in the curve values array.
          io = dir ? 0 : 1,
          o0 = v[io + 0],
          o1 = v[io + 2],
          o2 = v[io + 4],
          o3 = v[io + 6];
        if ((o0 >= o1 === o1 >= o2 && o1 >= o2 === o2 >= o3) || Curve.isStraight(v)) {
          // Straight curves and curves with all involved points ordered
          // in coordinate direction are guaranteed to be monotone.
          curves.push(v);
        } else {
          var a = 3 * (o1 - o2) - o0 + o3,
            b = 2 * (o0 + o2) - 4 * o1,
            c = o1 - o0,
            tMin = /*#=*/ Numerical.CURVETIME_EPSILON,
            tMax = 1 - tMin,
            roots = [],
            n = Numerical.solveQuadratic(a, b, c, roots, tMin, tMax);
          if (!n) {
            curves.push(v);
          } else {
            roots.sort();
            var t = roots[0],
              parts = Curve.subdivide(v, t);
            curves.push(parts[0]);
            if (n > 1) {
              t = (roots[1] - t) / (1 - t);
              parts = Curve.subdivide(parts[1], t);
              curves.push(parts[0]);
            }
            curves.push(parts[1]);
          }
        }
        return curves;
      },
 
      // Converts from the point coordinates (p0, p1, p2, p3) for one axis to
      // the polynomial coefficients and solves the polynomial for val
      solveCubic: function (v, coord, val, roots, min, max) {
        var v0 = v[coord],
          v1 = v[coord + 2],
          v2 = v[coord + 4],
          v3 = v[coord + 6],
          res = 0;
        // If val is outside the curve values, no solution is possible.
        if (!((v0 < val && v3 < val && v1 < val && v2 < val) || (v0 > val && v3 > val && v1 > val && v2 > val))) {
          var c = 3 * (v1 - v0),
            b = 3 * (v2 - v1) - c,
            a = v3 - v0 - c - b;
          res = Numerical.solveCubic(a, b, c, v0 - val, roots, min, max);
        }
        return res;
      },
 
      getTimeOf: function (v, point) {
        // Before solving cubics, compare the beginning and end of the curve
        // with zero epsilon:
        var p0 = new Point(v[0], v[1]),
          p3 = new Point(v[6], v[7]),
          epsilon = /*#=*/ Numerical.EPSILON,
          geomEpsilon = /*#=*/ Numerical.GEOMETRIC_EPSILON,
          t = point.isClose(p0, epsilon) ? 0 : point.isClose(p3, epsilon) ? 1 : null;
        if (t === null) {
          // Solve the cubic for both x- and y-coordinates and consider all
          // solutions, testing with the larger / looser geometric epsilon.
          var coords = [point.x, point.y],
            roots = [];
          for (var c = 0; c < 2; c++) {
            var count = Curve.solveCubic(v, c, coords[c], roots, 0, 1);
            for (var i = 0; i < count; i++) {
              var u = roots[i];
              if (point.isClose(Curve.getPoint(v, u), geomEpsilon)) return u;
            }
          }
        }
        // Since we're comparing with geometric epsilon for any other t along
        // the curve, do so as well now for the beginning and end of the curve.
        return point.isClose(p0, geomEpsilon) ? 0 : point.isClose(p3, geomEpsilon) ? 1 : null;
      },
 
      getNearestTime: function (v, point) {
        if (Curve.isStraight(v)) {
          var x0 = v[0],
            y0 = v[1],
            x3 = v[6],
            y3 = v[7],
            vx = x3 - x0,
            vy = y3 - y0,
            det = vx * vx + vy * vy;
          // Avoid divisions by zero.
          if (det === 0) return 0;
          // Project the point onto the line and calculate its linear
          // parameter u along the line: u = (point - p1).dot(v) / v.dot(v)
          var u = ((point.x - x0) * vx + (point.y - y0) * vy) / det;
          return u < /*#=*/ Numerical.EPSILON
            ? 0
            : u > /*#=*/ 1 - Numerical.EPSILON
              ? 1
              : Curve.getTimeOf(v, new Point(x0 + u * vx, y0 + u * vy));
        }
 
        var count = 100,
          minDist = Infinity,
          minT = 0;
 
        function refine(t) {
          if (t >= 0 && t <= 1) {
            var dist = point.getDistance(Curve.getPoint(v, t), true);
            if (dist < minDist) {
              minDist = dist;
              minT = t;
              return true;
            }
          }
        }
 
        for (var i = 0; i <= count; i++) refine(i / count);
 
        // Now iteratively refine solution until we reach desired precision.
        var step = 1 / (count * 2);
        while (step > /*#=*/ Numerical.CURVETIME_EPSILON) {
          if (!refine(minT - step) && !refine(minT + step)) step /= 2;
        }
        return minT;
      },
 
      // TODO: Find better name
      getPart: function (v, from, to) {
        var flip = from > to;
        if (flip) {
          var tmp = from;
          from = to;
          to = tmp;
        }
        if (from > 0) v = Curve.subdivide(v, from)[1]; // [1] right
        // Interpolate the parameter at 'to' in the new curve and cut there.
        if (to < 1) v = Curve.subdivide(v, (to - from) / (1 - from))[0]; // [0] left
        // Return reversed curve if from / to were flipped:
        return flip ? [v[6], v[7], v[4], v[5], v[2], v[3], v[0], v[1]] : v;
      },
 
      /**
       * Determines if a curve is sufficiently flat, meaning it appears as a
       * straight line and has curve-time that is enough linear, as specified by
       * the given `flatness` parameter.
       *
       * @param {Number} flatness the maximum error allowed for the straight line
       *     to deviate from the curve
       *
       * @private
       */
      isFlatEnough: function (v, flatness) {
        // Thanks to Kaspar Fischer and Roger Willcocks for the following:
        // http://hcklbrrfnn.files.wordpress.com/2012/08/bez.pdf
        var x0 = v[0],
          y0 = v[1],
          x1 = v[2],
          y1 = v[3],
          x2 = v[4],
          y2 = v[5],
          x3 = v[6],
          y3 = v[7],
          ux = 3 * x1 - 2 * x0 - x3,
          uy = 3 * y1 - 2 * y0 - y3,
          vx = 3 * x2 - 2 * x3 - x0,
          vy = 3 * y2 - 2 * y3 - y0;
        return Math.max(ux * ux, vx * vx) + Math.max(uy * uy, vy * vy) <= 16 * flatness * flatness;
      },
 
      getArea: function (v) {
        // http://objectmix.com/graphics/133553-area-closed-bezier-curve.html
        var x0 = v[0],
          y0 = v[1],
          x1 = v[2],
          y1 = v[3],
          x2 = v[4],
          y2 = v[5],
          x3 = v[6],
          y3 = v[7];
        return (
          (3 *
            ((y3 - y0) * (x1 + x2) -
              (x3 - x0) * (y1 + y2) +
              y1 * (x0 - x2) -
              x1 * (y0 - y2) +
              y3 * (x2 + x0 / 3) -
              x3 * (y2 + y0 / 3))) /
          20
        );
      },
 
      getBounds: function (v) {
        var min = v.slice(0, 2), // Start with values of point1
          max = min.slice(), // clone
          roots = [0, 0];
        for (var i = 0; i < 2; i++) Curve._addBounds(v[i], v[i + 2], v[i + 4], v[i + 6], i, 0, min, max, roots);
        return new Rectangle(min[0], min[1], max[0] - min[0], max[1] - min[1]);
      },
 
      /**
       * Private helper for both Curve.getBounds() and Path.getBounds(), which
       * finds the 0-crossings of the derivative of a bezier curve polynomial, to
       * determine potential extremas when finding the bounds of a curve.
       * NOTE: padding is only used for Path.getBounds().
       */
      _addBounds: function (v0, v1, v2, v3, coord, padding, min, max, roots) {
        // Code ported and further optimised from:
        // http://blog.hackers-cafe.net/2009/06/how-to-calculate-bezier-curves-bounding.html
        function add(value, padding) {
          var left = value - padding,
            right = value + padding;
          if (left < min[coord]) min[coord] = left;
          if (right > max[coord]) max[coord] = right;
        }
 
        padding /= 2; // strokePadding is in width, not radius
        var minPad = min[coord] + padding,
          maxPad = max[coord] - padding;
        // Perform a rough bounds checking first: The curve can only extend the
        // current bounds if at least one value is outside the min-max range.
        if (
          v0 < minPad ||
          v1 < minPad ||
          v2 < minPad ||
          v3 < minPad ||
          v0 > maxPad ||
          v1 > maxPad ||
          v2 > maxPad ||
          v3 > maxPad
        ) {
          if (v1 < v0 != v1 < v3 && v2 < v0 != v2 < v3) {
            // If the values of a curve are sorted, the extrema are simply
            // the start and end point.
            // Only add strokeWidth to bounds for points which lie within 0
            // < t < 1. The corner cases for cap and join are handled in
            // getStrokeBounds()
            add(v0, 0);
            add(v3, 0);
          } else {
            // Calculate derivative of our bezier polynomial, divided by 3.
            // Doing so allows for simpler calculations of a, b, c and leads
            // to the same quadratic roots.
            var a = 3 * (v1 - v2) - v0 + v3,
              b = 2 * (v0 + v2) - 4 * v1,
              c = v1 - v0,
              count = Numerical.solveQuadratic(a, b, c, roots),
              // Add some tolerance for good roots, as t = 0, 1 are added
              // separately anyhow, and we don't want joins to be added
              // with radii in getStrokeBounds()
              tMin = /*#=*/ Numerical.CURVETIME_EPSILON,
              tMax = 1 - tMin;
            // See above for an explanation of padding = 0 here:
            add(v3, 0);
            for (var i = 0; i < count; i++) {
              var t = roots[i],
                u = 1 - t;
              // Test for good roots and only add to bounds if good.
              if (tMin <= t && t <= tMax)
                // Calculate bezier polynomial at t.
                add(u * u * u * v0 + 3 * u * u * t * v1 + 3 * u * t * t * v2 + t * t * t * v3, padding);
            }
          }
        }
      },
    },
  },
  Base.each(
    ['getBounds', 'getStrokeBounds', 'getHandleBounds'],
    // NOTE: Although Curve.getBounds() exists, we are using Path.getBounds() to
    // determine the bounds of Curve objects with defined segment1 and segment2
    // values Curve.getBounds() can be used directly on curve arrays, without
    // the need to create a Curve object first, as required by the code that
    // finds path interesections.
    function (name) {
      this[name] = function () {
        if (!this._bounds) this._bounds = {};
        var bounds = this._bounds[name];
        if (!bounds) {
          // Calculate the curve bounds by passing a segment list for the
          // curve to the static Path.get*Boudns methods.
          bounds = this._bounds[name] = ref.Path[name]([this._segment1, this._segment2], false, this._path);
        }
        return bounds.clone();
      };
    },
    /** @lends Curve# */ {
      /**
       * {@grouptitle Bounding Boxes}
       *
       * The bounding rectangle of the curve excluding stroke width.
       *
       * @name Curve#bounds
       * @type Rectangle
       */
      /**
       * The bounding rectangle of the curve including stroke width.
       *
       * @name Curve#strokeBounds
       * @type Rectangle
       */
      /**
       * The bounding rectangle of the curve including handles.
       *
       * @name Curve#handleBounds
       * @type Rectangle
       */
      /**
       * The rough bounding rectangle of the curve that is sure to include all of
       * the drawing, including stroke width.
       *
       * @name Curve#roughBounds
       * @type Rectangle
       * @ignore
       */
    }
  ),
  Base.each(
    {
      // Injection scope for tests both as instance and static methods
      // NOTE: Curve#isStraight is documented further down.
      isStraight: function (p1, h1, h2, p2) {
        if (h1.isZero() && h2.isZero()) {
          // No handles.
          return true;
        } else {
          var v = p2.subtract(p1);
          if (v.isZero()) {
            // Zero-length line, with some handles defined.
            return false;
          } else if (v.isCollinear(h1) && v.isCollinear(h2)) {
            // Collinear handles: In addition to v.isCollinear(h) checks, we
            // need to measure the distance to the line, in order to be able
            // to use the same epsilon as in Curve#getTimeOf(), see #1066.
            var l = new Line(p1, p2),
              epsilon = /*#=*/ Numerical.GEOMETRIC_EPSILON;
            if (l.getDistance(p1.add(h1)) < epsilon && l.getDistance(p2.add(h2)) < epsilon) {
              // Project handles onto line to see if they are in range:
              var div = v.dot(v),
                s1 = v.dot(h1) / div,
                s2 = v.dot(h2) / div;
              return s1 >= 0 && s1 <= 1 && s2 <= 0 && s2 >= -1;
            }
          }
        }
        return false;
      },
 
      // NOTE: Curve#isLinear is documented further down.
      isLinear: function (p1, h1, h2, p2) {
        var third = p2.subtract(p1).divide(3);
        return h1.equals(third) && h2.negate().equals(third);
      },
    },
    function (test, name) {
      // Produce the instance version that is called on curve object.
      this[name] = function (epsilon) {
        var seg1 = this._segment1,
          seg2 = this._segment2;
        return test(seg1._point, seg1._handleOut, seg2._handleIn, seg2._point, epsilon);
      };
 
      // Produce the static version that handles a curve values array.
      this.statics[name] = function (v, epsilon) {
        var x0 = v[0],
          y0 = v[1],
          x3 = v[6],
          y3 = v[7];
        return test(
          new Point(x0, y0),
          new Point(v[2] - x0, v[3] - y0),
          new Point(v[4] - x3, v[5] - y3),
          new Point(x3, y3),
          epsilon
        );
      };
    },
    /** @lends Curve# */ {
      statics: {}, // Filled in the Base.each loop above.
 
      /**
       * {@grouptitle Curve Tests}
       *
       * Checks if this curve has any curve handles set.
       *
       * @return {Boolean} {@true if the curve has handles set}
       * @see Curve#handle1
       * @see Curve#handle2
       * @see Segment#hasHandles()
       * @see Path#hasHandles()
       */
      hasHandles: function () {
        return !this._segment1._handleOut.isZero() || !this._segment2._handleIn.isZero();
      },
 
      /**
       * Checks if this curve has any length.
       *
       * @param {Number} [epsilon=0] the epsilon against which to compare the
       *     curve's length
       * @return {Boolean} {@true if the curve is longer than the given epsilon}
       */
      hasLength: function (epsilon) {
        return (!this.getPoint1().equals(this.getPoint2()) || this.hasHandles()) && this.getLength() > (epsilon || 0);
      },
 
      /**
       * Checks if this curve appears as a straight line. This can mean that
       * it has no handles defined, or that the handles run collinear with the
       * line that connects the curve's start and end point, not falling
       * outside of the line.
       *
       * @name Curve#isStraight
       * @function
       * @return {Boolean} {@true if the curve is straight}
       */
 
      /**
       * Checks if this curve is parametrically linear, meaning that it is
       * straight and its handles are positioned at 1/3 and 2/3 of the total
       * length of the curve.
       *
       * @name Curve#isLinear
       * @function
       * @return {Boolean} {@true if the curve is parametrically linear}
       */
 
      /**
       * Checks if the the two curves describe straight lines that are
       * collinear, meaning they run in parallel.
       *
       * @param {Curve} curve the other curve to check against
       * @return {Boolean} {@true if the two lines are collinear}
       */
      isCollinear: function (curve) {
        return curve && this.isStraight() && curve.isStraight() && this.getLine().isCollinear(curve.getLine());
      },
 
      /**
       * Checks if the curve is a straight horizontal line.
       *
       * @return {Boolean} {@true if the line is horizontal}
       */
      isHorizontal: function () {
        return this.isStraight() && Math.abs(this.getTangentAtTime(0.5).y) < /*#=*/ Numerical.TRIGONOMETRIC_EPSILON;
      },
 
      /**
       * Checks if the curve is a straight vertical line.
       *
       * @return {Boolean} {@true if the line is vertical}
       */
      isVertical: function () {
        return this.isStraight() && Math.abs(this.getTangentAtTime(0.5).x) < /*#=*/ Numerical.TRIGONOMETRIC_EPSILON;
      },
    }
  ),
  /** @lends Curve# */ {
    // Explicitly deactivate the creation of beans, as we have functions here
    // that look like bean getters but actually read arguments.
    // See #getTimeOf(), #getLocationOf(), #getNearestLocation(), ...
    beans: false,
 
    /**
     * {@grouptitle Positions on Curves}
     *
     * Calculates the curve location at the specified offset on the curve.
     *
     * @param {Number} offset the offset on the curve
     * @return {CurveLocation} the curve location at the specified the offset
     */
    getLocationAt: function (offset, _isTime) {
      // TODO: Remove _isTime handling in 1.0.0? (deprecated Jan 2016):
      return this.getLocationAtTime(_isTime ? offset : this.getTimeAt(offset));
    },
 
    /**
     * Calculates the curve location at the specified curve-time parameter on
     * the curve.
     *
     * @param {Number} time the curve-time parameter on the curve
     * @return {CurveLocation} the curve location at the specified the location
     */
    getLocationAtTime: function (t) {
      return t != null && t >= 0 && t <= 1 ? new ref.CurveLocation(this, t) : null;
    },
 
    /**
     * Calculates the curve-time parameter of the specified offset on the path,
     * relative to the provided start parameter. If offset is a negative value,
     * the parameter is searched to the left of the start parameter. If no start
     * parameter is provided, a default of `0` for positive values of `offset`
     * and `1` for negative values of `offset`.
     *
     * @param {Number} offset the offset at which to find the curve-time, in
     *     curve length units
     * @param {Number} [start] the curve-time in relation to which the offset is
     *     determined
     * @return {Number} the curve-time parameter at the specified location
     */
    getTimeAt: function (offset, start) {
      return Curve.getTimeAt(this.getValues(), offset, start);
    },
 
    // TODO: Remove in 1.0.0? (deprecated January 2016):
    /**
     * @deprecated use use {@link #getTimeOf(point)} instead.
     */
    getParameterAt: '#getTimeAt',
 
    /**
     * Calculates the curve-time parameters where the curve is tangential to
     * provided tangent. Note that tangents at the start or end are included.
     *
     * @param {Point} tangent the tangent to which the curve must be tangential
     * @return {Number[]} at most two curve-time parameters, where the curve is
     * tangential to the given tangent
     */
    getTimesWithTangent: function (/* tangent */) {
      var tangent = Point.read(arguments);
      return tangent.isZero() ? [] : Curve.getTimesWithTangent(this.getValues(), tangent);
    },
 
    /**
     * Calculates the curve offset at the specified curve-time parameter on
     * the curve.
     *
     * @param {Number} time the curve-time parameter on the curve
     * @return {Number} the curve offset at the specified the location
     */
    getOffsetAtTime: function (t) {
      return this.getPartLength(0, t);
    },
 
    /**
     * Returns the curve location of the specified point if it lies on the
     * curve, `null` otherwise.
     *
     * @param {Point} point the point on the curve
     * @return {CurveLocation} the curve location of the specified point
     */
    getLocationOf: function (/* point */) {
      return this.getLocationAtTime(this.getTimeOf(Point.read(arguments)));
    },
 
    /**
     * Returns the length of the path from its beginning up to up to the
     * specified point if it lies on the path, `null` otherwise.
     *
     * @param {Point} point the point on the path
     * @return {Number} the length of the path up to the specified point
     */
    getOffsetOf: function (/* point */) {
      var loc = this.getLocationOf.apply(this, arguments);
      return loc ? loc.getOffset() : null;
    },
 
    /**
     * Returns the curve-time parameter of the specified point if it lies on the
     * curve, `null` otherwise.
     * Note that if there is more than one possible solution in a
     * self-intersecting curve, the first found result is returned.
     *
     * @param {Point} point the point on the curve
     * @return {Number} the curve-time parameter of the specified point
     */
    getTimeOf: function (/* point */) {
      return Curve.getTimeOf(this.getValues(), Point.read(arguments));
    },
 
    // TODO: Remove in 1.0.0? (deprecated January 2016):
    /**
     * @deprecated use use {@link #getTimeOf(point)} instead.
     */
    getParameterOf: '#getTimeOf',
 
    /**
     * Returns the nearest location on the curve to the specified point.
     *
     * @function
     * @param {Point} point the point for which we search the nearest location
     * @return {CurveLocation} the location on the curve that's the closest to
     * the specified point
     */
    getNearestLocation: function (/* point */) {
      var point = Point.read(arguments),
        values = this.getValues(),
        t = Curve.getNearestTime(values, point),
        pt = Curve.getPoint(values, t);
      return new ref.CurveLocation(this, t, pt, null, point.getDistance(pt));
    },
 
    /**
     * Returns the nearest point on the curve to the specified point.
     *
     * @function
     * @param {Point} point the point for which we search the nearest point
     * @return {Point} the point on the curve that's the closest to the
     * specified point
     */
    getNearestPoint: function (/* point */) {
      var loc = this.getNearestLocation.apply(this, arguments);
      return loc ? loc.getPoint() : loc;
    },
 
    /**
     * Calculates the point on the curve at the given location.
     *
     * @name Curve#getPointAt
     * @function
     * @param {Number|CurveLocation} location the offset or location on the
     *     curve
     * @return {Point} the point on the curve at the given location
     */
 
    /**
     * Calculates the normalized tangent vector of the curve at the given
     * location.
     *
     * @name Curve#getTangentAt
     * @function
     * @param {Number|CurveLocation} location the offset or location on the
     *     curve
     * @return {Point} the normalized tangent of the curve at the given location
     */
 
    /**
     * Calculates the normal vector of the curve at the given location.
     *
     * @name Curve#getNormalAt
     * @function
     * @param {Number|CurveLocation} location the offset or location on the
     *     curve
     * @return {Point} the normal of the curve at the given location
     */
 
    /**
     * Calculates the weighted tangent vector of the curve at the given
     * location, its length reflecting the curve velocity at that location.
     *
     * @name Curve#getWeightedTangentAt
     * @function
     * @param {Number|CurveLocation} location the offset or location on the
     *     curve
     * @return {Point} the weighted tangent of the curve at the given location
     */
 
    /**
     * Calculates the weighted normal vector of the curve at the given location,
     * its length reflecting the curve velocity at that location.
     *
     * @name Curve#getWeightedNormalAt
     * @function
     * @param {Number|CurveLocation} location the offset or location on the
     *     curve
     * @return {Point} the weighted normal of the curve at the given location
     */
 
    /**
     * Calculates the curvature of the curve at the given location. Curvatures
     * indicate how sharply a curve changes direction. A straight line has zero
     * curvature, where as a circle has a constant curvature. The curve's radius
     * at the given location is the reciprocal value of its curvature.
     *
     * @name Curve#getCurvatureAt
     * @function
     * @param {Number|CurveLocation} location the offset or location on the
     *     curve
     * @return {Number} the curvature of the curve at the given location
     */
 
    /**
     * Calculates the point on the curve at the given location.
     *
     * @name Curve#getPointAtTime
     * @function
     * @param {Number} time the curve-time parameter on the curve
     * @return {Point} the point on the curve at the given location
     */
 
    /**
     * Calculates the normalized tangent vector of the curve at the given
     * location.
     *
     * @name Curve#getTangentAtTime
     * @function
     * @param {Number} time the curve-time parameter on the curve
     * @return {Point} the normalized tangent of the curve at the given location
     */
 
    /**
     * Calculates the normal vector of the curve at the given location.
     *
     * @name Curve#getNormalAtTime
     * @function
     * @param {Number} time the curve-time parameter on the curve
     * @return {Point} the normal of the curve at the given location
     */
 
    /**
     * Calculates the weighted tangent vector of the curve at the given
     * location, its length reflecting the curve velocity at that location.
     *
     * @name Curve#getWeightedTangentAtTime
     * @function
     * @param {Number} time the curve-time parameter on the curve
     * @return {Point} the weighted tangent of the curve at the given location
     */
 
    /**
     * Calculates the weighted normal vector of the curve at the given location,
     * its length reflecting the curve velocity at that location.
     *
     * @name Curve#getWeightedNormalAtTime
     * @function
     * @param {Number} time the curve-time parameter on the curve
     * @return {Point} the weighted normal of the curve at the given location
     */
 
    /**
     * Calculates the curvature of the curve at the given location. Curvatures
     * indicate how sharply a curve changes direction. A straight line has zero
     * curvature, where as a circle has a constant curvature. The curve's radius
     * at the given location is the reciprocal value of its curvature.
     *
     * @name Curve#getCurvatureAtTime
     * @function
     * @param {Number} time the curve-time parameter on the curve
     * @return {Number} the curvature of the curve at the given location
     */
  },
  // @ts-expect-error = 'new' expression, whose target lacks a construct signature
  new (function () {
    // Injection scope for various curve evaluation methods
    var methods = ['getPoint', 'getTangent', 'getNormal', 'getWeightedTangent', 'getWeightedNormal', 'getCurvature'];
    return Base.each(
      methods,
      function (name) {
        // NOTE: (For easier searching): This loop produces:
        // getPointAt, getTangentAt, getNormalAt, getWeightedTangentAt,
        // getWeightedNormalAt, getCurvatureAt, getPointAtTime,
        // getTangentAtTime, getNormalAtTime, getWeightedTangentAtTime,
        // getWeightedNormalAtTime, getCurvatureAtTime
        // TODO: Remove _isTime handling in 1.0.0? (deprecated Jan 2016):
        this[name + 'At'] = function (location, _isTime) {
          var values = this.getValues();
          return Curve[name](values, _isTime ? location : Curve.getTimeAt(values, location));
        };
 
        this[name + 'AtTime'] = function (time) {
          return Curve[name](this.getValues(), time);
        };
      },
      {
        statics: {
          _evaluateMethods: methods,
        },
      }
    );
  })(),
  // @ts-expect-error = 'new' expression, whose target lacks a construct signature
  new (function () {
    // Scope for methods that require private functions
 
    function getLengthIntegrand(v) {
      // Calculate the coefficients of a Bezier derivative.
      var x0 = v[0],
        y0 = v[1],
        x1 = v[2],
        y1 = v[3],
        x2 = v[4],
        y2 = v[5],
        x3 = v[6],
        y3 = v[7],
        ax = 9 * (x1 - x2) + 3 * (x3 - x0),
        bx = 6 * (x0 + x2) - 12 * x1,
        cx = 3 * (x1 - x0),
        ay = 9 * (y1 - y2) + 3 * (y3 - y0),
        by = 6 * (y0 + y2) - 12 * y1,
        cy = 3 * (y1 - y0);
 
      return function (t) {
        // Calculate quadratic equations of derivatives for x and y
        var dx = (ax * t + bx) * t + cx,
          dy = (ay * t + by) * t + cy;
        return Math.sqrt(dx * dx + dy * dy);
      };
    }
 
    // Amount of integral evaluations for the interval 0 <= a < b <= 1
    function getIterations(a, b) {
      // Guess required precision based and size of range...
      // TODO: There should be much better educated guesses for
      // this. Also, what does this depend on? Required precision?
      return Math.max(2, Math.min(16, Math.ceil(Math.abs(b - a) * 32)));
    }
 
    function evaluate(v, t, type, normalized) {
      // Do not produce results if parameter is out of range or invalid.
      if (t == null || t < 0 || t > 1) return null;
      var x0 = v[0],
        y0 = v[1],
        x1 = v[2],
        y1 = v[3],
        x2 = v[4],
        y2 = v[5],
        x3 = v[6],
        y3 = v[7],
        isZero = Numerical.isZero;
      // If the curve handles are almost zero, reset the control points to the
      // anchors.
      if (isZero(x1 - x0) && isZero(y1 - y0)) {
        x1 = x0;
        y1 = y0;
      }
      if (isZero(x2 - x3) && isZero(y2 - y3)) {
        x2 = x3;
        y2 = y3;
      }
      // Calculate the polynomial coefficients.
      var cx = 3 * (x1 - x0),
        bx = 3 * (x2 - x1) - cx,
        ax = x3 - x0 - cx - bx,
        cy = 3 * (y1 - y0),
        by = 3 * (y2 - y1) - cy,
        ay = y3 - y0 - cy - by,
        x,
        y;
      if (type === 0) {
        // type === 0: getPoint()
        // Calculate the curve point at parameter value t
        // Use special handling at t === 0 / 1, to avoid imprecisions.
        // See #960
        x = t === 0 ? x0 : t === 1 ? x3 : ((ax * t + bx) * t + cx) * t + x0;
        y = t === 0 ? y0 : t === 1 ? y3 : ((ay * t + by) * t + cy) * t + y0;
      } else {
        // type === 1: getTangent()
        // type === 2: getNormal()
        // type === 3: getCurvature()
        var tMin = /*#=*/ Numerical.CURVETIME_EPSILON,
          tMax = 1 - tMin;
        // 1: tangent, 1st derivative
        // 2: normal, 1st derivative
        // 3: curvature, 1st derivative & 2nd derivative
        // Prevent tangents and normals of length 0:
        // https://stackoverflow.com/questions/10506868/
        if (t < tMin) {
          x = cx;
          y = cy;
        } else if (t > tMax) {
          x = 3 * (x3 - x2);
          y = 3 * (y3 - y2);
        } else {
          x = (3 * ax * t + 2 * bx) * t + cx;
          y = (3 * ay * t + 2 * by) * t + cy;
        }
        if (normalized) {
          // When the tangent at t is zero and we're at the beginning
          // or the end, we can use the vector between the handles,
          // but only when normalizing as its weighted length is 0.
          if (x === 0 && y === 0 && (t < tMin || t > tMax)) {
            x = x2 - x1;
            y = y2 - y1;
          }
          // Now normalize x & y
          var len = Math.sqrt(x * x + y * y);
          if (len) {
            x /= len;
            y /= len;
          }
        }
        if (type === 3) {
          // Calculate 2nd derivative, and curvature from there:
          // http://cagd.cs.byu.edu/~557/text/ch2.pdf page#31
          // k = |dx * d2y - dy * d2x| / (( dx^2 + dy^2 )^(3/2))
          // @ts-expect-error = Subsequent variable declarations must have the same type
          var x2 = 6 * ax * t + 2 * bx,
            // @ts-expect-error = Subsequent variable declarations must have the same type
            y2 = 6 * ay * t + 2 * by,
            d = Math.pow(x * x + y * y, 3 / 2);
          // For JS optimizations we always return a Point, although
          // curvature is just a numeric value, stored in x:
          x = d !== 0 ? (x * y2 - y * x2) / d : 0;
          y = 0;
        }
      }
      // The normal is simply the rotated tangent:
      return type === 2 ? new Point(y, -x) : new Point(x, y);
    }
 
    return {
      statics: {
        classify: function (v) {
          // See: Loop and Blinn, 2005, Resolution Independent Curve Rendering
          // using Programmable Graphics Hardware, GPU Gems 3 chapter 25
          //
          // Possible types:
          //   'line'       (d1 == d2 == d3 == 0)
          //   'quadratic'  (d1 == d2 == 0)
          //   'serpentine' (d > 0)
          //   'cusp'       (d == 0)
          //   'loop'       (d < 0)
          //   'arch'       (serpentine, cusp or loop with roots outside 0..1)
          //
          // NOTE: Roots for serpentine, cusp and loop curves are only
          // considered if they are within 0..1. If the roots are outside,
          // then we degrade the type of curve down to an 'arch'.
 
          var x0 = v[0],
            y0 = v[1],
            x1 = v[2],
            y1 = v[3],
            x2 = v[4],
            y2 = v[5],
            x3 = v[6],
            y3 = v[7],
            // Calculate coefficients of I(s, t), of which the roots are
            // inflection points.
            a1 = x0 * (y3 - y2) + y0 * (x2 - x3) + x3 * y2 - y3 * x2,
            a2 = x1 * (y0 - y3) + y1 * (x3 - x0) + x0 * y3 - y0 * x3,
            a3 = x2 * (y1 - y0) + y2 * (x0 - x1) + x1 * y0 - y1 * x0,
            d3 = 3 * a3,
            d2 = d3 - a2,
            d1 = d2 - a2 + a1,
            // Normalize the vector (d1, d2, d3) to keep error consistent.
            l = Math.sqrt(d1 * d1 + d2 * d2 + d3 * d3),
            s = l !== 0 ? 1 / l : 0,
            isZero = Numerical.isZero,
            serpentine = 'serpentine'; // short-cut
          d1 *= s;
          d2 *= s;
          d3 *= s;
 
          function type(type, t1, t2) {
            var hasRoots = t1 !== undefined,
              t1Ok = hasRoots && t1 > 0 && t1 < 1,
              t2Ok = hasRoots && t2 > 0 && t2 < 1;
            // Degrade to arch for serpentine, cusp or loop if no solutions
            // within 0..1 are found. loop requires 2 solutions to be valid.
            if (hasRoots && (!(t1Ok || t2Ok) || (type === 'loop' && !(t1Ok && t2Ok)))) {
              type = 'arch';
              t1Ok = t2Ok = false;
            }
            return {
              type: type,
              roots:
                t1Ok || t2Ok
                  ? t1Ok && t2Ok
                    ? t1 < t2
                      ? [t1, t2]
                      : [t2, t1] // 2 solutions
                    : [t1Ok ? t1 : t2] // 1 solution
                  : null,
            };
          }
 
          if (isZero(d1)) {
            return isZero(d2)
              ? // @ts-expect-error = Expected 3 arguments, but got 1
                type(isZero(d3) ? 'line' : 'quadratic') // 5. / 4.
              : // @ts-expect-error = Expected 3 arguments, but got 2
                type(serpentine, d3 / (3 * d2)); // 3b.
          }
          var d = 3 * d2 * d2 - 4 * d1 * d3;
          if (isZero(d)) {
            // @ts-expect-error = Expected 3 arguments, but got 2
            return type('cusp', d2 / (2 * d1)); // 3a.
          }
          var f1 = d > 0 ? Math.sqrt(d / 3) : Math.sqrt(-d),
            f2 = 2 * d1;
          return type(
            d > 0 ? serpentine : 'loop', // 1. / 2.
            (d2 + f1) / f2,
            (d2 - f1) / f2
          );
        },
 
        getLength: function (v, a, b, ds) {
          if (a === undefined) a = 0;
          if (b === undefined) b = 1;
          if (Curve.isStraight(v)) {
            // Sub-divide the linear curve at a and b, so we can simply
            // calculate the Pythagorean Theorem to get the range's length.
            var c = v;
            if (b < 1) {
              c = Curve.subdivide(c, b)[0]; // left
              a /= b; // Scale parameter to new sub-curve.
            }
            if (a > 0) {
              c = Curve.subdivide(c, a)[1]; // right
            }
            // The length of straight curves can be calculated more easily.
            var dx = c[6] - c[0], // x3 - x0
              dy = c[7] - c[1]; // y3 - y0
            return Math.sqrt(dx * dx + dy * dy);
          }
          return Numerical.integrate(ds || getLengthIntegrand(v), a, b, getIterations(a, b));
        },
 
        getTimeAt: function (v, offset, start) {
          if (start === undefined) start = offset < 0 ? 1 : 0;
          if (offset === 0) return start;
          // See if we're going forward or backward, and handle cases
          // differently
          var abs = Math.abs,
            epsilon = /*#=*/ Numerical.EPSILON,
            forward = offset > 0,
            a = forward ? start : 0,
            b = forward ? 1 : start,
            // Use integrand to calculate both range length and part
            // lengths in f(t) below.
            ds = getLengthIntegrand(v),
            // Get length of total range
            rangeLength = Curve.getLength(v, a, b, ds),
            diff = abs(offset) - rangeLength;
          if (abs(diff) < epsilon) {
            // Matched the end:
            return forward ? b : a;
          } else if (diff > epsilon) {
            // We're out of bounds.
            return null;
          }
          // Use offset / rangeLength for an initial guess for t, to
          // bring us closer:
          var guess = offset / rangeLength,
            length = 0;
          // Iteratively calculate curve range lengths, and add them up,
          // using integration precision depending on the size of the
          // range. This is much faster and also more precise than not
          // modifying start and calculating total length each time.
          function f(t) {
            // When start > t, the integration returns a negative value.
            length += Numerical.integrate(ds, start, t, getIterations(start, t));
            start = t;
            return length - offset;
          }
          // Start with out initial guess for x.
          // NOTE: guess is a negative value when looking backwards.
          return Numerical.findRoot(f, ds, start + guess, a, b, 32, /*#=*/ Numerical.EPSILON);
        },
 
        getPoint: function (v, t) {
          return evaluate(v, t, 0, false);
        },
 
        getTangent: function (v, t) {
          return evaluate(v, t, 1, true);
        },
 
        getWeightedTangent: function (v, t) {
          return evaluate(v, t, 1, false);
        },
 
        getNormal: function (v, t) {
          return evaluate(v, t, 2, true);
        },
 
        getWeightedNormal: function (v, t) {
          return evaluate(v, t, 2, false);
        },
 
        getCurvature: function (v, t) {
          return evaluate(v, t, 3, false).x;
        },
 
        /**
         * Returns the t values for the "peaks" of the curve. The peaks are
         * calculated by finding the roots of the dot product of the first and
         * second derivative.
         *
         * Peaks are locations sharing some qualities of curvature extrema but
         * are cheaper to compute. They fulfill their purpose here quite well.
         * See:
         * https://math.stackexchange.com/questions/1954845/bezier-curvature-extrema
         *
         * @param {Number[]} v the curve values array
         * @return {Number[]} the roots of all found peaks
         */
        getPeaks: function (v) {
          var x0 = v[0],
            y0 = v[1],
            x1 = v[2],
            y1 = v[3],
            x2 = v[4],
            y2 = v[5],
            x3 = v[6],
            y3 = v[7],
            ax = -x0 + 3 * x1 - 3 * x2 + x3,
            bx = 3 * x0 - 6 * x1 + 3 * x2,
            cx = -3 * x0 + 3 * x1,
            ay = -y0 + 3 * y1 - 3 * y2 + y3,
            by = 3 * y0 - 6 * y1 + 3 * y2,
            cy = -3 * y0 + 3 * y1,
            tMin = /*#=*/ Numerical.CURVETIME_EPSILON,
            tMax = 1 - tMin,
            roots = [];
          Numerical.solveCubic(
            9 * (ax * ax + ay * ay),
            9 * (ax * bx + by * ay),
            2 * (bx * bx + by * by) + 3 * (cx * ax + cy * ay),
            cx * bx + by * cy,
            // Exclude 0 and 1 as we don't count them as peaks.
            roots,
            tMin,
            tMax
          );
          return roots.sort();
        },
      },
    };
  })(),
  // @ts-expect-error = 'new' expression, whose target lacks a construct signature
  new (function () {
    // Scope for bezier intersection using fat-line clipping
 
    function addLocation(locations, include, c1, t1, c2, t2, overlap) {
      // Determine if locations at the beginning / end of the curves should be
      // excluded, in case the two curves are neighbors, but do not exclude
      // connecting points between two curves if they were part of overlap
      // checks, as they could be self-overlapping.
      // NOTE: We don't pass p1 and p2, because v1 and v2 may be transformed
      // by their path.matrix, while c1 and c2 are untransformed. Passing null
      // for point in CurveLocation() will do the right thing.
      var excludeStart = !overlap && c1.getPrevious() === c2,
        excludeEnd = !overlap && c1 !== c2 && c1.getNext() === c2,
        tMin = /*#=*/ Numerical.CURVETIME_EPSILON,
        tMax = 1 - tMin;
      // Check t1 and t2 against correct bounds, based on excludeStart/End:
      // - excludeStart means the start of c1 connects to the end of c2
      // - excludeEnd means the end of c1 connects to the start of c2
      // - If either c1 or c2 are at the end of the path, exclude their end,
      //   which connects back to the beginning, but only if it's not part of
      //   a found overlap. The normal intersection will already be found at
      //   the beginning, and would be added twice otherwise.
      if (t1 !== null && t1 >= (excludeStart ? tMin : 0) && t1 <= (excludeEnd ? tMax : 1)) {
        if (t2 !== null && t2 >= (excludeEnd ? tMin : 0) && t2 <= (excludeStart ? tMax : 1)) {
          var loc1 = new ref.CurveLocation(c1, t1, null, overlap),
            loc2 = new ref.CurveLocation(c2, t2, null, overlap);
          // Link the two locations to each other.
          loc1._intersection = loc2;
          loc2._intersection = loc1;
          if (!include || include(loc1)) {
            ref.CurveLocation.insert(locations, loc1, true);
          }
        }
      }
    }
 
    function addCurveIntersections(v1, v2, c1, c2, locations, include, flip, recursion, calls, tMin, tMax, uMin, uMax) {
      // Avoid deeper recursion, by counting the total amount of recursions,
      // as well as the total amount of calls, to avoid massive call-trees as
      // suggested by @iconexperience in #904#issuecomment-225283430.
      // See also: #565 #899 #1074
      if (++calls >= 4096 || ++recursion >= 40) return calls;
      // Use an epsilon smaller than CURVETIME_EPSILON to compare curve-time
      // parameters in fat-line clipping code.
      var fatLineEpsilon = 1e-9,
        // Let P be the first curve and Q be the second
        q0x = v2[0],
        q0y = v2[1],
        q3x = v2[6],
        q3y = v2[7],
        getSignedDistance = Line.getSignedDistance,
        // Calculate the fat-line L for Q is the baseline l and two
        // offsets which completely encloses the curve P.
        d1 = getSignedDistance(q0x, q0y, q3x, q3y, v2[2], v2[3]),
        d2 = getSignedDistance(q0x, q0y, q3x, q3y, v2[4], v2[5]),
        factor = d1 * d2 > 0 ? 3 / 4 : 4 / 9,
        dMin = factor * Math.min(0, d1, d2),
        dMax = factor * Math.max(0, d1, d2),
        // Calculate non-parametric bezier curve D(ti, di(t)):
        // - di(t) is the distance of P from baseline l of the fat-line
        // - ti is equally spaced in [0, 1]
        dp0 = getSignedDistance(q0x, q0y, q3x, q3y, v1[0], v1[1]),
        dp1 = getSignedDistance(q0x, q0y, q3x, q3y, v1[2], v1[3]),
        dp2 = getSignedDistance(q0x, q0y, q3x, q3y, v1[4], v1[5]),
        dp3 = getSignedDistance(q0x, q0y, q3x, q3y, v1[6], v1[7]),
        // Get the top and bottom parts of the convex-hull
        hull = getConvexHull(dp0, dp1, dp2, dp3),
        top = hull[0],
        bottom = hull[1],
        tMinClip,
        tMaxClip;
      // Stop iteration if all points and control points are collinear.
      if (
        (d1 === 0 && d2 === 0 && dp0 === 0 && dp1 === 0 && dp2 === 0 && dp3 === 0) ||
        // Clip convex-hull with dMin and dMax, taking into account that
        // there will be no intersections if one of the results is null.
        (tMinClip = clipConvexHull(top, bottom, dMin, dMax)) == null ||
        (tMaxClip = clipConvexHull(top.reverse(), bottom.reverse(), dMin, dMax)) == null
      )
        return calls;
      // tMin and tMax are within the range (0, 1). Project it back to the
      // original parameter range for v2.
      var tMinNew = tMin + (tMax - tMin) * tMinClip,
        tMaxNew = tMin + (tMax - tMin) * tMaxClip;
      if (Math.max(uMax - uMin, tMaxNew - tMinNew) < fatLineEpsilon) {
        // We have isolated the intersection with sufficient precision
        var t = (tMinNew + tMaxNew) / 2,
          u = (uMin + uMax) / 2;
        // @ts-expect-error = Expected 7 arguments, but got 6.
        addLocation(locations, include, flip ? c2 : c1, flip ? u : t, flip ? c1 : c2, flip ? t : u);
      } else {
        // Apply the result of the clipping to curve 1:
        v1 = Curve.getPart(v1, tMinClip, tMaxClip);
        var uDiff = uMax - uMin;
        if (tMaxClip - tMinClip > 0.8) {
          // Subdivide the curve which has converged the least.
          if (tMaxNew - tMinNew > uDiff) {
            var parts = Curve.subdivide(v1, 0.5),
              t = (tMinNew + tMaxNew) / 2;
            calls = addCurveIntersections(
              v2,
              parts[0],
              c2,
              c1,
              locations,
              include,
              !flip,
              recursion,
              calls,
              uMin,
              uMax,
              tMinNew,
              t
            );
            calls = addCurveIntersections(
              v2,
              parts[1],
              c2,
              c1,
              locations,
              include,
              !flip,
              recursion,
              calls,
              uMin,
              uMax,
              t,
              tMaxNew
            );
          } else {
            var parts = Curve.subdivide(v2, 0.5),
              u = (uMin + uMax) / 2;
            calls = addCurveIntersections(
              parts[0],
              v1,
              c2,
              c1,
              locations,
              include,
              !flip,
              recursion,
              calls,
              uMin,
              u,
              tMinNew,
              tMaxNew
            );
            calls = addCurveIntersections(
              parts[1],
              v1,
              c2,
              c1,
              locations,
              include,
              !flip,
              recursion,
              calls,
              u,
              uMax,
              tMinNew,
              tMaxNew
            );
          }
        } else {
          // Iterate
          // For some unclear reason we need to check against uDiff === 0
          // here, to prevent a regression from happening, see #1638.
          // Maybe @iconexperience could shed some light on this.
          if (uDiff === 0 || uDiff >= fatLineEpsilon) {
            calls = addCurveIntersections(
              v2,
              v1,
              c2,
              c1,
              locations,
              include,
              !flip,
              recursion,
              calls,
              uMin,
              uMax,
              tMinNew,
              tMaxNew
            );
          } else {
            // The interval on the other curve is already tight enough,
            // therefore we keep iterating on the same curve.
            calls = addCurveIntersections(
              v1,
              v2,
              c1,
              c2,
              locations,
              include,
              flip,
              recursion,
              calls,
              tMinNew,
              tMaxNew,
              uMin,
              uMax
            );
          }
        }
      }
      return calls;
    }
 
    /**
     * Calculate the convex hull for the non-parametric bezier curve D(ti, di(t))
     * The ti is equally spaced across [0..1] — [0, 1/3, 2/3, 1] for
     * di(t), [dq0, dq1, dq2, dq3] respectively. In other words our CVs for the
     * curve are already sorted in the X axis in the increasing order.
     * Calculating convex-hull is much easier than a set of arbitrary points.
     *
     * The convex-hull is returned as two parts [TOP, BOTTOM]:
     * (both are in a coordinate space where y increases upwards with origin at
     * bottom-left)
     * TOP: The part that lies above the 'median' (line connecting end points of
     * the curve)
     * BOTTOM: The part that lies below the median.
     */
    function getConvexHull(dq0, dq1, dq2, dq3) {
      var p0 = [0, dq0],
        p1 = [1 / 3, dq1],
        p2 = [2 / 3, dq2],
        p3 = [1, dq3],
        // Find vertical signed distance of p1 and p2 from line [p0, p3]
        dist1 = dq1 - (2 * dq0 + dq3) / 3,
        dist2 = dq2 - (dq0 + 2 * dq3) / 3,
        hull;
      // Check if p1 and p2 are on the opposite side of the line [p0, p3]
      if (dist1 * dist2 < 0) {
        // p1 and p2 lie on different sides of [p0, p3]. The hull is a
        // quadrilateral and line [p0, p3] is NOT part of the hull so we are
        // pretty much done here. The top part includes p1, we will reverse
        // it later if that is not the case.
        hull = [
          [p0, p1, p3],
          [p0, p2, p3],
        ];
      } else {
        // p1 and p2 lie on the same sides of [p0, p3]. The hull can be a
        // triangle or a quadrilateral and line [p0, p3] is part of the
        // hull. Check if the hull is a triangle or a quadrilateral. We have
        // a triangle if the vertical distance of one of the middle points
        // (p1, p2) is equal or less than half the vertical distance of the
        // other middle point.
        var distRatio = dist1 / dist2;
        hull = [
          // p2 is inside, the hull is a triangle.
          distRatio >= 2
            ? [p0, p1, p3]
            : // p1 is inside, the hull is a triangle.
              distRatio <= 0.5
              ? [p0, p2, p3]
              : // Hull is a quadrilateral, we need all lines in correct order.
                [p0, p1, p2, p3],
          // Line [p0, p3] is part of the hull.
          [p0, p3],
        ];
      }
      // Flip hull if dist1 is negative or if it is zero and dist2 is negative
      return (dist1 || dist2) < 0 ? hull.reverse() : hull;
    }
 
    /**
     * Clips the convex-hull and returns [tMin, tMax] for the curve contained.
     */
    function clipConvexHull(hullTop, hullBottom, dMin, dMax) {
      if (hullTop[0][1] < dMin) {
        // Left of hull is below dMin, walk through the hull until it
        // enters the region between dMin and dMax
        return clipConvexHullPart(hullTop, true, dMin);
      } else if (hullBottom[0][1] > dMax) {
        // Left of hull is above dMax, walk through the hull until it
        // enters the region between dMin and dMax
        return clipConvexHullPart(hullBottom, false, dMax);
      } else {
        // Left of hull is between dMin and dMax, no clipping possible
        return hullTop[0][0];
      }
    }
 
    function clipConvexHullPart(part, top, threshold) {
      var px = part[0][0],
        py = part[0][1];
      for (var i = 1, l = part.length; i < l; i++) {
        var qx = part[i][0],
          qy = part[i][1];
        if (top ? qy >= threshold : qy <= threshold) {
          return qy === threshold ? qx : px + ((threshold - py) * (qx - px)) / (qy - py);
        }
        px = qx;
        py = qy;
      }
      // All points of hull are above / below the threshold
      return null;
    }
 
    /**
     * Intersections between curve and line becomes rather simple here mostly
     * because of Numerical class. We can rotate the curve and line so that the
     * line is on the X axis, and solve the implicit equations for the X axis
     * and the curve.
     */
    function getCurveLineIntersections(v, px, py, vx, vy) {
      var isZero = Numerical.isZero;
      if (isZero(vx) && isZero(vy)) {
        // Handle special case of a line with no direction as a point,
        // and check if it is on the curve.
        var t = Curve.getTimeOf(v, new Point(px, py));
        return t === null ? [] : [t];
      }
      // Calculate angle to the x-axis (1, 0).
      var angle = Math.atan2(-vy, vx),
        sin = Math.sin(angle),
        cos = Math.cos(angle),
        // (rlx1, rly1) = (0, 0)
        // Calculate the curve values of the rotated curve.
        rv = [],
        roots = [];
      for (var i = 0; i < 8; i += 2) {
        var x = v[i] - px,
          y = v[i + 1] - py;
        rv.push(x * cos - y * sin, x * sin + y * cos);
      }
      // Solve it for y = 0. We need to include t = 0, 1 and let addLocation()
      // do the filtering, to catch important edge cases.
      Curve.solveCubic(rv, 1, 0, roots, 0, 1);
      return roots;
    }
 
    function addCurveLineIntersections(v1, v2, c1, c2, locations, include, flip) {
      // addCurveLineIntersections() is called so that v1 is always the curve
      // and v2 the line. flip indicates whether the curves need to be flipped
      // in the call to addLocation().
      var x1 = v2[0],
        y1 = v2[1],
        x2 = v2[6],
        y2 = v2[7],
        roots = getCurveLineIntersections(v1, x1, y1, x2 - x1, y2 - y1);
      // NOTE: count could be -1 for infinite solutions, but that should only
      // happen with lines, in which case we should not be here.
      for (var i = 0, l = roots.length; i < l; i++) {
        // For each found solution on the rotated curve, get the point on
        // the real curve and with that the location on the line.
        var t1 = roots[i],
          p1 = Curve.getPoint(v1, t1),
          t2 = Curve.getTimeOf(v2, p1);
        if (t2 !== null) {
          // Only use the time values if there was no recursion, and let
          // addLocation() figure out the actual time values otherwise.
          // @ts-expect-error = Expected 7 arguments, but got 6
          addLocation(locations, include, flip ? c2 : c1, flip ? t2 : t1, flip ? c1 : c2, flip ? t1 : t2);
        }
      }
    }
 
    function addLineIntersection(v1, v2, c1, c2, locations, include) {
      var pt = Line.intersect(v1[0], v1[1], v1[6], v1[7], v2[0], v2[1], v2[6], v2[7]);
      if (pt) {
        // @ts-expect-error = Expected 7 arguments, but got 6
        addLocation(locations, include, c1, Curve.getTimeOf(v1, pt), c2, Curve.getTimeOf(v2, pt));
      }
    }
 
    function getCurveIntersections(v1, v2, c1, c2, locations, include) {
      // Avoid checking curves if completely out of control bounds.
      var epsilon = /*#=*/ Numerical.EPSILON,
        min = Math.min,
        max = Math.max;
 
      if (
        max(v1[0], v1[2], v1[4], v1[6]) + epsilon > min(v2[0], v2[2], v2[4], v2[6]) &&
        min(v1[0], v1[2], v1[4], v1[6]) - epsilon < max(v2[0], v2[2], v2[4], v2[6]) &&
        max(v1[1], v1[3], v1[5], v1[7]) + epsilon > min(v2[1], v2[3], v2[5], v2[7]) &&
        min(v1[1], v1[3], v1[5], v1[7]) - epsilon < max(v2[1], v2[3], v2[5], v2[7])
      ) {
        // Now detect and handle overlaps:
        var overlaps = getOverlaps(v1, v2);
        if (overlaps) {
          for (var i = 0; i < 2; i++) {
            var overlap = overlaps[i];
            addLocation(locations, include, c1, overlap[0], c2, overlap[1], true);
          }
        } else {
          var straight1 = Curve.isStraight(v1),
            straight2 = Curve.isStraight(v2),
            straight = straight1 && straight2,
            flip = straight1 && !straight2,
            before = locations.length;
          // Determine the correct intersection method based on whether
          // one or curves are straight lines:
          (straight ? addLineIntersection : straight1 || straight2 ? addCurveLineIntersections : addCurveIntersections)(
            flip ? v2 : v1,
            flip ? v1 : v2,
            flip ? c2 : c1,
            flip ? c1 : c2,
            locations,
            include,
            flip,
            // Define the defaults for these parameters of
            // addCurveIntersections():
            // recursion, calls, tMin, tMax, uMin, uMax
            0,
            0,
            0,
            1,
            0,
            1
          );
          // Handle the special case where the first curve's start- / end-
          // point overlaps with the second curve's start- / end-point,
          // but only if haven't found a line-line intersection already:
          // #805#issuecomment-148503018
          if (!straight || locations.length === before) {
            for (var i = 0; i < 4; i++) {
              var t1 = i >> 1, // 0, 0, 1, 1
                t2 = i & 1, // 0, 1, 0, 1
                i1 = t1 * 6,
                i2 = t2 * 6,
                p1 = new Point(v1[i1], v1[i1 + 1]),
                p2 = new Point(v2[i2], v2[i2 + 1]);
              if (p1.isClose(p2, epsilon)) {
                // @ts-expect-error = Expected 7 arguments, but got 6
                addLocation(locations, include, c1, t1, c2, t2);
              }
            }
          }
        }
      }
      return locations;
    }
 
    function getSelfIntersection(v1, c1, locations, include) {
      var info = Curve.classify(v1);
      if (info.type === 'loop') {
        var roots = info.roots;
        // @ts-expect-error = Expected 7 arguments, but got 6
        addLocation(locations, include, c1, roots[0], c1, roots[1]);
      }
      return locations;
    }
 
    function getIntersections(curves1, curves2, include, matrix1, matrix2, _returnFirst) {
      var epsilon = /*#=*/ Numerical.GEOMETRIC_EPSILON,
        self = !curves2;
      if (self) curves2 = curves1;
      var length1 = curves1.length,
        length2 = curves2.length,
        values1 = new Array(length1),
        values2 = self ? values1 : new Array(length2),
        locations = [];
 
      for (var i = 0; i < length1; i++) {
        values1[i] = curves1[i].getValues(matrix1);
      }
      if (!self) {
        for (var i = 0; i < length2; i++) {
          values2[i] = curves2[i].getValues(matrix2);
        }
      }
      // @ts-expect-error = Expected 4 arguments, but got 3
      var boundsCollisions = CollisionDetection.findCurveBoundsCollisions(values1, values2, epsilon);
      for (var index1 = 0; index1 < length1; index1++) {
        var curve1 = curves1[index1],
          v1 = values1[index1];
        if (self) {
          // First check for self-intersections within the same curve.
          getSelfIntersection(v1, curve1, locations, include);
        }
        // Check for intersections with potentially intersecting curves.
        var collisions1 = boundsCollisions[index1];
        if (collisions1) {
          for (var j = 0; j < collisions1.length; j++) {
            // There might be already one location from the above
            // self-intersection check:
            if (_returnFirst && locations.length) return locations;
            var index2 = collisions1[j];
            if (!self || index2 > index1) {
              var curve2 = curves2[index2],
                v2 = values2[index2];
              getCurveIntersections(v1, v2, curve1, curve2, locations, include);
            }
          }
        }
      }
      return locations;
    }
 
    /**
     * Code to detect overlaps of intersecting based on work by
     * @iconexperience in #648
     */
    function getOverlaps(v1, v2) {
      // Linear curves can only overlap if they are collinear. Instead of
      // using the #isCollinear() check, we pick the longer of the two curves
      // treated as lines, and see how far the starting and end points of the
      // other line are from this line (assumed as an infinite line). But even
      // if the curves are not straight, they might just have tiny handles
      // within geometric epsilon distance, so we have to check for that too.
 
      function getSquaredLineLength(v) {
        var x = v[6] - v[0],
          y = v[7] - v[1];
        return x * x + y * y;
      }
 
      var abs = Math.abs,
        getDistance = Line.getDistance,
        timeEpsilon = /*#=*/ Numerical.CURVETIME_EPSILON,
        geomEpsilon = /*#=*/ Numerical.GEOMETRIC_EPSILON,
        straight1 = Curve.isStraight(v1),
        straight2 = Curve.isStraight(v2),
        straightBoth = straight1 && straight2,
        flip = getSquaredLineLength(v1) < getSquaredLineLength(v2),
        l1 = flip ? v2 : v1,
        l2 = flip ? v1 : v2,
        // Get l1 start and end point values for faster referencing.
        px = l1[0],
        py = l1[1],
        vx = l1[6] - px,
        vy = l1[7] - py;
      // See if the starting and end point of curve two are very close to the
      // picked line. Note that the curve for the picked line might not
      // actually be a line, so we have to perform more checks after.
      if (
        getDistance(px, py, vx, vy, l2[0], l2[1], true) < geomEpsilon &&
        getDistance(px, py, vx, vy, l2[6], l2[7], true) < geomEpsilon
      ) {
        // If not both curves are straight, check against both of their
        // handles, and treat them as straight if they are very close.
        if (
          !straightBoth &&
          getDistance(px, py, vx, vy, l1[2], l1[3], true) < geomEpsilon &&
          getDistance(px, py, vx, vy, l1[4], l1[5], true) < geomEpsilon &&
          getDistance(px, py, vx, vy, l2[2], l2[3], true) < geomEpsilon &&
          getDistance(px, py, vx, vy, l2[4], l2[5], true) < geomEpsilon
        ) {
          straight1 = straight2 = straightBoth = true;
        }
      } else if (straightBoth) {
        // If both curves are straight and not very close to each other,
        // there can't be a solution.
        return null;
      }
      if (straight1 ^ straight2) {
        // If one curve is straight, the other curve must be straight too,
        // otherwise they cannot overlap.
        return null;
      }
 
      var v = [v1, v2],
        pairs = [];
      // Iterate through all end points:
      // First p1 of curve 1 & 2, then p2 of curve 1 & 2.
      for (var i = 0; i < 4 && pairs.length < 2; i++) {
        var i1 = i & 1, // 0, 1, 0, 1
          i2 = i1 ^ 1, // 1, 0, 1, 0
          t1 = i >> 1, // 0, 0, 1, 1
          t2 = Curve.getTimeOf(v[i1], new Point(v[i2][t1 ? 6 : 0], v[i2][t1 ? 7 : 1]));
        if (t2 != null) {
          // If point is on curve
          var pair = i1 ? [t1, t2] : [t2, t1];
          // Filter out tiny overlaps.
          if (!pairs.length || (abs(pair[0] - pairs[0][0]) > timeEpsilon && abs(pair[1] - pairs[0][1]) > timeEpsilon)) {
            pairs.push(pair);
          }
        }
        // We checked 3 points but found no match, curves can't overlap.
        if (i > 2 && !pairs.length) break;
      }
      if (pairs.length !== 2) {
        pairs = null;
      } else if (!straightBoth) {
        // Straight pairs don't need further checks. If we found 2 pairs,
        // the end points on v1 & v2 should be the same.
        var o1 = Curve.getPart(v1, pairs[0][0], pairs[1][0]),
          o2 = Curve.getPart(v2, pairs[0][1], pairs[1][1]);
        // Check if handles of the overlapping curves are the same too.
        if (
          abs(o2[2] - o1[2]) > geomEpsilon ||
          abs(o2[3] - o1[3]) > geomEpsilon ||
          abs(o2[4] - o1[4]) > geomEpsilon ||
          abs(o2[5] - o1[5]) > geomEpsilon
        )
          pairs = null;
      }
      return pairs;
    }
 
    /**
     * Internal method to calculates the curve-time parameters where the curve
     * is tangential to provided tangent.
     * Tangents at the start or end are included.
     *
     * @param {Number[]} v curve values
     * @param {Point} tangent the tangent to which the curve must be tangential
     * @return {Number[]} at most two curve-time parameters, where the curve is
     * tangential to the given tangent
     */
    function getTimesWithTangent(v, tangent) {
      // Algorithm adapted from: https://stackoverflow.com/a/34837312/7615922
      var x0 = v[0],
        y0 = v[1],
        x1 = v[2],
        y1 = v[3],
        x2 = v[4],
        y2 = v[5],
        x3 = v[6],
        y3 = v[7],
        normalized = tangent.normalize(),
        tx = normalized.x,
        ty = normalized.y,
        ax = 3 * x3 - 9 * x2 + 9 * x1 - 3 * x0,
        ay = 3 * y3 - 9 * y2 + 9 * y1 - 3 * y0,
        bx = 6 * x2 - 12 * x1 + 6 * x0,
        by = 6 * y2 - 12 * y1 + 6 * y0,
        cx = 3 * x1 - 3 * x0,
        cy = 3 * y1 - 3 * y0,
        den = 2 * ax * ty - 2 * ay * tx,
        times = [];
      if (Math.abs(den) < Numerical.CURVETIME_EPSILON) {
        var num = ax * cy - ay * cx,
          den = ax * by - ay * bx;
        if (den != 0) {
          var t = -num / den;
          if (t >= 0 && t <= 1) times.push(t);
        }
      } else {
        var delta =
            (bx * bx - 4 * ax * cx) * ty * ty +
            (-2 * bx * by + 4 * ay * cx + 4 * ax * cy) * tx * ty +
            (by * by - 4 * ay * cy) * tx * tx,
          k = bx * ty - by * tx;
        if (delta >= 0 && den != 0) {
          var d = Math.sqrt(delta),
            t0 = -(k + d) / den,
            t1 = (-k + d) / den;
          if (t0 >= 0 && t0 <= 1) times.push(t0);
          if (t1 >= 0 && t1 <= 1) times.push(t1);
        }
      }
      return times;
    }
 
    return /** @lends Curve# */ {
      /**
       * Returns all intersections between two {@link Curve} objects as an
       * array of {@link CurveLocation} objects.
       *
       * @param {Curve} curve the other curve to find the intersections with
       *     (if the curve itself or `null` is passed, the self intersection
       *     of the curve is returned, if it exists)
       * @return {CurveLocation[]} the locations of all intersections between
       *     the curves
       */
      getIntersections: function (curve) {
        var v1 = this.getValues(),
          v2 = curve && curve !== this && curve.getValues();
        return v2
          ? // @ts-expect-error = Expected 6 arguments, but got 5
            getCurveIntersections(v1, v2, this, curve, [])
          : // @ts-expect-error = Expected 4 arguments, but got 3
            getSelfIntersection(v1, this, []);
      },
 
      statics: /** @lends Curve */ {
        getOverlaps: getOverlaps,
        // Exposed for use in boolean offsetting
        getIntersections: getIntersections,
        getCurveLineIntersections: getCurveLineIntersections,
        getTimesWithTangent: getTimesWithTangent,
      },
    };
  })()
);
 
ref.Curve = Curve;