-
-
Save jkwok91/90e1999b78e491c7e5bc to your computer and use it in GitHub Desktop.
multiple pendulums
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
class Pendulum { | |
PVector origin; | |
PVector bob; | |
float arm; // length of arm | |
float r; | |
float angle, aV, aA; | |
Pendulum(float x, float y, float a) { | |
origin = new PVector(x, y); | |
bob = new PVector(origin.x, arm); | |
arm = 150.0; | |
r = 20; | |
angle = a; | |
aV = 0.0; | |
aA = 0.0; | |
} | |
void swing() { | |
aA = -0.00981*sin(angle); | |
aV += aA; | |
aV *= 0.999; | |
angle += aV; | |
bob.x = origin.x+arm*sin(angle); | |
bob.y = origin.y+arm*cos(angle); | |
} | |
void display() { | |
stroke(255); | |
line(origin.x, origin.y, bob.x, bob.y); | |
ellipse(bob.x, bob.y, r, r); | |
} | |
} |
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
int w = 800; | |
int h = 300; | |
int cx, cy; | |
float theta; | |
int numPends; | |
float G = 0.981; | |
PVector gravity; | |
Pendulum[] pends; | |
void setup() { | |
size(w, h); | |
background(0); | |
cx = width/2; | |
cy = height/2; | |
gravity = new PVector(0, G); | |
theta = 0.0; | |
setupPends(); | |
} | |
void setupPends() { | |
numPends = 5; | |
pends = new Pendulum[numPends]; | |
for (int i = 0; i < numPends; i++) { | |
Pendulum p = new Pendulum(cx, 0, theta-(i*(theta/numPends))); | |
pends[i] = p; | |
} | |
} | |
void draw() { | |
background(0); | |
text("click to start", cx, cy+50); | |
for (int i = 0; i < pends.length; i++) { | |
Pendulum p = pends[i]; | |
p.swing(); | |
p.display(); | |
} | |
} | |
void mouseReleased() { | |
float dx = mouseX - cx; | |
float dy = mouseY; | |
theta = atan(dx/dy); | |
setupPends(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment