[PYTHON] AtCoder Grand Contest 046: A --Animation drawing of Takahashikun, The Strider

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. image.png

If you change the argument from 90 to 136, it will look pretty cute. image.png

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). qiita2.gif

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) image.png

reference

Save anime GIFs with matplotlib without using imagemagick Animate line segments with matplotlib Draw shapes such as circles and rectangles with Matplotlib

Recommended Posts

AtCoder Grand Contest 046: A --Animation drawing of Takahashikun, The Strider
Python script to get a list of input examples for the AtCoder contest
AtCoder Grand Contest 041 Review
AtCoder Grand Contest 048 Review
AtCoder Grand Contest 045 Review
AtCoder Grand Contest 044 Review
AtCoder Grand Contest 046 Review
AtCoder Beginner Contest 166 A Explanation of Problem "A? C" (Python3, C ++, Java)
AtCoder Grand Contest 041 Participation Report
AtCoder Grand Contest 040 Participation Report
AtCoder Grand Contest 047 Participation Report
AtCoder Beginner Contest 169 A Explanation of Problem "Multiplication 1" (Python3, C ++, Java)
AtCoder Grand Contest Past Question Challenge 2
The story of participating in AtCoder
AtCoder Grand Contest Past Question Challenge 1
The story of writing a program
[AtCoder explanation] Control the A, B, C problems of ABC182 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC186 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC185 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC187 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC184 with Python!
AtCoder Beginner Contest 102 Review of past questions
AtCoder Beginner Contest 072 Review of past questions
AtCoder Beginner Contest 085 Review of past questions
AtCoder Beginner Contest 113 Review of past questions
AtCoder Beginner Contest 074 Review of past questions
AtCoder Beginner Contest 051 Review of past questions
AtCoder Beginner Contest 127 Review of past questions
AtCoder Beginner Contest 119 Review of past questions
AtCoder Beginner Contest 151 Review of past questions
AtCoder Beginner Contest 075 Review of past questions
AtCoder Beginner Contest 054 Review of past questions
AtCoder Beginner Contest 110 Review of past questions
AtCoder Beginner Contest 117 Review of past questions
AtCoder Beginner Contest 070 Review of past questions
AtCoder Beginner Contest 105 Review of past questions
AtCoder Beginner Contest 112 Review of past questions
AtCoder Beginner Contest 076 Review of past questions
Try drawing a simple animation in Python
AtCoder Beginner Contest 069 Review of past questions
AtCoder Beginner Contest 056 Review of past questions
AtCoder Beginner Contest 087 Review of past questions
AtCoder Beginner Contest 067 Review of past questions
Measure the relevance strength of a crosstab
AtCoder Beginner Contest 093 Review of past questions
AtCoder Beginner Contest 046 Review of past questions
[Python] [Explanation] AtCoder Typical DP Contest: A Contest
AtCoder Beginner Contest 049 Review of past questions
AtCoder Beginner Contest 081 Review of past questions
AtCoder Beginner Contest 047 Review of past questions
A quick overview of the Linux kernel
AtCoder Beginner Contest 060 Review of past questions
AtCoder Beginner Contest 057 Review of past questions
AtCoder Beginner Contest 121 Review of past questions
AtCoder Beginner Contest 126 Review of past questions
AtCoder Beginner Contest 103 Review of past questions
AtCoder Beginner Contest 061 Review of past questions
AtCoder Beginner Contest 059 Review of past questions
AtCoder Beginner Contest 044 Review of past questions
AtCoder Beginner Contest 083 Review of past questions
AtCoder Beginner Contest 048 Review of past questions