## 2019年5月5日日曜日

### Python - Tuples, Points, and Vectors - Projectile, Environment

The Ray Tracer Challenge: A Test-Driven Guide to Your First 3D Renderer (Jamis Buck(著)、Pragmatic Bookshelf)、Chapter 1(Tuples, Points, and Vectors)のPut It Together(12)を取り組んでみる。

コード

Python 3

```#!/usr/bin/env python3
from tuples import Point, Vector

class Projectile:
def __init__(self, position: Point, velocity: Vector):
self.position = position
self.velocity = velocity

class Environment:
def __init__(self, gravity: Vector, wind: Vector):
self.gravity = gravity
self.wind = wind

def tick(env: Environment, proj: Projectile) -> Projectile:
position = proj.position + proj.velocity
velocity = proj.velocity + env.gravity + env.wind
return Projectile(position, velocity)

if __name__ == '__main__':
p = Projectile(Point(0, 1, 0), Vector(1, 1, 0))
e = Environment(Vector(0, -0.1, 0), Vector(-0.01, 0, 0))
projectile = tick(e, p)
while projectile.position.y > 0:
projectile = tick(e, projectile)
print(f'x = {projectile.position.x:.2f}, ' +
f'y = {projectile.position.y:.2f}')
```

```C:\Users\...>py sample3.py
x = 1.99, y = 2.90
x = 2.97, y = 3.70
x = 3.94, y = 4.40
x = 4.90, y = 5.00
x = 5.85, y = 5.50
x = 6.79, y = 5.90
x = 7.72, y = 6.20
x = 8.64, y = 6.40
x = 9.55, y = 6.50
x = 10.45, y = 6.50
x = 11.34, y = 6.40
x = 12.22, y = 6.20
x = 13.09, y = 5.90
x = 13.95, y = 5.50
x = 14.80, y = 5.00
x = 15.64, y = 4.40
x = 16.47, y = 3.70
x = 17.29, y = 2.90
x = 18.10, y = 2.00
x = 18.90, y = 1.00
x = 19.69, y = -0.10

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