I haven't seen it, but I tried to draw a graph of problem A of AGC046. By the way, my rating is 624 right now ... I don't have a sense, but it's fun so I'm slowly moving forward step by step ❤️ mypage
If you have any problems, please go to here! I'm wondering if I can simulate with the following code, I don't know. I'm sorry if it's different.
import math
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def simulate(X):
#A list that stores the set of points that Takahashi will reach
x=[]
y=[]
#Start position
x.append(0)
y.append(0)
#How many times to return to the start can be calculated from this formula, problem A can be AC with this
K = 360 / math.gcd(X,360)
#True north at the start is the 0 degree axis
deg = 0
for i in range(int(K)): #Ngo seeking K points
next_x = x[-1] - np.sin(deg*(np.pi/180)) #Calculate the x-coordinate of the next point from the previous point and the current angle
next_y = y[-1] + np.cos(deg*(np.pi/180)) #Calculate the y-coordinate of the next point from the previous point and the current angle
x.append(next_x) #Save x points
y.append(next_y) #Save y point
deg+=X #Turn X degrees
return x,y
#When changing 90 degrees
X=90
x,y = simulate(X)
plt.plot(x, y,'b-o')
#Below, the settings to make it look easy to see in this plot
plt.axes().set_aspect('equal','datalim')
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
The execution result of the above code looks like the following.
If you change the argument from 90 to 136, it will look pretty cute.
Also, if you generate an animation with the following code,
fig = plt.figure()
imgs = []
plt.axes().set_aspect('equal','datalim')
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
for i in range(len(x)):
img = plt.plot(x[:i+1], y[:i+1], 'b-o')
imgs.append(img)
ani = animation.ArtistAnimation(fig, imgs, interval=1000)
ani.save('../qiita.gif', writer='pillow')
plt.show()
It looks like the following (when X = 136).
What do you call such a beautiful figure? ??
It's a short article, but thank you for reading ♡
import matplotlib.patches as patches
center_x = (min(x)+max(x))/2
center_y = 0.5
radius = np.sqrt(1/(2*(1-np.cos(X*(np.pi/180)))))
circle = patches.Circle(xy=(center_x, center_y), radius=radius, fc='w', ec='r')
center = patches.Circle(xy=(center_x, center_y), radius=0.01, fc='r', ec='r')
ax.add_patch(circle)
ax.add_patch(center)
I feel that the method of finding the x coordinate of the center is not strict, but it is easy and it looks like it when actually plotted, so ... lol The following is an execution example (when X = 260)
Save anime GIFs with matplotlib without using imagemagick Animate line segments with matplotlib Draw shapes such as circles and rectangles with Matplotlib
Recommended Posts