Skip to content

Commit

Permalink
fix(core): 修复 polyline 与多边形节点的交点不正确的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuan-ZW authored and DymoneLewis committed Nov 7, 2024
1 parent ae8bb8c commit 9b1a4ab
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
59 changes: 36 additions & 23 deletions packages/core/__tests__/algorithm/egde.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCrossPointOfLine, isInSegment } from '../../src/algorithm/edge';
import { getCrossPointOfLine, isInSegment } from '../../src/algorithm/edge'

describe('algorithm/edge', () => {
// one intersection
Expand All @@ -12,7 +12,7 @@ describe('algorithm/edge', () => {
x: 10,
y: 10,
},
];
]
const line2 = [
{
x: 10,
Expand All @@ -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 = [
Expand All @@ -38,7 +38,7 @@ describe('algorithm/edge', () => {
x: 10,
y: 10,
},
];
]
const line2 = [
{
x: 0,
Expand All @@ -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 = [
Expand All @@ -64,7 +64,7 @@ describe('algorithm/edge', () => {
x: 10,
y: 10,
},
];
]
const line2 = [
{
x: 10,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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()
})
})
6 changes: 2 additions & 4 deletions packages/core/src/algorithm/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

0 comments on commit 9b1a4ab

Please sign in to comment.