MCPcopy Index your code
hub / github.com/plotly/plotly.js / segmentsIntersect

Function segmentsIntersect

src/lib/geometry2d.js:10–27  ·  view source on GitHub ↗
(x1, y1, x2, y2, x3, y3, x4, y4)

Source from the content-addressed store, hash-verified

8 */
9exports.segmentsIntersect = segmentsIntersect;
10function segmentsIntersect(x1, y1, x2, y2, x3, y3, x4, y4) {
11 var a = x2 - x1;
12 var b = x3 - x1;
13 var c = x4 - x3;
14 var d = y2 - y1;
15 var e = y3 - y1;
16 var f = y4 - y3;
17 var det = a * f - c * d;
18 // parallel lines? intersection is undefined
19 // ignore the case where they are colinear
20 if(det === 0) return null;
21 var t = (b * f - c * e) / det;
22 var u = (b * d - a * e) / det;
23 // segments do not intersect?
24 if(u < 0 || u > 1 || t < 0 || t > 1) return null;
25
26 return {x: x1 + a * t, y: y1 + d * t};
27}
28
29/*
30 * find the minimum distance between two line segments (1->2 and 3->4)

Callers 2

geometry2d.jsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…