## 2019年6月8日土曜日

### Python - Groups - hexagon, cylinders and spheres

The Ray Tracer Challenge: A Test-Driven Guide to Your First 3D Renderer (Jamis Buck(著)、Pragmatic Bookshelf)、Chapter 14(Groups)のPutting It Together(203)を取り組んでみる。

コード

Python 3

```#!/usr/bin/env python3
import math
import time
from camera import Camera
from tuples import Point, Vector, Color
from spheres import Sphere
from cylinders import Cylinder
from groups import Group
from transformations import translation, scaling, view_transform
from transformations import rotation_y, rotation_z
from lights import Light
from world import World

width = 250
height = 125

camera = Camera(width, height, math.pi / 4,
transform=view_transform(Point(0, 2, -5), Point(0, 0, 0),
Vector(0, 1, 0)))

def hexagon_side(transform=None):
side = Group(transform=transform)
for shape in [Sphere(transform=translation(0, 0, -1) *
scaling(0.25, 0.25, 0.25)),
Cylinder(minimum=0, maximum=1,
transform=translation(0, 0, -1) *
rotation_y(-math.pi / 6) *
rotation_z(-math.pi / 2) *
scaling(0.25, 1, 0.25))]:
return side

def hexagon():
hex = Group()
for n in range(6):
side = hexagon_side(rotation_y(n * math.pi / 3))
return hex

world = World([hexagon()],
Light(Point(-5, 5, -5), Color(1, 1, 1)))
start = time.time()
canvas = camera.render(world)
s = time.time() - start
with open(f'sample5.ppm', 'w') as f:
canvas.to_ppm(f)
print(f'ファイル名 sample5.ppm,{s}秒')
```

```C:\Users\...>py sample5.py
ファイル名 sample5.ppm,479.89529490470886秒

C:\Users\...>
```