Path utility extension 🧠

Extension of SVG Path utility code
Adds support for V and H statements
Adds support for multiple sets of instructions per statement

I made these modifications to deal with InkScape exports using absolute coordinates.

SVG from svgrepo.com/svg/482779/brain-illustration-12

Log in to post a comment.

// Forked from "Path input type" by reinder
// https://turtletoy.net/turtle/46adb0ad70

// Extension of SVG Path utility code. Made by Jurgen Westerhof 2024
// The MIT License
//
// Adds support for V and H statements
// Adds support for multiple sets of instructions per statement
//
// https://turtletoy.net/turtle/fb0c83c18b
//

const pathSetInputs = [ //Paths converted to absolute SVG with InkScape from https://www.svgrepo.com/svg/482779/brain-illustration-12
'M 410.34,153.043 C 409.062,151.75 407.628,150.566 406.052,149.516 L 404.437,149.179 C 403.591,148.811 402.917,148.207 402.477,147.477 399.953,146.286 397.311,145.518 394.623,145.134 392.302,144.812 390.672,142.657 391.009,140.32 391.322,137.984 393.493,136.354 395.814,136.675 398.472,137.067 401.136,137.742 403.692,138.745 404.107,136.879 404.342,134.974 404.342,133.054 404.334,128.852 403.347,124.611 401.246,120.621 398.847,116.059 395.328,112.5 391.212,110.071 387.606,107.939 383.53,106.669 379.344,106.371 379.039,110.188 378.13,113.99 376.546,117.674 375.605,119.837 373.105,120.833 370.934,119.9 368.77,118.975 367.767,116.467 368.7,114.303 370.22,110.753 370.934,107.1 370.95,103.478 L 370.895,101.934 C 370.605,96.917 368.943,92.073 366.113,87.942 363.283,83.818 359.317,80.44 354.379,78.3 350.836,76.787 347.167,76.074 343.553,76.074 336.631,76.074 329.929,78.74 324.866,83.442 325.814,86.664 326.355,90.058 326.355,93.586 326.355,95.938 324.427,97.85 322.075,97.85 319.723,97.85 317.803,95.938 317.803,93.586 317.803,90.129 317.168,86.852 316.008,83.803 H 316.016 C 314.048,78.669 310.568,74.248 306.108,71.128 301.663,68.009 296.278,66.182 290.415,66.182 285.328,66.182 280.585,67.57 276.509,69.968 273.248,71.896 270.434,74.499 268.224,77.556 272.543,83.474 275.098,90.788 275.098,98.674 275.098,101.025 273.178,102.93 270.818,102.93 268.466,102.93 266.546,101.025 266.546,98.674 266.546,91.745 263.998,85.442 259.758,80.621 254.726,74.875 247.381,71.278 239.15,71.278 233.436,71.278 228.176,73.01 223.778,75.989 219.396,78.968 215.908,83.185 213.846,88.139 212.514,91.376 211.753,94.943 211.753,98.674 211.753,101.025 209.848,102.93 207.489,102.93 205.129,102.93 203.225,101.025 203.225,98.674 203.225,95.209 203.727,91.87 204.636,88.687 201.422,87.386 197.942,86.665 194.368,86.665 192.338,86.665 190.3,86.9 188.23,87.363 182.147,88.766 177.06,92.043 173.336,96.479 170.655,99.678 168.742,103.479 167.747,107.563 176.566,107.719 185.376,111.129 192.181,117.792 193.859,119.446 193.89,122.143 192.244,123.82 190.59,125.513 187.893,125.529 186.208,123.89 180.862,118.678 173.972,116.067 167.042,116.059 165.804,116.059 164.565,116.145 163.326,116.302 H 163.311 163.303 C 157.495,117.109 151.898,119.758 147.453,124.29 142.232,129.62 139.645,136.503 139.638,143.448 139.638,147.407 140.5,151.396 142.224,155.065 143.564,157.957 145.446,160.654 147.876,163.021 V 163.028 C 150.62,165.725 153.787,167.716 157.166,169.017 159.352,169.872 160.458,172.341 159.596,174.535 158.742,176.746 156.28,177.844 154.078,176.981 149.665,175.272 145.495,172.655 141.888,169.119 140.313,167.567 138.909,165.913 137.664,164.158 134.442,168.775 132.694,174.3 132.694,179.929 132.694,184.288 133.737,188.685 135.939,192.816 138.416,197.441 141.999,201.047 146.216,203.508 149.547,205.46 153.271,206.691 157.112,207.122 L 157.104,206.863 C 157.104,204.605 157.316,202.395 157.7,200.247 158.139,197.927 160.373,196.413 162.678,196.829 164.998,197.26 166.527,199.486 166.111,201.806 165.805,203.444 165.641,205.145 165.641,206.862 L 165.711,208.846 165.938,210.845 V 210.868 L 165.946,210.907 C 167.906,224.296 179.476,234.305 193.021,234.305 L 194.989,234.227 H 194.997 C 201.001,233.796 206.394,231.475 210.674,227.862 213.676,225.33 216.137,222.156 217.823,218.58 218.818,216.44 221.381,215.532 223.506,216.542 225.638,217.546 226.548,220.085 225.544,222.218 223.718,226.067 221.225,229.539 218.231,232.494 219.908,237.652 223.059,242.081 227.175,245.373 231.879,249.144 237.812,251.37 244.224,251.37 L 246.2,251.292 H 246.208 C 250.268,251.002 254.023,249.834 257.355,248.015 256.837,245.562 256.54,243.054 256.54,240.514 256.54,235.277 257.708,229.939 260.122,224.922 261.149,222.798 263.696,221.912 265.813,222.939 267.937,223.95 268.839,226.506 267.812,228.63 265.954,232.479 265.084,236.524 265.084,240.514 265.084,243.304 265.515,246.087 266.346,248.728 H 266.354 C 268.541,255.697 273.48,261.788 280.589,265.205 284.43,267.063 288.475,267.933 292.457,267.933 298.783,267.933 304.968,265.722 309.875,261.717 L 309.891,261.709 C 312.87,259.255 315.363,256.151 317.157,252.443 L 317.165,252.427 C 318.835,248.963 319.713,245.334 319.861,241.712 319.971,239.36 321.954,237.526 324.321,237.636 326.673,237.738 328.491,239.728 328.389,242.08 328.185,246.83 327.033,251.627 324.846,256.15 V 256.142 C 323.09,259.803 320.778,263.025 318.066,265.776 323.098,273.003 331.47,277.558 340.524,277.55 342.201,277.55 343.91,277.394 345.627,277.064 352.219,275.826 357.777,272.337 361.728,267.54 365.671,262.734 367.968,256.628 367.968,250.184 L 367.96,249.605 V 249.573 C 367.929,248.123 367.78,246.603 367.498,245.073 366.816,241.444 365.428,238.151 363.524,235.243 360.765,232.813 357.7,229.639 354.808,226.111 351.727,222.349 348.89,218.233 346.985,214.087 345.731,211.312 344.837,208.506 344.821,205.551 344.821,204.242 345.001,202.886 345.495,201.585 345.973,200.26 346.757,199.006 347.807,197.971 349.469,196.317 352.165,196.317 353.835,197.971 355.497,199.648 355.497,202.36 353.835,204.015 L 353.513,204.525 353.349,205.552 C 353.333,206.626 353.78,208.445 354.744,210.537 355.7,212.623 357.119,214.966 358.789,217.278 362.097,221.911 366.385,226.458 369.614,229.232 L 370.053,229.609 370.375,230.087 C 373.017,234.022 374.953,238.553 375.885,243.499 V 243.507 L 376.191,245.623 377.469,245.678 C 382.878,245.67 388.325,244.071 393.115,240.708 396.925,238.027 399.825,234.586 401.785,230.721 403.752,226.849 404.779,222.569 404.779,218.258 404.779,214.221 403.869,210.168 402.051,206.398 400.365,206.939 398.656,207.37 396.924,207.668 394.604,208.083 392.401,206.507 391.986,204.188 391.593,201.86 393.162,199.665 395.482,199.257 403.038,197.956 409.96,193.551 414.24,186.48 416.952,182.036 418.23,177.144 418.23,172.308 418.23,165.182 415.478,158.214 410.352,153.048 Z M 214.788,120.481 C 218.864,115.66 224.273,112.234 230.089,109.992 235.929,107.75 242.184,106.676 248.189,106.676 254.029,106.684 259.641,107.672 264.407,109.883 266.539,110.854 267.472,113.402 266.484,115.542 265.504,117.675 262.972,118.615 260.817,117.628 257.525,116.084 253.017,115.198 248.189,115.198 243.219,115.198 237.921,116.114 233.162,117.965 228.404,119.791 224.226,122.519 221.286,125.984 219.781,127.787 217.077,128.022 215.274,126.486 213.479,124.965 213.252,122.276 214.788,120.481 Z M 311.925,221.396 C 306.963,221.396 302.463,220.832 298.356,219.884 298.466,221.059 298.544,222.368 298.544,223.756 298.544,226.539 298.238,229.72 297.368,233.06 296.49,236.391 295.009,239.895 292.673,243.258 291.333,245.202 288.675,245.68 286.739,244.339 284.795,242.998 284.325,240.349 285.649,238.397 287.373,235.912 288.447,233.372 289.106,230.872 289.765,228.372 290.008,225.941 290.008,223.755 290.016,220.737 289.553,218.252 289.224,216.833 287.139,215.892 285.218,214.834 283.424,213.65 276.275,209.025 271.494,202.919 268.491,197.11 266.477,193.214 265.253,189.452 264.595,186.183 264.219,184.317 264.031,182.625 264.031,181.087 264.031,178.72 265.944,176.807 268.303,176.807 270.662,176.807 272.575,178.72 272.575,181.087 272.567,182.325 272.834,184.403 273.508,186.763 274.182,189.13 275.256,191.819 276.808,194.523 279.92,199.963 284.866,205.411 292.501,208.947 297.604,211.314 303.953,212.85 311.925,212.85 314.284,212.85 316.197,214.763 316.197,217.122 316.197,219.484 314.285,221.396 311.925,221.396 Z M 341.979,190.229 C 341.101,192.409 338.616,193.475 336.421,192.604 334.234,191.726 333.184,189.257 334.046,187.07 V 187.062 L 334.07,186.999 334.196,186.623 334.635,185.087 C 334.957,183.739 335.286,181.834 335.286,179.741 L 335.278,179.443 C 329.415,181.434 323.975,182.327 318.966,182.327 311.394,182.343 304.841,180.266 299.51,177.115 294.172,173.965 290.018,169.794 286.913,165.553 283.386,160.748 278.761,156.437 273.462,153.388 268.148,150.33 262.206,148.512 255.896,148.496 250.37,148.512 244.491,149.891 238.353,153.262 232.529,156.46 228.782,160.716 226.36,165.388 223.954,170.068 222.935,175.178 222.942,179.835 222.926,184.146 223.836,188.049 225.019,190.511 226.03,192.643 225.113,195.183 222.989,196.194 220.865,197.206 218.317,196.288 217.306,194.164 215.448,190.228 214.414,185.274 214.398,179.835 214.406,174.426 215.48,168.515 218.122,162.864 212.824,164.064 207.901,164.597 203.417,164.597 200.995,164.597 198.713,164.44 196.542,164.158 L 195.75,165.757 195.742,165.772 195.679,165.836 195.452,166.126 194.542,167.364 C 193.766,168.478 192.692,170.1 191.524,172.169 189.196,176.269 186.531,182.054 185.237,188.551 184.775,190.864 182.533,192.377 180.22,191.906 177.908,191.444 176.402,189.202 176.881,186.874 178.417,179.168 181.452,172.615 184.093,167.951 185.472,165.537 186.735,163.625 187.659,162.323 184.179,161.281 181.169,160.003 178.708,158.701 173.088,155.714 170.163,152.72 169.913,152.477 168.282,150.776 168.329,148.079 170.022,146.441 171.723,144.811 174.412,144.858 176.05,146.535 L 176.098,146.582 176.348,146.825 177.571,147.851 C 178.683,148.729 180.401,149.937 182.697,151.159 187.306,153.589 194.189,156.051 203.415,156.067 209.897,156.067 217.54,154.829 226.421,151.411 228.671,149.318 231.265,147.406 234.244,145.775 241.55,141.77 248.91,139.967 255.894,139.967 263.874,139.967 271.297,142.288 277.725,145.987 284.153,149.703 289.601,154.79 293.794,160.512 296.342,163.968 299.689,167.316 303.836,169.753 308.006,172.199 312.936,173.791 318.964,173.798 323.095,173.798 327.751,173.03 333.019,171.18 331.734,169.118 329.821,167.127 326.67,165.324 324.616,164.156 323.887,161.554 325.047,159.508 326.215,157.446 328.81,156.74 330.863,157.885 335.911,160.731 339.29,164.501 341.249,168.444 343.232,172.371 343.82,176.353 343.82,179.74 343.797,185.652 342.096,189.924 341.979,190.229 Z M 340.38,137.938 C 330.409,137.938 321.685,135.249 314.63,131.181 313.635,132.561 312.302,134.293 310.585,136.174 306.721,140.415 301.045,145.393 293.45,148.12 291.216,148.904 288.786,147.76 287.994,145.534 287.202,143.307 288.37,140.87 290.573,140.086 296.178,138.087 300.865,134.16 304.111,130.609 305.616,128.986 306.784,127.45 307.631,126.274 303.421,122.716 300.098,118.663 297.817,114.493 295.654,110.495 294.399,106.372 294.384,102.366 294.384,100.015 296.297,98.102 298.656,98.102 301.008,98.102 302.92,100.015 302.92,102.366 302.904,104.522 303.657,107.399 305.318,110.417 306.956,113.442 309.449,116.593 312.71,119.416 319.232,125.068 328.646,129.394 340.381,129.394 342.74,129.394 344.645,131.315 344.645,133.674 344.644,136.025 342.739,137.938 340.38,137.938 Z M 374.023,198.037 C 372.526,199.84 369.829,200.106 368.011,198.593 366.193,197.096 365.942,194.4 367.431,192.589 372.087,186.937 373.804,181.246 373.819,175.799 373.835,170.837 372.314,166.047 369.931,162.011 367.579,157.981 364.327,154.752 361.199,152.957 359.098,151.75 357.123,151.209 355.555,151.209 353.195,151.209 351.291,149.305 351.291,146.945 351.291,144.586 353.196,142.673 355.555,142.673 358.149,142.673 360.689,143.308 363.104,144.374 365.51,145.425 367.784,146.922 369.9,148.749 374.125,152.401 377.715,157.371 379.973,163.196 381.462,167.083 382.371,171.348 382.371,175.8 382.379,183.12 379.879,190.958 374.023,198.037 Z',
'M 428.699,66.574 C 396.943,29.442 349.183,4.155 287.986,0.462 283.008,0.156 278.054,0 273.147,0 221.678,0 174.332,16.712 139.623,47.393 104.89,77.995 83.263,122.801 83.341,176.465 83.341,177.68 83.349,178.895 83.372,180.118 L 83.318,186.592 39.139,264.61 C 36.764,268.819 35.564,273.515 35.564,278.211 35.564,282.129 36.396,286.065 38.08,289.733 41.647,297.517 48.662,303.114 56.971,305.042 L 56.955,305.112 77.273,310.709 85.3,388.132 85.363,388.124 C 85.928,399.404 90.96,409.994 99.505,417.425 107.101,424.041 116.797,427.631 126.768,427.631 127.778,427.631 128.774,427.537 129.778,427.467 L 152.8,429.293 153.953,429.168 154.321,429.152 C 155.206,429.152 156.03,429.466 156.665,430.038 L 156.703,430.078 C 157.433,430.736 157.864,431.7 157.864,432.688 V 512 H 372.824 V 499.96 C 372.824,499.96 372.824,467.344 372.824,454.332 372.8,449.095 373.843,438.741 375.897,428.754 376.916,423.746 378.177,418.752 379.605,414.434 381.016,410.123 382.647,406.454 384.01,404.314 398.308,381.268 418.524,362.362 437.038,335.624 455.53,308.91 471.293,274.255 475.612,221.673 476.161,215.081 476.436,208.488 476.436,201.928 476.436,152.079 460.508,103.674 428.699,66.574 Z M 451.611,219.664 C 447.433,268.539 433.762,297.784 417.206,321.967 400.675,346.126 380.232,365.119 363.59,391.544 360.69,396.216 358.565,401.39 356.739,406.892 354.027,415.131 352.091,424.114 350.766,432.478 349.449,440.865 348.752,448.469 348.744,454.332 348.744,462.688 348.744,475.324 348.744,487.92 H 181.944 V 432.689 C 181.944,424.842 178.605,417.372 172.756,412.129 167.653,407.543 161.069,405.066 154.32,405.066 153.826,405.066 153.348,405.12 152.862,405.152 L 129.864,403.318 128.735,403.443 126.768,403.553 C 122.582,403.553 118.506,402.04 115.315,399.265 111.631,396.059 109.451,391.45 109.31,386.551 L 109.303,386.097 99.536,291.876 62.929,281.78 62.49,281.694 C 61.377,281.474 60.445,280.737 59.966,279.702 L 59.644,278.213 60.099,276.473 107.35,193.03 107.452,179.994 V 179.821 C 107.429,178.693 107.421,177.579 107.421,176.467 107.5,129.216 125.865,91.809 155.574,65.432 185.314,39.117 226.844,24.082 273.147,24.082 277.56,24.082 282.013,24.223 286.497,24.498 C 341.947,27.986 382.756,50.107 410.418,82.253 438.034,114.423 452.356,157.151 452.356,201.927 452.356,207.812 452.113,213.73 451.611,219.664'
];
// an array of [fill, Path]
const pathSets = pathSetInputs.flatMap(ps => new PathSet(ps).reverse().map((e,i,a) => [i==a.length-1, e]));

