Permalink
Browse files

Added planes

  • Loading branch information...
adusca committed Dec 27, 2014
1 parent a9c55cf commit f5a6c0a5d6f65dbede2bbee595305a8f64e32d8d
Showing with 42 additions and 11 deletions.
  1. +28 −3 geometry.py
  2. +14 −8 render.py
View
@@ -10,15 +10,15 @@ def __init__(self, slope, start):
self.start = start
@staticmethod
def throughPoints(point0, point1):
def through_points(point0, point1):
"""
Creates a line that passes in two given points
"""
x1, y1, z1 = point1
x0, y0, z0 = point0
return Line((x1 - x0, y1 - y0, z1 - z0), (x0, y0, z0))
def distanceToPoint(self, point):
def distance_to_point(self, point):
"""
Returns the distance between a line and a point
"""
@@ -29,6 +29,25 @@ def distanceToPoint(self, point):
proj = vec.project(v)
return (proj.subtract(vec)).norm()
def point_after_t(self, num):
p = Vector(self.start)
q = Vector(self.slope)
return p.add(q.mul_by_number(num))
class HorizontalPlane():
def __init__(self, cte, color):
self.cte = cte
self.color = color
def intersect(self, line):
return line.slope[2] != 0
def intersection_value(self, line):
return float(self.cte - line.start[2])/line.slope[2]
def color_at_point(self, p):
return (0, 200, 0) if int(p[0]) % 2 == int(p[1]) % 2 else (0, 0, 200)
class Sphere():
def __init__(self, center, radius, color):
"""
@@ -41,7 +60,7 @@ def __init__(self, center, radius, color):
self.color = color
def intersect(self, line):
return line.distanceToPoint(self.center) <= self.radius
return line.distance_to_point(self.center) <= self.radius
def intersection_value(self, line):
v = Vector(line.slope)
@@ -52,6 +71,9 @@ def intersection_value(self, line):
B = sum(-2*x*y for (x, y) in zip(v.coords, q.coords))
C = sum(y*y for y in q.coords) - self.radius**2
return solve_quadradic((A, B, C))
def color_at_point(self, p):
return self.color
class Vector():
def __init__(self, coords):
@@ -63,6 +85,9 @@ def scalar_product(self, v2):
def subtract(self, v2):
return Vector([self.coords[0] - v2.coords[0], self.coords[1] - v2.coords[1], self.coords[2] - v2.coords[2]])
def add(self, v2):
return Vector([self.coords[0] + v2.coords[0], self.coords[1] + v2.coords[1], self.coords[2] + v2.coords[2]])
def norm(self):
return sqrt(sum(x**2 for x in self.coords))
View
@@ -12,27 +12,33 @@ def coordinates(num, sz = size):
"""
return 4.0*num/(sz - 1) - 2.0
# Creating the world
S1 = Sphere((1, 0, 0), 0.5, (10, 220, 0))
S2 = Sphere((0.6, 0, 0), 0.5, (200, 10, 200))
def create_world():
S1 = Sphere((0, 0, 0), 0.5, (100, 220, 0))
S2 = Sphere((0, 0, 0), 1, (200, 10, 200))
S3 = Sphere((1, 1, -3), 0.4, (100, 100, 200))
P = HorizontalPlane(0, (200, 200, 200))
return [S1, S2, S3, P]
world = create_world()
camera = (0, 0, -8)
world = [S1, S2]
light = (0, 10, 0)
# Colouring each pixel
for i in range(size):
x0 = coordinates(i)
for j in range(size):
y0 = coordinates(j)
l = Line.throughPoints(camera, (x0, y0, -5))
l = Line.through_points(camera, (x0, y0, -5))
ans = []
for S in world:
if S.intersect(l):
ans.append((S.intersection_value(l), S))
t = S.intersection_value(l)
p = l.point_after_t(t)
ans.append((t, S, p.coords))
if not ans:
rays[i, j] = (255, 255, 255)
else:
ans.sort()
rays[i, j] = ans[0][1].color
rays[i, j] = ans[0][1].color_at_point(ans[0][2])
tmp.save(path)

0 comments on commit f5a6c0a

Please sign in to comment.