import math
import matplotlib
from matplotlib.animation import FuncAnimation as FA
from matplotlib.patches import Ellipse as mpE
from matplotlib.patches import Polygon as mpP
import matplotlib.pyplot as plt
def plotHelper(option) :
fig = plt.figure(figsize = (12, 12))
ax = fig.add_subplot(xlim = (-1.01, 1.01),
ylim = (-1.01, 1.01))
#Remove axes and ticks.
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.spines["left"].set_visible(False)
ax.set_xticks([])
ax.set_yticks([])
circ = mpE(xy = (0, 0),
width = 2,
height = 2,
edgecolor = 'k',
facecolor = '#440154FF')
ax.add_patch(circ)
sp = math.sqrt(3) / 2
sn = -1 * sp
if option == "Fiddler" :
quads = mpP([(-1, 0), (-0.5, sp), (0.5, sp), (1,0),
(0.5, sn), (-0.5, sn), (-1, 0), (1, 0)],
edgecolor = 'k',
facecolor = '#2A788EFF')
ax.add_patch(quads)
elif option == "EC1" :
quad = mpP([(0, 1), (1, 0), (0, -1), (0, 1)],
edgecolor = 'k',
facecolor = '#2A788EFF')
tri = mpP([(-1, 0), (0, 1), (0, -1), (-1, 0)],
edgecolor = 'k',
facecolor = '#7AD151FF')
ax.add_patch(quad)
ax.add_patch(tri)
else :
quad = mpP([(0.5, sp), (1, 0), (0.5, sn), (0.5, sp)],
edgecolor = 'k',
facecolor = '#2A788EFF')
tri = mpP([(-1, 0), (0.5, sp), (0.5, sn), (-1, 0)],
edgecolor = 'k',
facecolor = '#7AD151FF')
ax.add_patch(quad)
ax.add_patch(tri)
if option == "EC3" :
length = mpP([(-1, 0), (1, 0)],
edgecolor = 'k')
rad = mpP([(0, 0), (0.5, sp)],
edgecolor = 'k', ls = ':')
root = mpP([(0.54, 0.335), (0.7, 0.335)],
edgecolor = 'k', lw = 0.85)
ax.add_patch(length)
ax.add_patch(rad)
ax.add_patch(root)
text = [("r", (-0.5, -0.05)),
("r", (0.25, 0.3)),
("a", (0.25, -0.05)),
("√r²-a²", (0.6, 0.3)),
("r-a", (0.75, -0.05))]
for t in text :
plt.annotate(t[0], t[1], c = "k", fontsize = 24,
ha = "center", va = "center")
fig.savefig("2023.11.10" + option + ".png", bbox_inches='tight')
plotHelper("Fiddler")
plotHelper("EC1")
plotHelper("EC2")
plotHelper("EC3")
matplotlib.rc_file_defaults()
fig = plt.figure(figsize = (10.5, 7))
ax = fig.add_subplot(xlim = (-1.01, 2.02),
ylim = (-1.01, 1.01))
ax.set_title("Maximizing the Quadrilateral + Triangle Areas", fontsize = 24)
#Remove axes and ticks.
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.spines["left"].set_visible(False)
ax.set_xticks([])
ax.set_yticks([])
font = {'size' : 20}
#Eight artists:
#Circle, Quadrilateral, Triangle, Length.
#Length Text, Quadrilateral Area, Triangle Area, Total Area
circ = mpE(xy = (0, 0),
width = 2,
height = 2,
edgecolor = 'k',
facecolor = '#440154FF')
quad = mpP([(-1, 0)],
edgecolor = 'k',
facecolor = '#2A788EFF')
tri = mpP([(-1, 0)],
edgecolor = 'k',
facecolor = '#7AD151FF')
leng = mpP([(-1, 0)],
edgecolor = 'k')
length_text = ax.text(-0.95, 0, '', ha = 'left', **font)
quad_text = ax.text(1.03, 0.3, '', **font, color = '#2A788EFF')
tri_text = ax.text(1.03, 0, '', **font, color = '#7AD151FF')
tot_text = ax.text(1.03, -0.3, '', **font)
def init():
"""Initialize seven artists."""
ax.add_patch(circ)
ax.add_patch(quad)
ax.add_patch(tri)
ax.add_patch(leng)
length_text.set_text('')
quad_text.set_text('')
tri_text.set_text('')
tot_text.set_text('')
return circ, quad, tri, leng, length_text, quad_text, tri_text, tot_text
def animate(i):
a = i/1000
x = i/1000 - 1
yp = math.sqrt(1- x**2)
yn = -1 * yp
quad_a = 2*yp / math.pi
tri_a = a*yp / math.pi
tot_a = (2+a)*yp / math.pi
"""Update eight artists."""
quad.set_xy([(-1, 0), (x, yp), (1, 0), (x, yn), (-1, 0)])
tri.set_xy([(-1, 0), (x, yp), (x, yn), (-1, 0)])
leng.set_xy([(-1, 0), (x, 0)])
length_text.set_text("%.4fr" % a)
quad_text.set_text("Quad: %.4f%% of Circle" % quad_a)
tri_text.set_text("Triangle: %.4f%% of Circle" % tri_a)
tot_text.set_text("Total: %.4f%% of Circle" % tot_a)
if i > 413 :
length_text.set_x(x)
length_text.set_ha("right")
return circ, quad, tri, leng, length_text, quad_text, tri_text, tot_text
#Run animation.
anim = FA(fig, animate, init_func = init, frames = 2000, interval = 5, blit = True)
#Save animation.
anim.save('2023.11.10 EC.mp4');