Home > database >  Simulate a rotating equal triangle by Python
Simulate a rotating equal triangle by Python

Time:06-30

can anyone help me, i stuck at the last step

image

this is my code. then for the last step to rotate it, i didnt know what should i do

from IPython import display
import matplotlib.pyplot as plt
import numpy as np
import matplotlib

# Enter x and y coordinates of points and colors
a=(0,0.5);b=(0.43,-0.25);c=(-0.43,-0.25)
center=(0,0)
n = 3;r=1.0
theta = np.arange(0,360 (360/(n)),360/(n))
to=np.arange(0,2*np.pi,0.01)
#t = arange(0,360,360/n)
x = r*np.sin(np.radians(theta))
y=r*np.cos(np.radians(theta))
xo=r*np.sin(to);yo=r*np.cos(to)
plt.plot(xo,yo)
plt.plot(x,y)
plt.scatter(x,y)

plt.xlim([-1, 1]);plt.ylim([-1,1])

CodePudding user response:

This is the perfect case for an animation:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib
from matplotlib.animation import FuncAnimation

# Enter x and y coordinates of points and colors
a=(0,0.5);b=(0.43,-0.25);c=(-0.43,-0.25)
center=(0,0)
n = 3;r=1.0
theta = np.arange(0,360 (360/(n)),360/(n))
to=np.arange(0,2*np.pi,0.01)

x = r * np.cos(np.radians(theta))
y = r * np.sin(np.radians(theta))
xo = r * np.cos(to); yo = r * np.sin(to)

fig, ax = plt.subplots()
ax.plot(xo,yo)

# create artists: they will be used to update the position
# of the points being rendered
triangle, = ax.plot(x,y)
vertices = ax.scatter(x,y)

lim = r * 1.25
ax.set_xlim([-lim, lim]);ax.set_ylim([-lim, lim])
ax.set_aspect("equal")

w = 2
T = 2 * np.pi / w
# this defines the time steps of the animation
dt = np.linspace(0, 10 * T, num=500)

def animate(i):
    x = r * np.cos(np.radians(theta)   w * dt[i])
    y = r * np.sin(np.radians(theta)   w * dt[i])
    # update the position of the points to be rendered
    triangle.set_data(x, y)
    vertices.set_offsets(np.stack([x, y]).T)
    ax.set_title("Rotation #%s" % int(w * dt[i] / (2 * np.pi)   1))

ani = FuncAnimation(fig, animate, frames=len(dt), repeat=False)
plt.show()

CodePudding user response:

Check this out..

from IPython import display
import matplotlib.pyplot as plt
import numpy as np
import matplotlib

# Enter x and y coordinates of points and colors
a=(0,0.5);b=(0.43,-0.25);c=(-0.43,-0.25)
center=(0,0)
n = 3;r=1.0
theta = np.arange(0,360 (360/(n)),360/(n))


w = 2
T = 2*np.pi/w
dt = np.linspace(0, 10*T, num=10) #increase num for more finely distributed rotations.

for d in dt:
    to=np.arange(0,2*np.pi,0.01)
    x = r*np.sin(np.radians(theta   d))
    y=r*np.cos(np.radians(theta   d))
    xo=r*np.sin(to);yo=r*np.cos(to)
    plt.plot(xo,yo)
    plt.plot(x,y)
    plt.scatter(x,y)


plt.xlim([-1, 1]);plt.ylim([-1,1])

enter image description here

  • Related