#!/usr/bin/env python
from Tuple4 import Point, Colour
from Canvas import Canvas
from Sphere import Sphere
from Ray import Ray
from Transformation import Scaling, Rotation_z, Shearing
from math import pi


ray_origin = Point(0, 0, -5)
wall_z = 10.0
wall_size = 7.0
canvas_pixels = 100
pixel_size = wall_size / canvas_pixels
half = wall_size / 2
red = Colour(0.8, 0, 0)
s = Sphere()

# Try various transforms
# shrink it along the y axis
s.transform = Scaling(1, 0.5, 1)
# shrink it along the x axis
s.transform = Scaling(0.5, 1, 1)
# shrink it, and rotate it!
s.transform = Rotation_z(pi / 4) * Scaling(0.5, 1, 1)
# shrink it, and skew it!
s.transform = Shearing(1, 0, 0, 0, 0, 0) * Scaling(0.5, 1, 1)

c = Canvas(canvas_pixels, canvas_pixels)

for y in range(0, canvas_pixels):
    world_y = half - pixel_size * y

    for x in range(0, canvas_pixels):
        world_x = -half + pixel_size * x

        position = Point(world_x, world_y, wall_z)
        r = Ray(ray_origin, (position - ray_origin).normalize())
        xs = s.intersects(r)
        if xs.hit():
            c.write_pixel(x, y, red)

c.to_ppm_file('sphere-test.ppm')
