Skip to content

lineIntersection

Finds the intersection point (if there is one) of the lines p1q1 and p2q2.

Each line is given by two [x, y] points. The result is where the two lines meet; here the segments themselves cross, so the point lies on both.

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

lineIntersection([20, 160], [240, 40], [20, 40], [240, 160]);

The points define infinite lines, not bounded segments, so an intersection is returned even when it falls past the ends of both segments (dashed). Use segmentsIntersect when the segments themselves must cross.

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

lineIntersection([20, 160], [100, 120], [160, 40], [240, 80]);

Parallel (or coincident) lines never meet, so the result is null instead of a point.

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

lineIntersection([20, 60], [240, 60], [20, 140], [240, 140]);

Parameters

import {lineIntersection} from "@d3plus/math";
p1requiredPoint

The first point of the first line segment, which should always be an [x, y] formatted Array.

q1requiredPoint

The second point of the first line segment, which should always be an [x, y] formatted Array.

p2requiredPoint

The first point of the second line segment, which should always be an [x, y] formatted Array.

q2requiredPoint

The second point of the second line segment, which should always be an [x, y] formatted Array.