Last active
November 7, 2023 18:52
-
-
Save w32zhong/60f68aa37626360120edfb7a6ba3d444 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%pip install ipympl | |
%matplotlib ipympl | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from matplotlib.widgets import Slider | |
# draw initial triangle | |
triangle = np.array([ | |
[1, 4, 1, 1], | |
[3, 5, 4, 3] | |
]) | |
fig, ax = plt.subplots() | |
ax.set_xlim(-10, 10) | |
ax.set_ylim(-10, 10) | |
ax.grid(True) | |
ax.scatter(0, 0, color='red') | |
plot, = ax.plot(triangle[0], triangle[1]) | |
# define rotation updates | |
def rotation_2D(theta): | |
return np.array([ | |
[np.cos(theta), -np.sin(theta)], | |
[np.sin(theta), np.cos(theta)], | |
]) | |
def update(theta): | |
T = rotation_2D(theta) | |
T_triangle = T @ triangle | |
plot.set_data(T_triangle[0], T_triangle[1]) | |
fig.canvas.draw_idle() | |
# add a slider UI | |
fig.subplots_adjust(bottom=0.25) | |
ax = fig.add_axes([0.25, 0.1, 0.65, 0.03]) | |
slider = Slider( | |
ax=ax, | |
label='rotation', | |
valmin=0, | |
valmax=6, | |
valinit=0, | |
) | |
slider.on_changed(update) | |
# show canvas | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment