Skip to content

Commit b56f4ea

Browse files
committed
move lineIntersect -> Lib.geometry2d
1 parent ee7a839 commit b56f4ea

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

src/components/annotations/draw.js

+2-23
Original file line numberDiff line numberDiff line change
@@ -487,15 +487,15 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
487487
// to get the parity of the number of intersections.
488488
if(edges.reduce(function(a, x) {
489489
return a ^
490-
!!lineIntersect(headX, headY, headX + 1e6, headY + 1e6,
490+
!!Lib.segmentsIntersect(headX, headY, headX + 1e6, headY + 1e6,
491491
x[0], x[1], x[2], x[3]);
492492
}, false)) {
493493
// no line or arrow - so quit drawArrow now
494494
return;
495495
}
496496

497497
edges.forEach(function(x) {
498-
var p = lineIntersect(tailX, tailY, headX, headY,
498+
var p = Lib.segmentsIntersect(tailX, tailY, headX, headY,
499499
x[0], x[1], x[2], x[3]);
500500
if(p) {
501501
tailX = p.x;
@@ -701,24 +701,3 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
701701
}
702702
else annText.call(textLayout);
703703
}
704-
705-
// look for intersection of two line segments
706-
// (1->2 and 3->4) - returns array [x,y] if they do, null if not
707-
function lineIntersect(x1, y1, x2, y2, x3, y3, x4, y4) {
708-
var a = x2 - x1,
709-
b = x3 - x1,
710-
c = x4 - x3,
711-
d = y2 - y1,
712-
e = y3 - y1,
713-
f = y4 - y3,
714-
det = a * f - c * d;
715-
// parallel lines? intersection is undefined
716-
// ignore the case where they are colinear
717-
if(det === 0) return null;
718-
var t = (b * f - c * e) / det,
719-
u = (b * d - a * e) / det;
720-
// segments do not intersect?
721-
if(u < 0 || u > 1 || t < 0 || t > 1) return null;
722-
723-
return {x: x1 + a * t, y: y1 + d * t};
724-
}

src/lib/geometry2d.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright 2012-2017, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
/*
12+
* look for intersection of two line segments
13+
* (1->2 and 3->4) - returns array [x,y] if they do, null if not
14+
*/
15+
exports.segmentsIntersect = function segmentsIntersect(x1, y1, x2, y2, x3, y3, x4, y4) {
16+
var a = x2 - x1,
17+
b = x3 - x1,
18+
c = x4 - x3,
19+
d = y2 - y1,
20+
e = y3 - y1,
21+
f = y4 - y3,
22+
det = a * f - c * d;
23+
// parallel lines? intersection is undefined
24+
// ignore the case where they are colinear
25+
if(det === 0) return null;
26+
var t = (b * f - c * e) / det,
27+
u = (b * d - a * e) / det;
28+
// segments do not intersect?
29+
if(u < 0 || u > 1 || t < 0 || t > 1) return null;
30+
31+
return {x: x1 + a * t, y: y1 + d * t};
32+
};

src/lib/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ lib.rotationXYMatrix = matrixModule.rotationXYMatrix;
7474
lib.apply2DTransform = matrixModule.apply2DTransform;
7575
lib.apply2DTransform2 = matrixModule.apply2DTransform2;
7676

77+
var geom2dModule = require('./geometry2d');
78+
lib.segmentsIntersect = geom2dModule.segmentsIntersect;
79+
lib.segmentDistance = geom2dModule.segmentDistance;
80+
7781
var extendModule = require('./extend');
7882
lib.extendFlat = extendModule.extendFlat;
7983
lib.extendDeep = extendModule.extendDeep;

0 commit comments

Comments
 (0)