Skip to content

Commit 2610659

Browse files
authored
Fix some issues that may cause floating point issues (#284)
1 parent 7b89431 commit 2610659

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

content/geometry/CirclePolygonIntersection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ double circlePoly(P c, double r, vector<P> ps) {
2323
if (det <= 0) return arg(p, q) * r2;
2424
auto s = max(0., -a-sqrt(det)), t = min(1., -a+sqrt(det));
2525
if (t < 0 || 1 <= s) return arg(p, q) * r2;
26-
P u = p + d * s, v = p + d * t;
26+
P u = p + d * s, v = q + d * (t-1);
2727
return arg(p,u) * r2 + u.cross(v)/2 + arg(v,q) * r2;
2828
};
2929
auto sum = 0.0;

content/geometry/PolygonCut.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ vector<P> polygonCut(const vector<P>& poly, P s, P e) {
2727
vector<P> res;
2828
rep(i,0,sz(poly)) {
2929
P cur = poly[i], prev = i ? poly[i-1] : poly.back();
30-
bool side = s.cross(e, cur) < 0;
31-
if (side != (s.cross(e, prev) < 0))
32-
res.push_back(lineInter(s, e, cur, prev).second);
33-
if (side)
30+
auto a = s.cross(e, cur), b = s.cross(e, prev);
31+
if ((a < 0) != (b < 0))
32+
res.push_back(cur + (prev - cur) * (a / (a - b)));
33+
if (a < 0)
3434
res.push_back(cur);
3535
}
3636
return res;

0 commit comments

Comments
 (0)