import math
import matplotlib
from matplotlib.animation import FuncAnimation as FA
from matplotlib.patches import Rectangle as MPR
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import random
import seaborn as sns
#For each stop in a list of stops, it returns a new list composed of the 3 directions Amare
#can take from each stop, and the likelihood of each happening.
#A stop is (x, y, w), which represents the coordinate and weight.
def getNewStops(stops):
newstops = []
#Look at each stop.
for stop in stops:
x = stop[0]
y = stop[1]
w = stop[2]
#The tangential slope of the stop.
m_t = -1 * (0 - x) / (0 - y)
#The radial slope could be undefined.
if m_t == 0:
m = float('inf')
#Most of the times, it is not.
else :
m = (0 - y) / (0 - x)
#Amare could go tangentially left. This happens 1/4 of the time.
tangential_left = (x - math.sqrt(1 / (1 + m_t**2)),
y - m_t * math.sqrt(1 / (1 + m_t**2)),
w / 4)
#Amare could go radially. This happens 1/2 of the time.
#Vertical.
if m == float('inf'):
if y > 0:
radial = (0, y + 1, w / 2)
#All others.
elif y > 0:
radial = (x + m/abs(m) * math.sqrt(1 / (1 + m**2)),
y + abs(m) * math.sqrt(1 / (1 + m**2)),
w / 2)
else :
radial = (x - m/abs(m) * math.sqrt(1 / (1 + m**2)),
y - abs(m) * math.sqrt(1 / (1 + m**2)),
w / 2)
#Amare could go tangentially right. This happens 1/4 of the time.
tangential_right = (x + math.sqrt(1 / (1 + m_t**2)),
y + m_t * math.sqrt(1 / (1 + m_t**2)),
w / 4)
newstops.append(tangential_left)
newstops.append(radial)
newstops.append(tangential_right)
return(newstops)
all_stops = [[(0, 0, 1)],
[(0, 1, 1)]]
#Get next iteration of stops from last iteration.
for s in range(2, 11):
all_stops.append(getNewStops(all_stops[-1]))
def avgDist(stops) :
def dist(point):
return(math.sqrt(point[0]**2 + point[1]**2))
avgdist = 0
for point in stops:
avgdist += dist(point[0:2])*point[2]
return(avgdist)
def AmareWalk() :
#Plot Setup.
matplotlib.rc_file_defaults()
fig = plt.figure(figsize = (8, 8))
ax = fig.add_subplot(111, xlim = (-8.5, 8.5), ylim = (-6.5, 10.5))
ax.set_title("Where Does Amare End Up?", fontsize = 14)
#Remove axes and ticks.
for side in ['bottom', 'left', 'top', 'right']:
ax.spines[side].set_visible(False)
ax.set_xticks([])
ax.set_yticks([])
#Footnote font size.
font = {'size' : 14}
#Three artists:
#Generation text, Number text, Total text.
a_text = ax.text(-8.5, -7, '', **font)
ev_text = ax.text(-1, -7, '', **font)
scat = ax.scatter([], [], c = "brown")
def init():
a_text.set_text('')
ev_text.set_text('')
return a_text, ev_text
def animate(i):
x = []
y = []
w = []
stops = all_stops[i]
for p in stops:
x.append(p[0])
y.append(p[1])
w.append(p[2]*4096)
scat.set_offsets(np.c_[x, y])
scat.set_sizes(w)
#Update footnote text.
a_text.set_text("Distance Traveled: %.0f" % (i))
ev_text.set_text("Average Distance from Center: %.4f" % (avgDist(stops)))
return a_text, ev_text
#Run animation.
anim = FA(fig, animate, init_func = init, frames = 11, interval = 2000, blit = False)
#Save animation.
anim.save('2022.05.20 Express.mp4');
AmareWalk()