-
Notifications
You must be signed in to change notification settings - Fork 1
/
dart.py
126 lines (95 loc) · 4.06 KB
/
dart.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Wedge
def plot_dartboard(x, y):
colors = ['black', 'red'] * 10
patches = []
# Define the radii for the bands
radii = [0.1, 0.2, 0.35, 0.55, 0.80, 1.0]
# Plot the bands of colors
for i in range(len(radii) - 1):
color = colors[i]
wedge = Wedge((0, 0), radii[i + 1], 0, 360, width=radii[i + 1] - radii[i], facecolor=color)
plt.gca().add_patch(wedge)
plt.scatter(x, y, marker='+', s=100, c='w')
plt.axis('scaled')
plt.gca().set_facecolor("grey")
def simulate_3d_dart_throw(angle_x, angle_y,
initial_position=None,
initial_velocity=15.0,
time_step=0.01,
plot=False):
'''
This function solves the trajectory of the dart.
Use the required inputs: angle_x and angle_y
and pass scalar values (not arrays) to the function. This function returns the position of the dart
where it (approximately) hits the board.
You can set the plot
flag to True to visualize where a random throw
with these input angles would land.
'''
# Convert the angles from degrees to radians
if initial_position is None:
initial_position = [0, 0, 0]
sigma = 1
angle_x += sigma * np.random.randn(1)
angle_y += sigma * np.random.randn(1)
angle_x = np.radians(angle_x)
angle_y = np.radians(angle_y)
# Initialize lists to store the position of the dart at each time step
x_positions = [initial_position[0]]
y_positions = [initial_position[1] + 0.5]
z_positions = [initial_position[2]]
# Calculate the initial velocity components
initial_velocity_x = initial_velocity * np.cos(angle_x) * np.cos(angle_y)
initial_velocity_y = initial_velocity * np.sin(angle_x) * np.cos(angle_y)
initial_velocity_z = initial_velocity * np.sin(angle_y)
# Simulate the dart throw until it hits the ground (z position becomes negative)
while x_positions[-1] <= 5:
# Calculate the new position and velocity
new_x = x_positions[-1] + initial_velocity_x * time_step
new_y = y_positions[-1] + initial_velocity_y * time_step
new_z = z_positions[-1] + initial_velocity_z * time_step - 0.5 * 9.8 * time_step ** 2
new_velocity_x = initial_velocity_x
new_velocity_y = initial_velocity_y
new_velocity_z = initial_velocity_z - 9.8 * time_step
# Update the position and velocity
x_positions.append(new_x)
y_positions.append(new_y)
z_positions.append(new_z)
initial_velocity_x = new_velocity_x
initial_velocity_y = new_velocity_y
initial_velocity_z = new_velocity_z
if plot: plot_dartboard(y_positions[-1], z_positions[-1])
return np.reshape(np.array([y_positions[-1], z_positions[-1]]), [-1, 2])
def plot_hits_on_dartboard(angle_x, angle_y):
hits = []
for i in range(10):
# Simulate the 3D dart throw
hits.append(simulate_3d_dart_throw(angle_x, angle_y, plot=False))
hits = np.array(hits).squeeze()
print('Optimal_theta_x =', round(angle_x, 2),'°')
print('Optimal_theta_y =', round(angle_y, 2),'°')
print('Average distance to bulls eye = {:f} '.format(np.mean(np.square(hits))))
plot_dartboard(hits[:, 0], hits[:, 1])
plt.savefig('darts.png')
def find_optimal_throw_angles():
'''
Implement your solution here.
You can use the function simulate_3d_dart_throw
to test a few trial angles to get
some data to work with.
To goal is to find optimal angles
in x and y directions
such that the darts land close to the origin (0,0).
Once you have your solution, commit it to a new branch
and create a pull request on GitHub.
'''
optimal_theta_x = 0
optimal_theta_y = 0
return optimal_theta_x, optimal_theta_y
if __name__ == "__main__":
# Don't change the following lines:
theta_x, theta_y = find_optimal_throw_angles()
plot_hits_on_dartboard(angle_x=theta_x, angle_y=theta_y)