Skip to content

polygonInside

Checks if one polygon is inside another polygon.

True when every point of the first polygon lies inside the second and none of their edges cross. The layout uses this to decide whether a label’s box fits within a shape.

import {polygonInside} from "@d3plus/math";

const polyA = [
  [70, 60],
  [180, 70],
  [170, 140],
  [80, 130],
];
const polyB = [
  [20, 20],
  [240, 30],
  [230, 180],
  [30, 170],
];
polygonInside(polyA, polyB);

A polygon that pokes out of the container is not inside it, even though most of its area overlaps.

import {polygonInside} from "@d3plus/math";

const polyA = [
  [150, 60],
  [300, 70],
  [290, 140],
  [160, 130],
];
const polyB = [
  [20, 20],
  [240, 30],
  [230, 180],
  [30, 170],
];
polygonInside(polyA, polyB);

Parameters

import {polygonInside} from "@d3plus/math";
polyArequiredArray.<Point>

An Array of [x, y] points to be used as the inner polygon, checking if it is inside polyA.

polyBrequiredArray.<Point>

An Array of [x, y] points to be used as the containing polygon.