Skip to content

Instantly share code, notes, and snippets.

@sagea
Created October 14, 2015 04:31
Show Gist options
  • Save sagea/25e6a03f86c454cf3ed6 to your computer and use it in GitHub Desktop.
Save sagea/25e6a03f86c454cf3ed6 to your computer and use it in GitHub Desktop.
Circle & sphere interactions
// 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