Skip to content

Commit

Permalink
refactor: 线段求交时2分算法相连相邻优化
Browse files Browse the repository at this point in the history
  • Loading branch information
army8735 committed Jan 19, 2025
1 parent 838cb88 commit 349c1d4
Show file tree
Hide file tree
Showing 3 changed files with 451 additions and 49 deletions.
18 changes: 1 addition & 17 deletions src/math/bezier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ export function bezierExtremeT3(x0: number, y0: number, x1: number, y1: number,
return res;
}

// 贝塞尔曲线的极值点的t,包含默认的0和1,直线则默认就是[0, 1]
// 贝塞尔曲线的极值点的t,包含默认的0和1驻点,直线则默认就是[0, 1]
export function bezierExtremeT(x0: number, y0: number, x1: number, y1: number,
x2?: number, y2?: number, x3?: number, y3?: number) {
const len = arguments.length;
Expand Down Expand Up @@ -775,17 +775,6 @@ function getBezierMonotonicityT(points: { x: number, y: number }[], isX = true,
const p2 = isX ? points[2].x : points[2].y;
if (points.length === 4) {
const p3 = isX ? points[3].x : points[3].y;
// const t = equation
// .getRoots([
// isX ? 3 * (points[1].x - points[0].x) : 3 * (points[1].y - points[0].y),
// isX
// ? 6 * (points[2].x + points[0].x - 2 * points[1].x)
// : 6 * (points[2].y + points[0].y - 2 * points[1].y),
// isX
// ? 3 * (points[3].x + 3 * points[1].x - points[0].x - 3 * points[2].x)
// : 3 * (points[3].y + 3 * points[1].y - points[0].y - 3 * points[2].y),
// ])
// .filter((i) => i > eps&& i < 1 - eps);
const t = equation.getRoots([
3 * p1 - 3 * p0,
2 * (3 * p0 - 6 * p1 + 3 * p2),
Expand All @@ -798,11 +787,6 @@ function getBezierMonotonicityT(points: { x: number, y: number }[], isX = true,
}
}
else if (points.length === 3) {
// const t = isX
// ? (points[0].x - points[1].x) /
// (points[0].x - 2 * points[1].x + points[2].x)
// : (points[0].y - points[1].y) /
// (points[0].y - 2 * points[1].y + points[2].y);
const t = equation.getRoots([
2 * (p1 - p0),
2 * (p0 - 2 * p1 + p2),
Expand Down
7 changes: 5 additions & 2 deletions src/math/bo/intersect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import isec from '../isec';
import Point from './Point';

const EPS = 0.001;
const EPS = 0.1;
const EPS2 = 1;

export function getIntersectionLineLine(
Expand Down Expand Up @@ -241,7 +241,10 @@ export function filterIsec(res: Array<{ x: number; y: number; t1: number; t2: nu
res.splice(i, 1);
}
}
return res.map(item => {
return res.filter(item => {
// 相连排除
return !(item.t1 === 0 || item.t1 === 1 && item.t2 === 0 || item.t2 === 1);
}).map(item => {
return {
point: new Point(Math.round(item.x), Math.round(item.y)),
toSource: item.t1,
Expand Down
Loading

0 comments on commit 349c1d4

Please sign in to comment.