Skip to content

polygonRayCast

Gives the two closest intersection points between a ray cast from a point inside a polygon. The two points should lie on opposite sides of the origin.

The third argument is the ray’s angle in radians. At π/4 the two crossings sit on the diagonal through the origin.

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

const poly = [
  [40, 30],
  [200, 20],
  [250, 100],
  [210, 180],
  [60, 170],
  [20, 90],
];
polygonRayCast(poly, [130, 100], Math.PI / 4);

Casts a line through a point inside the polygon and returns where it meets the edges on either side: the closest crossing to the left of the origin and the closest to the right. With the default angle of 0 the ray is horizontal, which is how a label’s available width at a given height is found.

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

const poly = [
  [40, 30],
  [200, 20],
  [250, 100],
  [210, 180],
  [60, 170],
  [20, 90],
];
polygonRayCast(poly, [130, 100]);

Parameters

import {polygonRayCast} from "@d3plus/math";
polyrequiredArray.<Point>

The polygon to test against, which should be an [x, y] formatted Array.

originrequiredPoint

The origin point of the ray to be cast, which should be an [x, y] formatted Array.

alphanumberDefault 0

The angle in radians of the ray.