Untitled

 avatar
unknown
python
4 years ago
1.5 kB
6
Indexable
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

YARDS_PER_MILE = 1760
SECONDS_PER_HOUR = 60*60
vp = 15 * YARDS_PER_MILE / SECONDS_PER_HOUR # runner yards per second (22/3)


def can_catch_simulation(v, vp, dt=0.01, draw=False):
	x, y = 0, 0
	xp, yp = 50, 0
	pt_x = []
	pt_y = []
	pt_xp = []
	pt_yp = []
	can_catch = False
	# simplest euler-method integration
	while yp < 100:
		target_vector = complex(xp-x, yp-y)
		v_vector = v * target_vector/abs(target_vector)
		x += v_vector.real * dt
		y += v_vector.imag * dt
		yp += vp * dt
		pt_x.append(x)
		pt_y.append(y)
		pt_xp.append(xp)
		pt_yp.append(yp)
		if y >= yp:
			can_catch = True
			break
	if draw:
		fig, ax = plt.subplots()
		xdata, ydata = [], []
		ln, = plt.plot([], [], 'bo')
		ln2, = plt.plot([], [], 'r.')
		def init():
			ax.set_xlim(0, 50)
			ax.set_ylim(0, 100)
			return ln, ln2

		def update(frame):
			ln.set_data([pt_xp[frame]], [pt_yp[frame]])
			ln2.set_data([pt_x[frame]], [pt_y[frame]])
			return ln, ln2

		ani = FuncAnimation(fig, update, frames=range(0, len(pt_x), 50), init_func=init, blit=True)

		# plt.plot(pt_xp, pt_yp, '.')
		# plt.plot(pt_x, pt_y, '.r')
		plt.show()
	return can_catch

# in mph
low_v, high_v = 0, 100
while high_v - low_v > 0.01:
	v = (low_v + high_v) / 2
	if can_catch_simulation(v * YARDS_PER_MILE / SECONDS_PER_HOUR, vp):
		high_v = v
	else:
		low_v = v
print(high_v)

can_catch_simulation(high_v * YARDS_PER_MILE / SECONDS_PER_HOUR, vp, draw=True)




Editor is loading...