Skip to content

Commit

Permalink
Decide the triangle orientation with a symmetric formula (in this cas…
Browse files Browse the repository at this point in the history
…e a majority vote)

Solves #43.
  • Loading branch information
Fil committed Aug 26, 2019
1 parent a12c6cc commit 371e0bd
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,14 @@ function dist(ax, ay, bx, by) {
}

function orient(px, py, qx, qy, rx, ry) {
return (qy - py) * (rx - qx) - (qx - px) * (ry - qy) < 0;
const d = cross(px, py, qx, qy, rx, ry);
if (Math.abs(d) > 1e-6) return d < 0;
// in doubt, use a majority vote
return (d < 0) + (cross(qx, qy, rx, ry, px, py) < 0) + (cross(rx, ry, px, py, qx, qy) < 0) > 1;
}

function cross(px, py, qx, qy, rx, ry) {
return (qy - py) * (rx - qx) - (qx - px) * (ry - qy);
}

function inCircle(ax, ay, bx, by, cx, cy, px, py) {
Expand Down

0 comments on commit 371e0bd

Please sign in to comment.