Skip to content

Commit 47fe4a2

Browse files
author
Hannah
authored
Merge pull request #7228 from shehzan10/line-segment-intersection
Add line segment-line segment intersection
2 parents 9bf381c + 1d7e177 commit 47fe4a2

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Change Log
55

66
##### Additions :tada:
77
* Added `Scene.invertClassificationSupported` for checking if invert classification is supported.
8+
* Added `computeLineSegmentLineSegmentIntersection` to `Intersections2D`. [#7228](https://github.com/AnalyticalGraphicsInc/Cesium/pull/7228)
89

910
##### Fixes :wrench:
1011
* Fixed issue causing polyline to look wavy depending on the position of the camera [#7209](https://github.com/AnalyticalGraphicsInc/cesium/pull/7209)

Source/Core/Intersections2D.js

+59
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
define([
2+
'./Cartesian2',
23
'./Cartesian3',
4+
'./Check',
35
'./defined',
46
'./DeveloperError'
57
], function(
8+
Cartesian2,
69
Cartesian3,
10+
Check,
711
defined,
812
DeveloperError) {
913
'use strict';
@@ -278,5 +282,60 @@ define([
278282
return new Cartesian3(l1, l2, l3);
279283
};
280284

285+
/**
286+
* Compute the intersection between 2 line segments
287+
*
288+
* @param {Number} x00 The x coordinate of the first line's first vertex.
289+
* @param {Number} y00 The y coordinate of the first line's first vertex.
290+
* @param {Number} x01 The x coordinate of the first line's second vertex.
291+
* @param {Number} y01 The y coordinate of the first line's second vertex.
292+
* @param {Number} x10 The x coordinate of the second line's first vertex.
293+
* @param {Number} y10 The y coordinate of the second line's first vertex.
294+
* @param {Number} x11 The x coordinate of the second line's second vertex.
295+
* @param {Number} y11 The y coordinate of the second line's second vertex.
296+
* @param {Cartesian2} [result] The instance into to which to copy the result. If this parameter
297+
* is undefined, a new instance is created and returned.
298+
* @returns {Cartesian2} The intersection point, undefined if there is no intersection point or lines are coincident.
299+
*
300+
* @example
301+
* var result = Cesium.Intersections2D.computeLineSegmentLineSegmentIntersection(0.0, 0.0, 0.0, 2.0, -1, 1, 1, 1);
302+
* // result === new Cesium.Cartesian2(0.0, 1.0);
303+
*/
304+
Intersections2D.computeLineSegmentLineSegmentIntersection = function(x00, y00, x01, y01, x10, y10, x11, y11, result) {
305+
//>>includeStart('debug', pragmas.debug);
306+
Check.typeOf.number('x00', x00);
307+
Check.typeOf.number('y00', y00);
308+
Check.typeOf.number('x01', x01);
309+
Check.typeOf.number('y01', y01);
310+
Check.typeOf.number('x10', x10);
311+
Check.typeOf.number('y10', y10);
312+
Check.typeOf.number('x11', x11);
313+
Check.typeOf.number('y11', y11);
314+
//>>includeEnd('debug');
315+
316+
var numerator1A = (x11 - x10) * (y00 - y10) - (y11 - y10) * (x00 - x10);
317+
var numerator1B = (x01 - x00) * (y00 - y10) - (y01 - y00) * (x00 - x10);
318+
var denominator1 = (y11 - y10) * (x01 - x00) - (x11 - x10) * (y01 - y00);
319+
320+
// If denominator = 0, then lines are parallel. If denominator = 0 and both numerators are 0, then coincident
321+
if (denominator1 === 0) {
322+
return;
323+
}
324+
325+
var ua1 = numerator1A / denominator1;
326+
var ub1 = numerator1B / denominator1;
327+
328+
if (ua1 >= 0 && ua1 <= 1 && ub1 >= 0 && ub1 <= 1) {
329+
if (!defined(result)) {
330+
result = new Cartesian2();
331+
}
332+
333+
result.x = x00 + ua1 * (x01 - x00);
334+
result.y = y00 + ua1 * (y01 - y00);
335+
336+
return result;
337+
}
338+
};
339+
281340
return Intersections2D;
282341
});

Specs/Core/Intersections2DSpec.js

+54
Original file line numberDiff line numberDiff line change
@@ -288,4 +288,58 @@ defineSuite([
288288
expect(result3.z).toBeGreaterThan(0.0);
289289
});
290290
});
291+
292+
describe('computeLineSegmentLineSegmentIntersection', function() {
293+
it('returns the correct result for intersection point', function() {
294+
var intersection0 = Intersections2D.computeLineSegmentLineSegmentIntersection(0.0, 0.0, 0.0, 2.0, -1.0, 1.0, 1.0, 1.0);
295+
expect(intersection0.x).toEqualEpsilon(0.0, 1e-15);
296+
expect(intersection0.y).toEqualEpsilon(1.0, 1e-15);
297+
298+
var intersection1 = Intersections2D.computeLineSegmentLineSegmentIntersection(0.0, 0.0, 10.0, 5.0, 0.0, 5.0, 10.0, 0.0);
299+
expect(intersection1.x).toEqualEpsilon(5.0, 1e-15);
300+
expect(intersection1.y).toEqualEpsilon(2.5, 1e-15);
301+
302+
var intersection2 = Intersections2D.computeLineSegmentLineSegmentIntersection(0.0, -5.0, 4.0, 3.0, -2.0, 1.0, 4.0, -2.0);
303+
expect(intersection2.x).toEqualEpsilon(2.0, 1e-15);
304+
expect(intersection2.y).toEqualEpsilon(-1.0, 1e-15);
305+
});
306+
307+
it('returns the correct result for intersection point on a vertex', function() {
308+
var intersection0 = Intersections2D.computeLineSegmentLineSegmentIntersection(0.0, 0.0, 0.0, 2.0, -1.0, 0.0, 1.0, 0.0);
309+
expect(intersection0.x).toEqualEpsilon(0.0, 1e-15);
310+
expect(intersection0.y).toEqualEpsilon(0.0, 1e-15);
311+
312+
var intersection1 = Intersections2D.computeLineSegmentLineSegmentIntersection(0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 2.0, 0.0);
313+
expect(intersection1.x).toEqualEpsilon(1.0, 1e-15);
314+
expect(intersection1.y).toEqualEpsilon(1.0, 1e-15);
315+
316+
var intersection2 = Intersections2D.computeLineSegmentLineSegmentIntersection(0.0, 0.0, 4.0, 3.0, 5.0, 0.0, 4.0, 3.0);
317+
expect(intersection2.x).toEqualEpsilon(4.0, 1e-15);
318+
expect(intersection2.y).toEqualEpsilon(3.0, 1e-15);
319+
});
320+
321+
it('returns undefined for non-intersecting lines', function() {
322+
var intersection0 = Intersections2D.computeLineSegmentLineSegmentIntersection(0.0, 0.0, 0.0, 5.0, 0.1, 4.8, 5.0, 0.0);
323+
expect(intersection0).toBeUndefined();
324+
325+
var intersection1 = Intersections2D.computeLineSegmentLineSegmentIntersection(10.0, 0.0, 0.0, -10.0, 0.0, 0.0, -8.0, -8.0);
326+
expect(intersection1).toBeUndefined();
327+
});
328+
329+
it('returns undefined for parallel lines', function() {
330+
var intersection0 = Intersections2D.computeLineSegmentLineSegmentIntersection(0.0, 0.0, 0.0, 2.0, 1.0, 1.0, 1.0, 4.0);
331+
expect(intersection0).toBeUndefined();
332+
333+
var intersection1 = Intersections2D.computeLineSegmentLineSegmentIntersection(1.0, 1.0, 4.0, 4.0, 0.0, 0.0, 3.0, 3.0);
334+
expect(intersection1).toBeUndefined();
335+
});
336+
337+
it('returns undefined for coincident lines', function() {
338+
var intersection0 = Intersections2D.computeLineSegmentLineSegmentIntersection(0.0, 0.0, 0.0, 2.0, 0.0, 1.0, 0.0, 4.0);
339+
expect(intersection0).toBeUndefined();
340+
341+
var intersection1 = Intersections2D.computeLineSegmentLineSegmentIntersection(0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 2.0);
342+
expect(intersection1).toBeUndefined();
343+
});
344+
});
291345
});

0 commit comments

Comments
 (0)