From 9b1a4ab76e0918eab301dab4d881f4991fefa94c Mon Sep 17 00:00:00 2001 From: Yuan-ZW <448411475@qq.com> Date: Tue, 5 Nov 2024 18:58:17 +0800 Subject: [PATCH] =?UTF-8?q?fix(core):=20=E4=BF=AE=E5=A4=8D=20polyline=20?= =?UTF-8?q?=E4=B8=8E=E5=A4=9A=E8=BE=B9=E5=BD=A2=E8=8A=82=E7=82=B9=E7=9A=84?= =?UTF-8?q?=E4=BA=A4=E7=82=B9=E4=B8=8D=E6=AD=A3=E7=A1=AE=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/__tests__/algorithm/egde.test.ts | 59 +++++++++++-------- packages/core/src/algorithm/edge.ts | 6 +- 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/packages/core/__tests__/algorithm/egde.test.ts b/packages/core/__tests__/algorithm/egde.test.ts index 756dabc0a..c3c61ec16 100644 --- a/packages/core/__tests__/algorithm/egde.test.ts +++ b/packages/core/__tests__/algorithm/egde.test.ts @@ -1,4 +1,4 @@ -import { getCrossPointOfLine, isInSegment } from '../../src/algorithm/edge'; +import { getCrossPointOfLine, isInSegment } from '../../src/algorithm/edge' describe('algorithm/edge', () => { // one intersection @@ -12,7 +12,7 @@ describe('algorithm/edge', () => { x: 10, y: 10, }, - ]; + ] const line2 = [ { x: 10, @@ -22,11 +22,11 @@ describe('algorithm/edge', () => { x: 0, y: 10, }, - ]; + ] expect( getCrossPointOfLine(line1[0], line1[1], line2[0], line2[1]), - ).toBeTruthy(); - }); + ).toBeTruthy() + }) // multiple intersection test('multiple intersection', () => { const line1 = [ @@ -38,7 +38,7 @@ describe('algorithm/edge', () => { x: 10, y: 10, }, - ]; + ] const line2 = [ { x: 0, @@ -48,11 +48,11 @@ describe('algorithm/edge', () => { x: 10, y: 10, }, - ]; + ] expect( getCrossPointOfLine(line1[0], line1[1], line2[0], line2[1]), - ).toBeFalsy(); - }); + ).toBeFalsy() + }) // no intersection test('intersection', () => { const line1 = [ @@ -64,7 +64,7 @@ describe('algorithm/edge', () => { x: 10, y: 10, }, - ]; + ] const line2 = [ { x: 10, @@ -74,18 +74,18 @@ describe('algorithm/edge', () => { x: 20, y: 10, }, - ]; + ] expect( getCrossPointOfLine(line1[0], line1[1], line2[0], line2[1]), - ).toBeFalsy(); - }); + ).toBeFalsy() + }) test('in segment', () => { const point = { x: 0, y: 0, - }; - const line = [ + } + const line1 = [ { x: -10, y: -10, @@ -94,15 +94,28 @@ describe('algorithm/edge', () => { x: 10, y: 10, }, - ]; - expect(isInSegment(point, line[0], line[1])).toBeTruthy(); - }); + ] + const line2 = [ + { + x: -10, + y: 10, + }, + { + x: 10, + y: -10, + }, + ] + expect(isInSegment(point, line1[0], line2[1])).toBeTruthy() + expect(isInSegment(point, line1[1], line2[0])).toBeTruthy() + expect(isInSegment(point, line2[0], line1[1])).toBeTruthy() + expect(isInSegment(point, line2[1], line1[0])).toBeTruthy() + }) // not in segment test('not in segment', () => { const point = { x: 10, y: 0, - }; + } const line = [ { x: -10, @@ -112,7 +125,7 @@ describe('algorithm/edge', () => { x: 10, y: 10, }, - ]; - expect(isInSegment(point, line[0], line[1])).toBeFalsy(); - }); -}); + ] + expect(isInSegment(point, line[0], line[1])).toBeFalsy() + }) +}) diff --git a/packages/core/src/algorithm/edge.ts b/packages/core/src/algorithm/edge.ts index 2b0127f0e..c83783dc5 100644 --- a/packages/core/src/algorithm/edge.ts +++ b/packages/core/src/algorithm/edge.ts @@ -60,10 +60,8 @@ export const isInSegment = (point: Point, start: Point, end: Point) => { const k = (endY - startY) / (endX - startX) const b = startY - k * startX return ( - x >= startX && - x <= endX && - y >= startY && - y <= endY && + ((x >= startX && x <= endX) || (x <= startX && x >= endX)) && + ((y >= startY && y <= endY) || (y <= startY && y >= endY)) && Math.abs(y - k * x + b) < Number.EPSILON ) }