d3plus-shape
Version:
Fancy SVG shapes for visualizations
28 lines (27 loc) • 843 B
JavaScript
import { polygonContains } from "d3-polygon";
import segmentsIntersect from "./segmentsIntersect.js";
/**
@function polygonInside
@desc Checks if one polygon is inside another polygon.
@param {Array} polyA An Array of `[x, y]` points to be used as the inner polygon, checking if it is inside polyA.
@param {Array} polyB An Array of `[x, y]` points to be used as the containing polygon.
@returns {Boolean}
*/
export default function (polyA, polyB) {
var iA = -1;
var nA = polyA.length;
var nB = polyB.length;
var bA = polyA[nA - 1];
while (++iA < nA) {
var aA = bA;
bA = polyA[iA];
var iB = -1;
var bB = polyB[nB - 1];
while (++iB < nB) {
var aB = bB;
bB = polyB[iB];
if (segmentsIntersect(aA, bA, aB, bB)) return false;
}
}
return polygonContains(polyB, polyA[0]);
}