Skip to content

Instantly share code, notes, and snippets.

@omaraflak
Last active July 23, 2020 13:52
Show Gist options
  • Save omaraflak/3ccc990dba3898def73252a6b0ef33d2 to your computer and use it in GitHub Desktop.
Save omaraflak/3ccc990dba3898def73252a6b0ef33d2 to your computer and use it in GitHub Desktop.
def sphere_intersect(center, radius, ray_origin, ray_direction):
b = 2 * np.dot(ray_direction, ray_origin - center)
c = np.linalg.norm(ray_origin - center) ** 2 - radius ** 2
delta = b ** 2 - 4 * c
if delta > 0:
t1 = (-b + np.sqrt(delta)) / 2
t2 = (-b - np.sqrt(delta)) / 2
if t1 > 0 and t2 > 0:
return min(t1, t2)
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment