-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeometry.py
More file actions
110 lines (82 loc) · 2.39 KB
/
geometry.py
File metadata and controls
110 lines (82 loc) · 2.39 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
Class to represent the scene and shapes for raytracing
"""
from numpy import array,dot
import math
class Material:
def __init__(self):
self.color = (0,0,0)
self.ambient = 0.0
self.diffuse = 0.0
self.specular = 0.0
def set_color(self, rgb):
"""
rgb is a 3-tuple that specifies red,green,blue values of a material
"""
self.color = rgb
def set_ambient(self, amb_val):
self.ambient = amb_val
def set_diffuse(self, diff_val):
self.diffuse = diff_val
def set_specular(self, spec_val):
self.specular = spec_val
def set_lighting(self, light_vals):
"""
light_vals should be a 3-tuple of (ambient, diffuse, specular)
"""
self.ambient, self.diffuse, self.specular = light_vals
def get_lighting(self):
"""
Returns a 3-tuple of (ambient, diffuse, specular)
"""
return (self.ambient, self.diffuse, self.specular)
class Shape:
"""
Basic class to represent a shape in a raytracing scene
Class vars:
pos = a 3-tuple representing the x,y,z position of the shape in space
"""
def __init__(self, pos):
self.pos = pos
def set_position(self, pos):
self.pos = pos
def get_position(self):
return self.pos
def set_material(self, material):
self.material = material
def intersection(self, Ray):
pass
def get_color(self):
return self.material.color
class Square(Shape):
def intersection(self, Ray):
pass
class Sphere(Shape):
def __init__(self, center, radius):
self.center = array(center)
self.radius = float(radius)
def intersects(self, ray):
#a = d . d
#b = 2 d . (p0 - pc)
#c = (p0 - pc) . (p0 - pc) - r2
p0 = ray.origin
pc = self.center
d = ray.direction
r = self.radius
a = dot(d,d)
b = dot(2*d, p0-pc)
c = dot((p0 - pc), (p0-pc)) - r**2
try:
val1 = (-b+math.sqrt(b**2 - (4*a*c)))/(2*a)
val2 = (-b-math.sqrt(b**2 - (4*a*c)))/(2*a)
except ValueError:
return False
return True
class Scene:
def __init__(self, x,y):
self.x = x
self.y = y
def get_width(self):
return self.x
def get_height(self):
return self.y