From cccb84c9df3a8801649206332b89649751a0de7f Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Thu, 23 Jan 2025 19:42:41 +0700 Subject: [PATCH] Fix some issues that may cause floating point issues --- content/geometry/CirclePolygonIntersection.h | 2 +- content/geometry/PolygonCut.h | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/content/geometry/CirclePolygonIntersection.h b/content/geometry/CirclePolygonIntersection.h index ab8e72030..2ef3d5e1b 100644 --- a/content/geometry/CirclePolygonIntersection.h +++ b/content/geometry/CirclePolygonIntersection.h @@ -23,7 +23,7 @@ double circlePoly(P c, double r, vector

ps) { if (det <= 0) return arg(p, q) * r2; auto s = max(0., -a-sqrt(det)), t = min(1., -a+sqrt(det)); if (t < 0 || 1 <= s) return arg(p, q) * r2; - P u = p + d * s, v = p + d * t; + P u = p + d * s, v = q + d * (t-1); return arg(p,u) * r2 + u.cross(v)/2 + arg(v,q) * r2; }; auto sum = 0.0; diff --git a/content/geometry/PolygonCut.h b/content/geometry/PolygonCut.h index e056eb786..623d1bcfe 100644 --- a/content/geometry/PolygonCut.h +++ b/content/geometry/PolygonCut.h @@ -27,10 +27,10 @@ vector

polygonCut(const vector

& poly, P s, P e) { vector

res; rep(i,0,sz(poly)) { P cur = poly[i], prev = i ? poly[i-1] : poly.back(); - bool side = s.cross(e, cur) < 0; - if (side != (s.cross(e, prev) < 0)) - res.push_back(lineInter(s, e, cur, prev).second); - if (side) + auto a = s.cross(e, cur), b = s.cross(e, prev); + if ((a < 0) != (b < 0)) + res.push_back(cur + (prev - cur) * (a / (a - b))); + if (a < 0) res.push_back(cur); } return res;