Skip to content

Instantly share code, notes, and snippets.

@FriedGil
Created November 20, 2023 03:36
Show Gist options
  • Save FriedGil/06611c847e213581a0fd97359bb1d3c7 to your computer and use it in GitHub Desktop.
Save FriedGil/06611c847e213581a0fd97359bb1d3c7 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
# %%
import math
def gamma(v:float) -> float:
return math.sqrt(1.0/(1.0-v*v))
def gamma_squared(v:float)-> float:
return 1.0/(1.0-v*v)
def relativeX(v:float, xi:float,yi:float,t:float):
return gamma_squared(v)*(xi+v*t) - gamma(v)*v*math.sqrt(gamma_squared(v)*(xi+v*t)*(xi+v*t)+yi*yi)
def contract_shape(xCoords, yCoords,v):
factor = 1/gamma(v)
stretched_x = [x * factor for x in xCoords]
stretched_y = [y * factor for y in yCoords]
return stretched_x,stretched_y
def getApparentRectanglePosition(shapeX,shapeY,v,t):
percievedX = []
for i in range(len(shapeX)):
percievedX.append(relativeX(v,shapeX[i],shapeY[i],t))
out = plt.plot(percievedX, shapeY, '-')
return out
def getRectanglePosition(shapeX,shapeY,v,t):
newX = []
for i in range(len(shapeX)):
newX.append(shapeX[i] + v*t)
out = plt.plot(newX, shapeY, '-')
return out
# %%
%matplotlib ipympl
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as patches
import matplotlib.transforms as tr
from matplotlib.widgets import Button
plt.close()
class InteractiveDrawer:
def __init__(self):
self.fig, self.ax = plt.subplots()
self.ax.clear()
self.points = []
self.cid = self.fig.canvas.mpl_connect('button_press_event', self.on_click)
def on_click(self, event):
if event.button == 1: # Left mouse button
self.points.append((event.xdata, event.ydata))
self.draw()
def draw(self):
self.ax.clear()
x, y = zip(*self.points)
plt.xlim(-20, 20)
plt.ylim(-20, 20)
self.ax.plot(x, y, marker='o', linestyle='-', color='b')
self.fig.canvas.draw()
def reset(self):
self = InteractiveDrawer()
# Create an instance of the InteractiveDrawer class
drawer = InteractiveDrawer()
# Keep the notebook running to interact with the plot
plt.show()
plt.xlim(-20, 20)
plt.ylim(-20, 20)
# %%
plt.close()
shapeX = [point[0] for point in drawer.points]
shapeY = [point[1] for point in drawer.points]
velocity = .9
shapeX,shapeY = contract_shape(shapeX,shapeY,velocity)
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# Create a figure and axis
fig, ax = plt.subplots()
ax.set_xlim(-20,20)
ax.set_ylim(-20, 20)
# Initialize an empty plot (to be updated in the animation)
rect1, = ax.plot([], [], label='Rectangle 1')
rect1Apparent, = ax.plot([], [], label='Rectangle 1 Apparent')
def init():
rect1.set_data([], [])
rect1Apparent.set_data([], [])
return rect1,rect1Apparent
# Define the update function for the animation
def update(frame):
ax.clear()
ax.set_xlim(-20,20)
ax.set_ylim(-20, 20)
time = frame / 10.0 # Adjust as needed based on your time range
rect1Apparent.set_data(getApparentRectanglePosition(shapeX,shapeY,velocity,time)[0].get_data())
rect1.set_data(getRectanglePosition(shapeX,shapeY,velocity,time)[0].get_data())
return rect1, rect1Apparent
# Set up the animation
num_frames = 200 # Adjust as needed
ani = FuncAnimation(fig, update, frames=num_frames, init_func=init, blit=True)
from matplotlib import rc
rc('animation', html='jshtml')
ax.set_xlim(-20,20)
ax.set_ylim(-20, 20)
ani
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment