Skip to content

Instantly share code, notes, and snippets.

@joehonton
Last active June 11, 2022 01:58
Show Gist options
  • Select an option

  • Save joehonton/9f69e4b34de21df5818cb1d240023b68 to your computer and use it in GitHub Desktop.

Select an option

Save joehonton/9f69e4b34de21df5818cb1d240023b68 to your computer and use it in GitHub Desktop.

Revisions

  1. joehonton revised this gist Jun 11, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pnpoly-with-clipped-polygons.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    function isInsideClippedPolygon(polygon, mouseX, mouseY) {
    const c = false;
    let c = false;

    // make sure there are at least 3 visible nodes
    var countv = 0;
  2. joehonton revised this gist Jun 11, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pnpoly-with-clipped-polygons.js
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ function isInsideClippedPolygon(polygon, mouseX, mouseY) {
    if (countv < 3)
    return false;
    // get the first visible node
    const firstv = null;
    let firstv = null;
    for (let k=0; k < polygon.length; k++) {
    if (polygon[k].visible == true) {
    firstv = k;
  3. joehonton created this gist Apr 11, 2021.
    57 changes: 57 additions & 0 deletions pnpoly-with-clipped-polygons.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    function isInsideClippedPolygon(polygon, mouseX, mouseY) {
    const c = false;

    // make sure there are at least 3 visible nodes
    var countv = 0;
    for (let k=0; k < polygon.length && countv < 3; k++) {
    if (polygon[k].visible == true)
    countv++;
    }
    if (countv < 3)
    return false;
    // get the first visible node
    const firstv = null;
    for (let k=0; k < polygon.length; k++) {
    if (polygon[k].visible == true) {
    firstv = k;
    break;
    }
    }

    // raycast most-recent-visible-node -> next-visible-node
    const mrv = firstv;
    for (let i=mrv+1; i < polygon.length; i++) {
    if (polygon[i].visible == false)
    continue;
    let j = mrv;
    const ix = polygon[i].x;
    const iy = polygon[i].y;
    const jx = polygon[j].x;
    const jy = polygon[j].y;
    const iySide = (iy > mouseY);
    const jySide = (jy > mouseY);

    if (iySide != jySide) {
    const intersectX = (jx-ix) * (mouseY-iy) / (jy-iy) + ix;
    if (mouseX < intersectX)
    c = !c;
    }
    mrv = i;
    }

    // raycast most-recent-visible-node -> first-visible-node
    const ix = polygon[firstv].x;
    const iy = polygon[firstv].y;
    const jx = polygon[mrv].x;
    const jy = polygon[mrv].y;
    const iySide = (iy > mouseY);
    const jySide = (jy > mouseY);

    if (iySide != jySide) {
    const intersectX = (jx-ix) * (mouseY-iy) / (jy-iy) + ix;
    if (mouseX < intersectX)
    c = !c;
    }

    return c;
    }