-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.py
More file actions
27 lines (22 loc) · 870 Bytes
/
objects.py
File metadata and controls
27 lines (22 loc) · 870 Bytes
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
from ray import Ray
from math import sqrt
class Sphere:
"""This is object sphere"""
def __init__(self, center, radius, material):
self.center = center
self.radius = radius
self.material = material
def intersect(self, ray):
"""Find a intersection with the sphere"""
ray_origin_to_center = ray.origin - self.center
b = ray.direction.dot(ray_origin_to_center)
c = ray_origin_to_center.dot(ray_origin_to_center) - self.radius * self.radius
discriminant = b * b - c
if discriminant >= 0:
distance_coefficient = -b - sqrt(discriminant)
if distance_coefficient > 0:
return distance_coefficient
return None
def normal(self, surf_point):
"""Return surface normal of sphere"""
return (surf_point - self.center).normalize()