const turtle = new Turtle();
const polygons = new Polygons();

const add = (a,b) => [a[0]+b[0],a[1]+b[1]];
const scale = (a,s) => [a[0]*s,a[1]*s];

function walk(i) {
    const [hatch, path] = pathSets[i];
    const steps = path.length() | 0;

    const p = polygons.create();
    for(let j = 0; j <= steps; j++) {
        p.addPoints(scale(add(path.p( j/steps ), [-250, -250]), .3));
    }
    if(hatch) p.addHatching(1, .15);
    polygons.draw(turtle, p);
    
    return i < pathSets.length - 1;
}

// Convert multiple svg paths to an array.
// InkScape filled paths describe polygons separated by a Z (closed path) where the first index is the
// filled polygon, with 'gaps' defined as paths on the other indexes. So I split by 'z' then reverse
// the array and add a boolean: true if the path should be filled.
function PathSet(svg) {
    return svg.split(/z/gi).map(s => (s.trim() == '')? false: new Path(s)).filter(p => p);
}

////////////////////////////////////////////////////////////////
// Path utility code. Created by Reinder Nijhoff 2023
// Parses a single SVG path (only M, C and L statements are
// supported). The p-method will return
// [...position, ...derivative] for a normalized point t.
//
// https://turtletoy.net/turtle/46adb0ad70
//
// Modifications by Jurgen Westerhof 2024 (support for InkScape absolute export)
// Added support for H and V statements
// Added support for multiple sets of parameter(s) for one statement
////////////////////////////////////////////////////////////////
function Path(svg) {
    class MoveTo {
        constructor(p) { this.p0 = p; }
        p(t, s) { return [...this.p0, 1, 0]; }
        length() { return 0; }
    }
    class LineTo {
        constructor(p0, p1) { this.p0 = p0, this.p1 = p1; }
        p(t, s = 1) {
            const nt = 1 - t, p0 = this.p0, p1 = this.p1;
            return [ 
                nt*p0[0] + t*p1[0],
                nt*p0[1] + t*p1[1],
                (p1[0] - p0[0]) * s,
                (p1[1] - p0[1]) * s,
            ];
        }
        length() { 
            const p0 = this.p0, p1 = this.p1;
            return Math.hypot(p0[0]-p1[0], p0[1]-p1[1]);
        }
    }
    class BezierTo {
        constructor(p0, c0, c1, p1) { this.p0 = p0, this.c0 = c0, this.c1 = c1, this.p1 = p1; }
        p(t, s = 1) {
            const nt = 1 - t, p0 = this.p0, c0 = this.c0, c1 = this.c1, p1 = this.p1;
            return [ 
                nt*nt*nt*p0[0] + 3*t*nt*nt*c0[0] + 3*t*t*nt*c1[0] + t*t*t*p1[0],
                nt*nt*nt*p0[1] + 3*t*nt*nt*c0[1] + 3*t*t*nt*c1[1] + t*t*t*p1[1],
                (3*nt*nt*(c0[0]-p0[0]) + 6*t*nt*(c1[0]-c0[0]) + 3*t*t*(p1[0]-c1[0])) * s,
                (3*nt*nt*(c0[1]-p0[1]) + 6*t*nt*(c1[1]-c0[1]) + 3*t*t*(p1[1]-c1[1])) * s,
            ];
        }
        length() {
            return this._length || (
                this._length = Array.from({length:25}, (x, i) => this.p(i/25)).reduce( 
                    (a,c,i,v) => i > 0 ? a + Math.hypot(c[0]-v[i-1][0], c[1]-v[i-1][1]) : a, 0));
        }
    }
    class Path {
        constructor(svg) {
            this.segments = [];
            this.parsePath(svg);
        }
        parsePath(svg) {
            const knownStatements = 'MLCHV';
            const t = svg.match(new RegExp(`([0-9.-]+|[${knownStatements}])`, 'g'));
            let st = t[0];
            for (let s, i=0; i<t.length;) {
                const a = t[i++];
                if(knownStatements.indexOf(a) > -1) st = a; else i--;
                switch (st) {
                    case 'M': this.add(new MoveTo(s=[t[i++],t[i++]]));
                              break;
                    case 'L': this.add(new LineTo(s, s=[t[i++],t[i++]]));
                              break;
                    case 'C': this.add(new BezierTo(s, [t[i++],t[i++]], [t[i++],t[i++]], s=[t[i++],t[i++]]));
                              break;
                    case 'H': this.add(new LineTo(s, s=[t[i++],s[1]]));
                              break;
                    case 'V': this.add(new LineTo(s, s=[s[0],t[i++]]));
                              break;
                    default:  i++;
                }
            }
        }
        add(segment) {
            this.segments.push(segment);
            this._length = 0;
        }
        length() {
            return this._length || (this._length = this.segments.reduce((a,c) => a + c.length(), 0));
        }
        p(t) {
            t = Math.max(Math.min(t, 1), 0) * this.length();
            for (let l=0, i=0, sl=0; i<this.segments.length; i++, l+=sl) {
                sl = this.segments[i].length();
                if (t > l && t <= l + sl) {
                    return this.segments[i].p((t-l)/sl, sl/this.length());
                }
            }
            return this.segments[Math.min(1, this.segments.length-1)].p(0);
        }
    }
    return new Path(svg);
}


////////////////////////////////////////////////////////////////
// Polygon Clipping utility code - Created by Reinder Nijhoff 2019
// (Polygon binning by Lionel Lemarie 2021) https://turtletoy.net/turtle/95f33bd383
// (Delegated Hatching by Jurgen Westerhof 2024) https://turtletoy.net/turtle/d068ad6040
// (Deferred Polygon Drawing by Jurgen Westerhof 2024) https://turtletoy.net/turtle/6f3d2bc0b5
// https://turtletoy.net/turtle/a5befa1f8d
//
// const polygons = new Polygons();
// const p = polygons.create();
// polygons.draw(turtle, p);
// polygons.list();
// polygons.startDeferSession();
// polygons.stopDeferring();
// polygons.finalizeDeferSession(turtle);
//
// p.addPoints(...[[x,y],]);
// p.addSegments(...[[x,y],]);
// p.addOutline();
// p.addHatching(angle, distance); OR p.addHatching(HatchObject); where HatchObject has a method 'hatch(PolygonClass, thisPolygonInstance)'
// p.inside([x,y]);
// p.boolean(polygon, diff = true);
// p.segment_intersect([x,y], [x,y], [x,y], [x,y]);
////////////////////////////////////////////////////////////////
function Polygons(){const t=[],s=25,e=Array.from({length:s**2},t=>[]),n=class{constructor(){this.cp=[],this.dp=[],this.aabb=[]}addPoints(...t){let s=1e5,e=-1e5,n=1e5,h=-1e5;(this.cp=[...this.cp,...t]).forEach(t=>{s=Math.min(s,t[0]),e=Math.max(e,t[0]),n=Math.min(n,t[1]),h=Math.max(h,t[1])}),this.aabb=[s,n,e,h]}addSegments(...t){t.forEach(t=>this.dp.push(t))}addOutline(){for(let t=0,s=this.cp.length;t<s;t++)this.dp.push(this.cp[t],this.cp[(t+1)%s])}draw(t){for(let s=0,e=this.dp.length;s<e;s+=2)t.jump(this.dp[s]),t.goto(this.dp[s+1])}addHatching(t, s) {if(typeof t == 'object') return t.hatch(n, this);const e=new n;e.cp.push([-1e5,-1e5],[1e5,-1e5],[1e5,1e5],[-1e5,1e5]);const h=Math.sin(t)*s,o=Math.cos(t)*s,a=200*Math.sin(t),i=200*Math.cos(t);for(let t=.5;t<150/s;t++) {e.dp.push([h*t+i,o*t-a],[h*t-i,o*t+a]);e.dp.push([-h*t+i,-o*t-a],[-h*t-i,-o*t+a]);}e.boolean(this,!1);this.dp=[...this.dp,...e.dp]}inside(t){let s=0;for(let e=0,n=this.cp.length;e<n;e++)this.segment_intersect(t,[.1,-1e3],this.cp[e],this.cp[(e+1)%n])&&s++;return 1&s}boolean(t,s=!0){const e=[];for(let n=0,h=this.dp.length;n<h;n+=2){const h=this.dp[n],o=this.dp[n+1],a=[];for(let s=0,e=t.cp.length;s<e;s++){const n=this.segment_intersect(h,o,t.cp[s],t.cp[(s+1)%e]);!1!==n&&a.push(n)}if(0===a.length)s===!t.inside(h)&&e.push(h,o);else{a.push(h,o);const n=o[0]-h[0],i=o[1]-h[1];a.sort((t,s)=>(t[0]-h[0])*n+(t[1]-h[1])*i-(s[0]-h[0])*n-(s[1]-h[1])*i);for(let n=0;n<a.length-1;n++)(a[n][0]-a[n+1][0])**2+(a[n][1]-a[n+1][1])**2>=.001&&s===!t.inside([(a[n][0]+a[n+1][0])/2,(a[n][1]+a[n+1][1])/2])&&e.push(a[n],a[n+1])}}return(this.dp=e).length>0}segment_intersect(t,s,e,n){const h=(n[1]-e[1])*(s[0]-t[0])-(n[0]-e[0])*(s[1]-t[1]);if(0===h)return!1;const o=((n[0]-e[0])*(t[1]-e[1])-(n[1]-e[1])*(t[0]-e[0]))/h,a=((s[0]-t[0])*(t[1]-e[1])-(s[1]-t[1])*(t[0]-e[0]))/h;return o>=0&&o<=1&&a>=0&&a<=1&&[t[0]+o*(s[0]-t[0]),t[1]+o*(s[1]-t[1])]}};const y=function(n,j=[]){const h={},o=200/s;for(var a=0;a<s;a++){const c=a*o-100,r=[0,c,200,c+o];if(!(n[3]<r[1]||n[1]>r[3]))for(var i=0;i<s;i++){const c=i*o-100;r[0]=c,r[2]=c+o,n[0]>r[2]||n[2]<r[0]||e[i+a*s].forEach(s=>{const e=t[s];n[3]<e.aabb[1]||n[1]>e.aabb[3]||n[0]>e.aabb[2]||n[2]<e.aabb[0]||j.includes(s)||(h[s]=1)})}}return Array.from(Object.keys(h),s=>t[s])};return{list:()=>t,create:()=>new n,draw:(n,h,o=!0)=>{rpl=y(h.aabb, this.dei === undefined? []: Array.from({length: t.length - this.dei}).map((e, i) => this.dsi + i));for(let t=0;t<rpl.length&&h.boolean(rpl[t]);t++);const td=n.isdown();if(this.dsi!==undefined&&this.dei===undefined)n.pu();h.draw(n),o&&function(n){t.push(n);const h=t.length-1,o=200/s;e.forEach((t,e)=>{const a=e%s*o-100,i=(e/s|0)*o-100,c=[a,i,a+o,i+o];c[3]<n.aabb[1]||c[1]>n.aabb[3]||c[0]>n.aabb[2]||c[2]<n.aabb[0]||t.push(h)})}(h);if(td)n.pd();},startDeferSession:()=>{if(this.dei!==undefined)throw new Error('Finalize deferring before starting new session');this.dsi=t.length;},stopDeferring:()=>{if(this.dsi === undefined)throw new Error('Start deferring before stopping');this.dei=t.length;},finalizeDeferSession:(n)=>{if(this.dei===undefined)throw new Error('Stop deferring before finalizing');for(let i=this.dsi;i<this.dei;i++) {rpl = y(t[i].aabb,Array.from({length:this.dei-this.dsi+1}).map((e,j)=>i+j));for(let j=0;j<rpl.length&&t[i].boolean(rpl[j]);j++);t[i].draw(n);}this.dsi=undefined;this.dei=undefined;}}}