Created
November 20, 2023 03:37
-
-
Save FriedGil/346eefdd5a7e6be813d1b331ef068986 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import math\n", | |
"\n", | |
"def gamma(v:float) -> float:\n", | |
" return math.sqrt(1.0/(1.0-v*v))\n", | |
"\n", | |
"def gamma_squared(v:float)-> float:\n", | |
" return 1.0/(1.0-v*v)\n", | |
"\n", | |
"def relativeX(v:float, xi:float,yi:float,t:float):\n", | |
" return gamma_squared(v)*(xi+v*t) - gamma(v)*v*math.sqrt(gamma_squared(v)*(xi+v*t)*(xi+v*t)+yi*yi)\n", | |
"\n", | |
"\n", | |
"def contract_shape(xCoords, yCoords,v):\n", | |
" factor = 1/gamma(v)\n", | |
" stretched_x = [x * factor for x in xCoords]\n", | |
" stretched_y = [y * factor for y in yCoords]\n", | |
" return stretched_x,stretched_y\n", | |
"\n", | |
"\n", | |
"def getApparentRectanglePosition(shapeX,shapeY,v,t):\n", | |
" percievedX = []\n", | |
" for i in range(len(shapeX)):\n", | |
" percievedX.append(relativeX(v,shapeX[i],shapeY[i],t))\n", | |
" out = plt.plot(percievedX, shapeY, '-')\n", | |
" return out\n", | |
"\n", | |
"def getRectanglePosition(shapeX,shapeY,v,t):\n", | |
" newX = []\n", | |
" for i in range(len(shapeX)):\n", | |
" newX.append(shapeX[i] + v*t)\n", | |
" out = plt.plot(newX, shapeY, '-')\n", | |
" return out" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%matplotlib ipympl\n", | |
"import matplotlib.pyplot as plt\n", | |
"import numpy as np\n", | |
"import matplotlib.patches as patches\n", | |
"import matplotlib.transforms as tr\n", | |
"from matplotlib.widgets import Button\n", | |
"\n", | |
"plt.close()\n", | |
"\n", | |
"\n", | |
"class InteractiveDrawer:\n", | |
" def __init__(self):\n", | |
" self.fig, self.ax = plt.subplots()\n", | |
" self.ax.clear()\n", | |
" self.points = []\n", | |
" self.cid = self.fig.canvas.mpl_connect('button_press_event', self.on_click)\n", | |
"\n", | |
" def on_click(self, event):\n", | |
" if event.button == 1: # Left mouse button\n", | |
" self.points.append((event.xdata, event.ydata))\n", | |
" self.draw()\n", | |
"\n", | |
" def draw(self):\n", | |
" self.ax.clear()\n", | |
" x, y = zip(*self.points)\n", | |
" plt.xlim(-20, 20)\n", | |
" plt.ylim(-20, 20)\n", | |
" self.ax.plot(x, y, marker='o', linestyle='-', color='b')\n", | |
" self.fig.canvas.draw()\n", | |
" \n", | |
" def reset(self):\n", | |
" self = InteractiveDrawer()\n", | |
"\n", | |
"# Create an instance of the InteractiveDrawer class\n", | |
"drawer = InteractiveDrawer()\n", | |
"\n", | |
"# Keep the notebook running to interact with the plot\n", | |
"plt.show()\n", | |
"plt.xlim(-20, 20)\n", | |
"plt.ylim(-20, 20)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.close()\n", | |
"shapeX = [point[0] for point in drawer.points]\n", | |
"shapeY = [point[1] for point in drawer.points]\n", | |
"velocity = .9\n", | |
"shapeX,shapeY = contract_shape(shapeX,shapeY,velocity)\n", | |
"\n", | |
"import matplotlib.pyplot as plt\n", | |
"from matplotlib.animation import FuncAnimation\n", | |
"\n", | |
"# Create a figure and axis\n", | |
"fig, ax = plt.subplots()\n", | |
"ax.set_xlim(-20,20)\n", | |
"ax.set_ylim(-20, 20)\n", | |
"\n", | |
"# Initialize an empty plot (to be updated in the animation)\n", | |
"rect1, = ax.plot([], [], label='Rectangle 1')\n", | |
"rect1Apparent, = ax.plot([], [], label='Rectangle 1 Apparent')\n", | |
"\n", | |
"\n", | |
"def init():\n", | |
" rect1.set_data([], [])\n", | |
" rect1Apparent.set_data([], [])\n", | |
" return rect1,rect1Apparent\n", | |
"\n", | |
"# Define the update function for the animation\n", | |
"def update(frame):\n", | |
" ax.clear()\n", | |
" ax.set_xlim(-20,20)\n", | |
" ax.set_ylim(-20, 20)\n", | |
" time = frame / 10.0 # Adjust as needed based on your time range\n", | |
" rect1Apparent.set_data(getApparentRectanglePosition(shapeX,shapeY,velocity,time)[0].get_data())\n", | |
" rect1.set_data(getRectanglePosition(shapeX,shapeY,velocity,time)[0].get_data())\n", | |
" return rect1, rect1Apparent\n", | |
"\n", | |
"# Set up the animation\n", | |
"num_frames = 200 # Adjust as needed\n", | |
"ani = FuncAnimation(fig, update, frames=num_frames, init_func=init, blit=True)\n", | |
"\n", | |
"from matplotlib import rc\n", | |
"rc('animation', html='jshtml')\n", | |
"ax.set_xlim(-20,20)\n", | |
"ax.set_ylim(-20, 20)\n", | |
"\n", | |
"ani" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.10.4" | |
}, | |
"orig_nbformat": 4 | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment