Created
October 14, 2015 04:31
-
-
Save sagea/25e6a03f86c454cf3ed6 to your computer and use it in GitHub Desktop.
Circle & sphere interactions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Point is inside circle. | |
function isWithinCircle(x, y, circleX, circleY, circleRadius){ | |
return square(circleX - x) + square(circleY - y) < square(circleRadius); | |
} | |
// Two circles interecting | |
function doCirclesIntersect(x1, y1, r1, x2, y2, r2){ | |
return Math.sqrt(square(x1-x2) + square(y1 - y2)) <= r1 + r2; | |
} | |
// Point is inside sphere | |
function isWithinSphere(x, y, z, circleX, circleY, circleZ, circleRadius){ | |
return square(circleX - x) + square(circleY - y) + (circleZ - z) < square(circleRadius) | |
} | |
// Spheres intersect | |
function doSpheresIntersect(x1, y1, z1, r1, x2, y2, z2, r2){ | |
return Math.sqrt(square(x1 - x2) + square(y1 - y2) + square(z1 - z2)) <= r1 + r2 | |
} | |
// Utility | |
function square(x){ | |
// Math.pow(x, 2) also works | |
return x * x; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment