Skip to content

Commit

Permalink
slightly faster formula
Browse files Browse the repository at this point in the history
  • Loading branch information
itzpr3d4t0r committed Jul 12, 2024
1 parent 017dcf3 commit 957c887
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions src_c/collisions.c
Original file line number Diff line number Diff line change
Expand Up @@ -593,36 +593,35 @@ static int
pgIntersection_CircleCircle(pgCircleBase *A, pgCircleBase *B,
double *intersections)
{
double x1 = A->x;
double y1 = A->y;
double r1 = A->r;
double x2 = B->x;
double y2 = B->y;
double r2 = B->r;

if (x1 == x2 && y1 == y2 && r1 == r2)
double dx = B->x - A->x;
double dy = B->y - A->y;
double d2 = dx * dx + dy * dy;
double r_sum = A->r + B->r;
double r_diff = A->r - B->r;
double r_sum2 = r_sum * r_sum;
double r_diff2 = r_diff * r_diff;

if (d2 > r_sum2 || d2 < r_diff2) {
return 0;
}

double dx = x2 - x1;
double dy = y2 - y1;
double d = sqrt(dx * dx + dy * dy);

if (d > r1 + r2 || d < fabs(r1 - r2)) {
if (d2 == 0 && A->r == B->r) {
return 0;
}

double a = (r1 * r1 - r2 * r2 + d * d) / (2 * d);
double h = sqrt(r1 * r1 - a * a);
double d = sqrt(d2);
double a = (d2 + A->r * A->r - B->r * B->r) / (2 * d);
double h = sqrt(A->r * A->r - a * a);

double xm = x1 + a * (x2 - x1) / d;
double ym = y1 + a * (y2 - y1) / d;
double xm = A->x + a * (dx / d);
double ym = A->y + a * (dy / d);

double xs1 = xm + h * (y2 - y1) / d;
double ys1 = ym - h * (x2 - x1) / d;
double xs2 = xm - h * (y2 - y1) / d;
double ys2 = ym + h * (x2 - x1) / d;
double xs1 = xm + h * (dy / d);
double ys1 = ym - h * (dx / d);
double xs2 = xm - h * (dy / d);
double ys2 = ym + h * (dx / d);

if (d == r1 + r2 || d == fabs(r1 - r2)) {
if (d2 == r_sum2 || d2 == r_diff2) {
intersections[0] = xs1;
intersections[1] = ys1;
return 1;
Expand Down

0 comments on commit 957c887

Please sign in to comment.