-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpendulums_sim.pde
More file actions
69 lines (53 loc) · 1.39 KB
/
pendulums_sim.pde
File metadata and controls
69 lines (53 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
int n = 7; //number of pendulums.
float m = 20; //all bobs have the same mass.
int[] bob_color = new int[n];
float[] r = new float[n];
float[] a = new float[n];
float[] a_v = new float[n];
float[] a_a = new float[n];
float g = 1; //free fall acc.
void setup(){
size(600,600);
set_start(a, 0.3, n); //setting starting angle.
set_start(a_v, 0, n); //setting starting angular velocity.
set_start(a_a, 0, n); //assigning numbers to the angular acceleratoin array.
set_bob_color();
set_length(100, 550);
}
void draw(){
background(255);
stroke(0);
strokeWeight(2);
//make point 0,0 at the middle top
translate(width/2, height/12);
for (int i = 0; i < n; i++)
{
strokeWeight(0.4);
float xi = r[i] * sin(a[i]);
float yi = r[i] * cos(a[i]);
line(0, 0, xi, yi);
fill(bob_color[i]);
ellipse(xi, yi, m, m);
a_a[i] = -g/r[i] * a[i];
a_v[i] += a_a[i] * 2;
//a_v[i] *= 0.995; //friction.
a[i] += a_v[i];
}
// Add delay on first frame (showing release angle more clearly).
if (frameCount == 2)
delay(200);
}
void set_start(float[] arr, float elem, int n){
for (int i = 0; i<n; i++)
arr[i] = elem;
}
void set_bob_color(){
int inc = 255/n;
for (int i = 0; i < n; i++)
bob_color[n-i-1] = inc * i;
}
void set_length(float s, float f){
float inc = (f - s)/n;
for (int i = 0; i < n; i ++)
r[i] = s + inc * i;
}