commit 4b5d35cecd3548202afb8026652803b325b2c962 Author: Anson Date: Fri Jul 26 03:46:33 2019 -0700 init commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..924388c --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Double Pendulum In Processing.py + +Simple Port of the marvelous Daniel Shiffman from The Coding Train's double pendulum Processing sketch to Python. + +![Double Pendulum Animation](animation.webm) diff --git a/animation.webm b/animation.webm new file mode 100644 index 0000000..ab69294 Binary files /dev/null and b/animation.webm differ diff --git a/double_pendulum.mkv b/double_pendulum.mkv new file mode 100644 index 0000000..b873816 Binary files /dev/null and b/double_pendulum.mkv differ diff --git a/double_pendulum.py b/double_pendulum.py new file mode 100644 index 0000000..9658270 --- /dev/null +++ b/double_pendulum.py @@ -0,0 +1,80 @@ +def setup(): + size(900, 600) + global canvas, cx, cy + cx = width / 2 + cy = 200 + canvas = createGraphics(width, height) + canvas.beginDraw() + canvas.background(255) + canvas.endDraw() + + global r1, r2, m1, m2, a1, a2, a1_v, a2_v, g, px2, py2 + r1 = 200 + r2 = 200 + m1 = 40 + m2 = 40 + a1 = PI / 2 + a2 = PI / 2 + a1_v = 0 + a2_v = 0 + g = 1 + + px2 = -1 + py2 = -1 + + +def draw(): + + background(255) + imageMode(CORNER) + image(canvas, 0, 0, width, height) + global r1, r2, m1, m2, a1, a2, a1_v, a2_v, g, px2, py2 + num1 = -g * (2 * m1 + m2) * sin(a1) + num2 = -m2 * g * sin(a1 - 2 * a2) + num3 = -2 * sin(a1 - a2) * m2 + num4 = a2_v * a2_v * r2 + a1_v * a1_v * r1 * cos(a1 - a2) + den = r1 * (2 * m1 + m2 - m2 * cos(2 * a1 - 2 * a2)) + a1_a = (num1 + num2 + num3 * num4) / den + + num1 = 2 * sin(a1 - a2) + num2 = a1_v * a1_v * r1 * (m1 + m2) + num3 = g * (m1 + m2) * cos(a1) + num4 = a2_v * a2_v * r2 * m2 * cos(a1 - a2) + den = r2 * (2 * m1 + m2 - m2 * cos(2 * a1 - 2 * a2)) + a2_a = (num1 * (num2 + num3 + num4)) / den + + translate(cx, cy) + stroke(0) + strokeWeight(2) + + x1 = r1 * sin(a1) + y1 = r1 * cos(a1) + + x2 = x1 + r2 * sin(a2) + y2 = y1 + r2 * cos(a2) + + line(0, 0, x1, y1) + fill(0) + ellipse(x1, y1, m1, m1) + + line(x1, y1, x2, y2) + fill(0) + ellipse(x2, y2, m2, m2) + + a1_v += a1_a + a2_v += a2_a + a1 += a1_v + a2 += a2_v + + canvas.beginDraw() + canvas.translate(cx, cy) + canvas.stroke(0) + + if frameCount > 1: + canvas.line(px2, py2, x2, y2) + + canvas.endDraw() + + px2 = x2 + py2 = y2 + diff --git a/double_pendulum.pyde b/double_pendulum.pyde new file mode 100644 index 0000000..251b2ff --- /dev/null +++ b/double_pendulum.pyde @@ -0,0 +1,78 @@ + +def setup(): + size(900, 600) + global canvas, cx, cy + cx = width / 2 + cy = 200 + canvas = createGraphics(width, height) + canvas.beginDraw() + canvas.background(255) + canvas.endDraw() + global r1, r2, m1, m2, a1, a2, a1_v, a2_v, g, px2, py2 + r1 = 200 + r2 = 200 + m1 = 40 + m2 = 40 + a1 = PI / 2 + a2 = PI / 2 + a1_v = 0 + a2_v = 0 + g = 1 + + px2 = -1 + py2 = -1 + +def draw(): + + background(255) + imageMode(CORNER) + image(canvas, 0, 0, width, height) + global r1, r2, m1, m2, a1, a2, a1_v, a2_v, g, px2, py2 + num1 = -g * (2 * m1 + m2) * sin(a1) + num2 = -m2 * g * sin(a1 - 2 * a2) + num3 = -2 * sin(a1 - a2) * m2 + num4 = a2_v * a2_v * r2 + a1_v * a1_v * r1 * cos(a1 - a2) + den = r1 * (2 * m1 + m2 - m2 * cos(2 * a1 - 2 * a2)) + a1_a = (num1 + num2 + num3 * num4) / den + + num1 = 2 * sin(a1 - a2) + num2 = a1_v * a1_v * r1 * (m1 + m2) + num3 = g * (m1 + m2) * cos(a1) + num4 = a2_v * a2_v * r2 * m2 * cos(a1 - a2) + den = r2 * (2 * m1 + m2 - m2 * cos(2 * a1 - 2 * a2)) + a2_a = (num1 * (num2 + num3 + num4)) / den + + translate(cx, cy) + stroke(0) + strokeWeight(2) + + x1 = r1 * sin(a1) + y1 = r1 * cos(a1) + + x2 = x1 + r2 * sin(a2) + y2 = y1 + r2 * cos(a2) + + line(0, 0, x1, y1) + fill(0) + ellipse(x1, y1, m1, m1) + + line(x1, y1, x2, y2) + fill(0) + ellipse(x2, y2, m2, m2) + + a1_v += a1_a + a2_v += a2_a + a1 += a1_v + a2 += a2_v + + canvas.beginDraw() + canvas.translate(cx, cy) + canvas.stroke(0) + + if frameCount > 1: + canvas.line(px2, py2, x2, y2) + + canvas.endDraw() + + px2 = x2 + py2 = y2 diff --git a/doublependulum.pde b/doublependulum.pde new file mode 100644 index 0000000..fbed226 --- /dev/null +++ b/doublependulum.pde @@ -0,0 +1,92 @@ +// Daniel Shiffman +// http://codingtra.in +// http://patreon.com/codingtrain + +// Double Pendulum +// https://youtu.be/uWzPe_S-RVE + +float r1 = 200; // length of first pendulum +float r2 = 200; // length of second pendulum +float m1 = 40; // mass of first pendulum excluding weight of string +float m2 = 40; // mass of second pendulum excluding weight of string +float a1 = PI/2; // angle formed by first pendulum and normal - angle1 +float a2 = PI/2; //angle formed by second pendulum and normal - angle2 +float a1_v = 0; //angular velocity of pendulum1 +float a2_v = 0; //angular velocity of pendulum2 +float g = 1; //gravitational constant (realistic value not considered for simplicity ) + +float px2 = -1; // previous position of second pendulum sphere - x offset +float py2 = -1; // previos position of second pendulum sphere - y offset +float cx, cy; //centre of x and y for background + +PGraphics canvas; // canvas is just a variable name DO NOT CONFUSE IT WITH P5.JS + +void setup() { + size(900, 600); + cx = width/2; + cy = 200; + canvas = createGraphics(width, height); + canvas.beginDraw(); + canvas.background(255); + canvas.endDraw(); +} + +void draw() { + background(255); + imageMode(CORNER); + image(canvas, 0, 0, width, height); + // numerators are moduled + float num1 = -g * (2 * m1 + m2) * sin(a1); + float num2 = -m2 * g * sin(a1-2*a2); + float num3 = -2*sin(a1-a2)*m2; + float num4 = a2_v*a2_v*r2+a1_v*a1_v*r1*cos(a1-a2); + float den = r1 * (2*m1+m2-m2*cos(2*a1-2*a2)); + float a1_a = (num1 + num2 + num3*num4) / den; + + num1 = 2 * sin(a1-a2); + num2 = (a1_v*a1_v*r1*(m1+m2)); + num3 = g * (m1 + m2) * cos(a1); + num4 = a2_v*a2_v*r2*m2*cos(a1-a2); + den = r2 * (2*m1+m2-m2*cos(2*a1-2*a2)); + float a2_a = (num1*(num2+num3+num4)) / den; + + translate(cx, cy); + stroke(0); + strokeWeight(2); + + float x1 = r1 * sin(a1); + float y1 = r1 * cos(a1); + + float x2 = x1 + r2 * sin(a2); + float y2 = y1 + r2 * cos(a2); + + + line(0, 0, x1, y1); + fill(0); + ellipse(x1, y1, m1, m1); + + line(x1, y1, x2, y2); + fill(0); + ellipse(x2, y2, m2, m2); + + a1_v += a1_a; + a2_v += a2_a; + a1 += a1_v; + a2 += a2_v; + // as momentum increases , slowly pendulum comes to rest + // a1_v *= 0.99; // for drag + // a2_v *= 0.99; // for drag + + canvas.beginDraw(); + //canvas.background(0, 1); + canvas.translate(cx, cy); + canvas.stroke(0); + if (frameCount > 1) { + canvas.line(px2, py2, x2, y2); + } + canvas.endDraw(); + + + px2 = x2; + py2 = y2; +} \ No newline at end of file diff --git a/sketch.properties b/sketch.properties new file mode 100644 index 0000000..2456b0a --- /dev/null +++ b/sketch.properties @@ -0,0 +1,2 @@ +mode=Python +mode.id=jycessing.mode.PythonMode