Skip to content

Commit

Permalink
Fixes issue with one intersection and two roots
Browse files Browse the repository at this point in the history
  • Loading branch information
mflerackers committed Oct 25, 2024
1 parent 2fa7ac3 commit c184bd1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
1 change: 0 additions & 1 deletion examples/clip.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,3 @@ scene("circle", () => {
});

go("circle");

16 changes: 15 additions & 1 deletion src/math/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1749,11 +1749,25 @@ export function clipLineToCircle(
else {
const t1 = (-b + Math.sqrt(dis)) / (2 * a);
const t2 = (-b - Math.sqrt(dis)) / (2 * a);
if ((t1 >= 0 && t1 <= 1) || (t2 >= 0 && t2 <= 1)) {
const b1 = t1 >= 0 && t1 <= 1;
const b2 = t2 >= 0 && t2 <= 1;
if (b1 && b2) {
Vec2.addScaled(l.p1, v, t1, result.p1);
Vec2.addScaled(l.p1, v, t2, result.p2);
return true;
}
else if (b1 || b2) {
const t = b1 ? t1 : t2;
if (testCirclePoint(circle, l.p1)) {
Vec2.copy(l.p1, result.p1);
Vec2.addScaled(l.p1, v, t, result.p2);
}
else {
Vec2.addScaled(l.p1, v, t, result.p1);
Vec2.copy(l.p2, result.p2);
}
return true;
}
}

// Check if line is completely within the circle
Expand Down

0 comments on commit c184bd1

Please sign in to comment.