-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectile.py
More file actions
53 lines (38 loc) · 1.52 KB
/
projectile.py
File metadata and controls
53 lines (38 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# pylint: disable=W,C,R
# This script will display a project being shot out at an initial velocity
# and its trajectory. The projectile is displayed as a green pixel.
#
# Run this script via:
# ```
# poetry run projectile
# ```
from ray_tracer_challenge.tuples import *
from ray_tracer_challenge.color import *
from ray_tracer_challenge.canvas import *
class Projectile:
def __init__(self, position: Point, velocity: Vector) -> None:
self.position = position
self.velocity = velocity
class Environment:
def __init__(self, gravity: Vector, wind: Vector) -> None:
self.gravity = gravity
self.wind = wind
def tick(environment: Environment, projectile: Projectile) -> Projectile:
return Projectile(
projectile.position + projectile.velocity,
projectile.velocity + environment.gravity + environment.wind,
)
initial_position = Projectile(Point(0, 1, 0), 11.25 * (Vector(1.0, 1.8, 0).normalize()))
initial_environment = Environment(Vector(0, -0.1, 0), Vector(-0.01, 0, 0))
def run(environment: Environment, initial_position: Projectile, canvas: Canvas) -> None:
projectile = initial_position
while tick(environment, projectile).position.y >= 0.0:
projectile = tick(environment, projectile)
canvas.set_pixel(
round(projectile.position.x),
canvas.height - round(projectile.position.y),
Colors.GREEN.value,
)
canvas.show()
def projectile() -> None:
run(initial_environment, initial_position, Canvas(900, 550))