Initial commit
This commit is contained in:
38
sim_billiards.py
Normal file
38
sim_billiards.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from turtle import *
|
||||
from math import sqrt
|
||||
from geometry.validation import expect_type
|
||||
from geometry.point import Point
|
||||
from geometry.vector import Vector
|
||||
from simulation import Simulation
|
||||
from simulation.ball import Ball
|
||||
|
||||
def get_ball_positions(origin, spacing, columns=5):
|
||||
"""Returns a list of Points in a triangle, growing to the right of position.
|
||||
spacing is the distance between each point,
|
||||
columns is the number of columns in the triangle, and
|
||||
jiggle is the amount of randomness to add to each ball's position.
|
||||
"""
|
||||
expect_type(origin, Point)
|
||||
positions = []
|
||||
col_spacing = spacing * sqrt(3) / 2
|
||||
for col in range(columns):
|
||||
offset_x = col_spacing * col
|
||||
for row in range(col + 1):
|
||||
offset_y = spacing * (row - col / 2)
|
||||
offset = Vector((offset_x, offset_y))
|
||||
positions.append(origin + offset)
|
||||
return positions
|
||||
|
||||
r = 10
|
||||
positions = get_ball_positions(Point((100, 0)), 2 * r + 2)
|
||||
rack = [Ball(p, radius=r, color="red") for p in positions]
|
||||
cue_ball = Ball(
|
||||
Point((-250, 0)),
|
||||
radius=r,
|
||||
velocity=Vector((9, 0)),
|
||||
mass=1,
|
||||
color="white"
|
||||
)
|
||||
|
||||
sim = Simulation(rack + [cue_ball], e=1, boundary=250)
|
||||
sim.run(1000)
|
||||
Reference in New Issue
Block a user