Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def render(self, canvas):
fovRadians = math.pi * (self.fieldOfView / 2.0) / 180.0
halfWidth = math.tan(fovRadians)
halfHeight = 0.75 * halfWidth
width = halfWidth * 2
height = halfHeight * 2
pixelWidth = width / (canvas.width - 1)
pixelHeight = height / (canvas.height - 1)
eye = Ray(self.position, self.lookingAt - self.position)
vpRight = eye.vector.cross(Vector.UP).normalized()
vpUp = vpRight.cross(eye.vector).normalized()
for y in range(canvas.height):
for x in range(canvas.width):
xcomp = vpRight.scale(x * pixelWidth - halfWidth)
ycomp = vpUp.scale(y * pixelHeight - halfHeight)
ray = Ray(eye.point, eye.vector + xcomp + ycomp)
colour = self.rayColour(ray)
canvas.plot(x, y, *colour)
def _lightIsVisible(self, l, p):
for (o, s) in self.objects:
t = o.intersectionTime(Ray(p, l - p))
if t is not None and t > EPSILON:
return False
return True
def colourAt(self, scene, ray, p, normal):
b = self.baseColourAt(p)
c = (0, 0, 0)
if self.specularCoefficient > 0:
reflectedRay = Ray(p, ray.vector.reflectThrough(normal))
reflectedColour = scene.rayColour(reflectedRay)
c = addColours(c, self.specularCoefficient, reflectedColour)
if self.lambertCoefficient > 0:
lambertAmount = 0
for lightPoint in scene.visibleLights(p):
contribution = (lightPoint - p).normalized().dot(normal)
if contribution > 0:
lambertAmount = lambertAmount + contribution
lambertAmount = min(1, lambertAmount)
c = addColours(c, self.lambertCoefficient * lambertAmount, b)
if self.ambientCoefficient > 0:
c = addColours(c, self.ambientCoefficient, b)
return c
halfWidth = math.tan(fovRadians)
halfHeight = 0.75 * halfWidth
width = halfWidth * 2
height = halfHeight * 2
pixelWidth = width / (canvas.width - 1)
pixelHeight = height / (canvas.height - 1)
eye = Ray(self.position, self.lookingAt - self.position)
vpRight = eye.vector.cross(Vector.UP).normalized()
vpUp = vpRight.cross(eye.vector).normalized()
for y in range(canvas.height):
for x in range(canvas.width):
xcomp = vpRight.scale(x * pixelWidth - halfWidth)
ycomp = vpUp.scale(y * pixelHeight - halfHeight)
ray = Ray(eye.point, eye.vector + xcomp + ycomp)
colour = self.rayColour(ray)
canvas.plot(x, y, *colour)