Skip to content

Commit

Permalink
use more permissive comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
itzpr3d4t0r committed Jul 25, 2024
1 parent 34e3a8d commit 609d52a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src_c/collisions.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ pgIntersection_CircleCircle(pgCircleBase *A, pgCircleBase *B,
return 0;
}

if (d2 == 0 && A->r == B->r) {
if (double_compare(d2, 0) && double_compare(A->r, B->r)) {
return 0;
}

Expand All @@ -621,7 +621,7 @@ pgIntersection_CircleCircle(pgCircleBase *A, pgCircleBase *B,
double xs2 = xm - h * (dy / d);
double ys2 = ym + h * (dx / d);

if (d2 == r_sum2 || d2 == r_diff2) {
if (double_compare(d2, r_sum2) || double_compare(d2, r_diff2)) {
intersections[0] = xs1;
intersections[1] = ys1;
return 1;
Expand Down
8 changes: 8 additions & 0 deletions src_c/include/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,12 @@ PG_FREEPOLY_COND(pgPolygonBase *poly, int was_sequence)
}
}

static int
double_compare(double a, double b)
{
/* Uses both a fixed epsilon and an adaptive epsilon */
const double e = 1e-6;
return fabs(a - b) < e || fabs(a - b) <= e * MAX(fabs(a), fabs(b));
}

#endif /* ~_GEOMETRY_H */

0 comments on commit 609d52a

Please sign in to comment.