forked from deermichel/raytracer.py
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsphere.py
executable file
·39 lines (33 loc) · 1.17 KB
/
sphere.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import math
class Sphere:
"""Sphere object"""
def __init__(self, position, radius):
"""Creates a new sphere"""
self.__position = position
self.__radius = radius
@property
def position(self):
"""Returns the position of the sphere"""
return self.__position
@property
def radius(self):
"""Returns the position of the sphere"""
return self.__radius
def intersect(self, ray):
"""Returns the intersection of a ray with this sphere"""
# http://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-sphere-intersection
l = self.__position - ray.origin
t_ca = l.dot(ray.direction)
if t_ca < 0:
return
d_squared = l.dot(l) - t_ca ** 2
radius_squared = self.__radius ** 2
if d_squared > radius_squared:
return
t_hc = math.sqrt(radius_squared - d_squared)
t = t_ca - t_hc
if t < 0.0:
t = t_ca + t_hc
hit_point = ray.origin + t * ray.direction
hit_normal = (hit_point - self.__position).normalize()
return t, hit_point, hit_normal