def _check_named_colour_value(self, name, r, g, b): test_colour = Colour(float(r), float(g), float(b)) print("\nExpected:") print(test_colour) print("\nGot:") print(getattr(world, name)) assert getattr(world, name) == test_colour
def __init__(self, width, height): self.width = width self.height = height self.pixels = [] for y in range(0, height): self.pixels.append([]) for x in range(0, width): self.pixels[y].append(Colour(0, 0, 0))
def _check_pattern_by_colour(self, pattern, px, py, pz, r, g, b): pat = getattr(world, pattern) pos = Point(float(px), float(py), float(pz)) tc = Colour(float(r), float(g), float(b)) c = pat.pattern(pos) print("\nExpected:") print(tc) print("\nGot:") print(c) assert c == tc
def _sphere_with_values(self, name): s = Sphere() # TODO: feels like we should have a generic function for parsing # gherkin tables digit = re.compile(r'^[-+]?\d*\.?\d+$') tuple3 = re.compile(r'^\([-+]?\d*\.?\d+\s*,\s*[-+]?\d*\.?\d+\s*,\s*' r'[-+]?\d*\.?\d+\)$') subattr = re.compile(r'.*\..*') scaling = re.compile(r'^scaling\([-+]?\d*\.?\d+\s*,\s*[-+]?\d*\.?\d+\s*,' r'\s*[-+]?\d*\.?\d+\)$') for row in self.table: attr = str(row[0]) obj = s print(obj, attr) while subattr.match(attr): print(attr.split('.')) obj = getattr(obj, attr.split('.', 2)[0]) attr = attr.split('.', 2)[1] print(obj, attr) value = row[1] if digit.match(value): print("DIGIT", attr, float(value)) setattr(obj, attr, float(value)) if tuple3.match(value): values = value.replace("(", "").replace(")", "").split(',') print("TUPLE3", attr, float(values[0]), float(values[1]), float(values[2])) setattr( obj, attr, Colour(float(values[0]), float(values[1]), float(values[2]))) if scaling.match(value): values = value.split('(')[1].replace("(", "").replace(")", "").\ split(',') print( "SCALING", attr, Transformation.Scaling(float(values[0]), float(values[1]), float(values[2]))) setattr( obj, attr, Transformation.Scaling(float(values[0]), float(values[1]), float(values[2]))) setattr(world, name, s)
def _set_material_stripe_pattern(self, name, r1, g1, b1, r2, g2, b2): m = getattr(world, name) c1 = Colour(float(r1), float(g1), float(b1)) c2 = Colour(float(r2), float(g2), float(b2)) m.pattern = Stripe(c1, c2)
#!/usr/bin/env python from Tuple4 import Point, Colour import Canvas import math def plot(canvas, point, colour): x = int((canvas.width / 2) + round(point.x)) y = int((canvas.height / 2) - round(point.y)) c.write_pixel(x, y, colour) def plot_cross(canvas, point, colour): x = int((canvas.width / 2) + round(point.x)) y = int((canvas.height / 2) - round(point.y)) c.write_pixel(x, y, colour) c.write_pixel(x + 1, y, colour) c.write_pixel(x - 1, y, colour) c.write_pixel(x, y + 1, colour) c.write_pixel(x, y - 1, colour) white = Colour(0.8, 0.8, 0.8) c = Canvas.Canvas(600, 600) p = Point(0, 250, 0) for r in range(0, 12): p2 = p.rotate_z(-r * math.pi / 6) plot_cross(c, p2, white) c.to_ppm_file("clock.ppm")
def _fill_canvas_colour(self, name, r, g, b): canvas = getattr(world, name) colour = Colour(float(r), float(g), float(b)) for x in range(0, canvas.width): for y in range(0, canvas.height): canvas.write_pixel(x, y, colour)
def _canvas_one_colour(self, name, red, green, blue): test_colour = Colour(float(red), float(green), float(blue)) canvas = getattr(world, name) for x in range(0, canvas.width): for y in range(0, canvas.height): assert canvas.pixel_at(x, y) == test_colour
def _point_light_by_number(self, name, x, y, z, r, g, b): setattr( world, name, PointLight(Point(float(x), float(y), float(z)), Colour(float(r), float(g), float(b))))
def pixel_at(self, x, y): p = self.pixels[y][x] return Colour(p.red, p.green, p.blue)