Skip to content

Instantly share code, notes, and snippets.

@iliakonnov
Last active January 15, 2019 18:00
Show Gist options
  • Save iliakonnov/ffe27d100c49c6b6bf59145afe14b870 to your computer and use it in GitHub Desktop.
Save iliakonnov/ffe27d100c49c6b6bf59145afe14b870 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
import matplotlib.lines as mlines
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.60)
plt.grid(True)
MIN = -10
MAX = 10
class Container:
sub = None
main = None
t = np.arange(MIN, MAX, 0.001)
Container.main = (1*(1 + 1*t - 1) + 1*(1 + 1*t - 1)) / np.sqrt(
(1**2 + 1**2) * ((1 + 1*t - 1)**2 + (1 + 1*t - 1)**2)
)
Container.sub = np.cos(1 * t)
diff = main - sub
main_plot, = plt.plot(t, Container.main, lw=2, color='red')
sub_plot, = plt.plot(t, Container.sub, lw=1, color='blue', linestyle=':')
diff_plot, = plt.plot(t, diff, lw=1, color='green')
A_circle = plt.scatter(1, 1, 100)
B_circle = plt.scatter(1, 1, 100)
v_arrow, = plt.plot([1], [1], marker = '')
u_arrow, = plt.plot([1], [1], marker = '')
plt.axis([MIN, MAX, MIN, MAX])
axcolor = 'lightgoldenrodyellow'
arg_vx = plt.axes([0.125, 0.10, 0.65, 0.03], facecolor=axcolor)
arg_vy = plt.axes([0.125, 0.15, 0.65, 0.03], facecolor=axcolor)
arg_ux = plt.axes([0.125, 0.20, 0.65, 0.03], facecolor=axcolor)
arg_uy = plt.axes([0.125, 0.25, 0.65, 0.03], facecolor=axcolor)
arg_Ax = plt.axes([0.125, 0.30, 0.65, 0.03], facecolor=axcolor)
arg_Ay = plt.axes([0.125, 0.35, 0.65, 0.03], facecolor=axcolor)
arg_Bx = plt.axes([0.125, 0.40, 0.65, 0.03], facecolor=axcolor)
arg_By = plt.axes([0.125, 0.45, 0.65, 0.03], facecolor=axcolor)
arg_omega = plt.axes([0.125, 0.50, 0.65, 0.03], facecolor=axcolor)
s_vx = Slider(arg_vx, 'v_x', MIN, MAX, valinit=1)
s_vy = Slider(arg_vy, 'v_y', MIN, MAX, valinit=1)
s_ux = Slider(arg_ux, 'u_x', MIN, MAX, valinit=1)
s_uy = Slider(arg_uy, 'u_y', MIN, MAX, valinit=1)
s_Ax = Slider(arg_Ax, 'A_x', MIN, MAX, valinit=1)
s_Ay = Slider(arg_Ay, 'A_y', MIN, MAX, valinit=1)
s_Bx = Slider(arg_Bx, 'B_x', MIN, MAX, valinit=1)
s_By = Slider(arg_By, 'B_y', MIN, MAX, valinit=1)
s_omega = Slider(arg_omega, 'omega', 0, MAX, valinit=1)
def update(val):
vx = s_vx.val
vy = s_vy.val
ux = s_ux.val
uy = s_uy.val
Ax = s_Ax.val
Ay = s_Ay.val
Bx = s_Bx.val
By = s_By.val
v_arrow.set_xdata([Ax, Ax + vx])
v_arrow.set_ydata([Ay, Ay + vy])
u_arrow.set_xdata([Bx, Bx + ux])
u_arrow.set_ydata([By, By + uy])
A_circle.set_offsets([[Ax, Ay]])
B_circle.set_offsets([[Bx, By]])
Container.main = (vx*(Bx + ux*t - Ax) + vy*(By + uy*t - Ay)) / np.sqrt(
(vx**2 + vy**2) * ((Bx + ux*t - Ax)**2 + (Bx + uy*t - Ay)**2)
)
main_plot.set_ydata(Container.main)
diff_plot.set_ydata(Container.main - Container.sub)
fig.canvas.draw_idle()
def update_omega(val):
omega = s_omega.val
Container.sub = np.sin(omega * t)
sub_plot.set_ydata(Container.sub)
diff_plot.set_ydata(Container.main - Container.sub)
for i in (s_vx, s_vy, s_ux, s_uy, s_Ax, s_Ay, s_Bx, s_By):
i.on_changed(update)
s_omega.on_changed(update_omega)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment