From fcdf13efc8fa4def7ad930c003bc689ba001f788 Mon Sep 17 00:00:00 2001 From: Chris Loer Date: Thu, 9 Mar 2023 15:58:51 -0800 Subject: [PATCH] Add line intersection unit test. --- src/symbol/projection.test.ts | 26 +++++++++++++++++++++++++- src/symbol/projection.ts | 2 +- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/symbol/projection.test.ts b/src/symbol/projection.test.ts index ee92b4c68da..251356f1c62 100644 --- a/src/symbol/projection.test.ts +++ b/src/symbol/projection.test.ts @@ -1,4 +1,4 @@ -import {project} from './projection'; +import {findIntersectionPoint, project} from './projection'; import Point from '@mapbox/point-geometry'; import {mat4} from 'gl-matrix'; @@ -9,4 +9,28 @@ describe('Projection', () => { const matrix = mat4.create(); expect(project(point, matrix).point.x).toBeCloseTo(point.x, 10); }); + + test('line intersection', () => { + const horizontal = [ + new Point(0, 0), + new Point(10, 0)]; + const vertical = [ + new Point(30, -20), + new Point(30, -10) + ]; + const intersection = findIntersectionPoint(horizontal[0], horizontal[1], vertical[0], vertical[1]); + expect(intersection).toEqual(new Point(30, 0)); + }); + + test('parallel line intersection', () => { + const first = [ + new Point(0, 0), + new Point(10, 0)]; + const second = [ + new Point(10, 0), + new Point(30, 0) + ]; + const intersection = findIntersectionPoint(first[0], first[1], second[0], second[1]); + expect(intersection).toEqual(new Point(10, 0)); + }); }); diff --git a/src/symbol/projection.ts b/src/symbol/projection.ts index e5dd87de4d9..99b9b3500ac 100644 --- a/src/symbol/projection.ts +++ b/src/symbol/projection.ts @@ -12,7 +12,7 @@ import type Transform from '../geo/transform'; import type Painter from '../render/painter'; import {WritingMode} from '../symbol/shaping'; -export {updateLineLabels, hideGlyphs, getLabelPlaneMatrix, getGlCoordMatrix, project, getPerspectiveRatio, placeFirstAndLastGlyph, placeGlyphAlongLine, xyTransformMat4}; +export {updateLineLabels, hideGlyphs, getLabelPlaneMatrix, getGlCoordMatrix, project, getPerspectiveRatio, placeFirstAndLastGlyph, placeGlyphAlongLine, xyTransformMat4, findIntersectionPoint}; /* * # Overview of coordinate spaces