Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testSphereSphere divides by zero if used on two spheres that share a center #353

Open
sovdeeth opened this issue Aug 15, 2024 · 0 comments

Comments

@sovdeeth
Copy link

In the following code, if the centers are equal, distSquared will be 0 and h becomes either infinity, -infinity, or NaN depending on the radii. The infinity cases resolve on the next line when multiplied by 0, but the NaN case does not (when the two spheres are identical).

Workaround is to compare equality before running the test, but I'd prefer just being able to test any two spheres without worry.

public static boolean testSphereSphere(
            double aX, double aY, double aZ, double radiusSquaredA,
            double bX, double bY, double bZ, double radiusSquaredB) {
        double dX = bX - aX, dY = bY - aY, dZ = bZ - aZ;
        double distSquared = dX * dX + dY * dY + dZ * dZ;
        double h = 0.5 + (radiusSquaredA - radiusSquaredB) / (2.0 * distSquared);
        double r_i = radiusSquaredA - h * h * distSquared;
        return r_i >= 0.0;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant