-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnergyGraph.pde
More file actions
executable file
·73 lines (60 loc) · 1.89 KB
/
Copy pathEnergyGraph.pde
File metadata and controls
executable file
·73 lines (60 loc) · 1.89 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
70
71
72
73
class EnergyGraph {
float X, Y, W, H;
float total, potential, kinetic;
EnergyGraph(float X, float Y, float W, float H) {
this.X = X;
this.Y = Y;
this.W = W;
this.H = H;
}
void draw() {
pushMatrix();
translate(X, Y);
// baseline
noFill();
stroke(#6C6C6C);
strokeWeight(2);
line(20, H, W - 20, H);
if (angleGraphHover) {
total = calculatePotential(simulation.theta0);
potential = calculatePotential(angleGraphTheta);
kinetic = total - potential;
}
else if (timerRunning) {
total = calculatePotential(simulation.theta0);
potential = calculatePotential(simulation.data[timer.index]);
kinetic = total - potential;
}
else {
total = calculatePotential(simulation.theta0);
potential = calculatePotential(simulation.theta0);
kinetic = calculateKinetic(simulation.theta0);
}
float maxEnergy = calculatePotential(PI/4);
float potentialMapped = map(potential, 0, maxEnergy, H, 0);
float kineticMapped = map(kinetic, 0, maxEnergy, H, 0);
float totalMapped = map(total, 0, maxEnergy, H, 0);
noStroke();
fill(#CB4B16);
rect(W/3 - 15, potentialMapped, 30, H - potentialMapped);
rect(2*W/3 - 15, kineticMapped, 30, H - kineticMapped);
stroke(#6C6C6C);
strokeWeight(2);
line(20, totalMapped, W - 20, totalMapped);
fill(#6C6C6C);
textSize(14);
text("U", W/3 - 5, H + 20);
text("K", 2*W/3 - 5, H + 20);
text("T", W - 10, totalMapped + 5);
popMatrix();
}
float calculatePotential(float theta) {
float h = simulation.l - simulation.l * cos(theta);
return simulation.m * g * h;
}
float calculateKinetic(float theta) {
float l = simulation.l, m = simulation.m, theta0 = simulation.theta0;
float velocity = - sqrt(g/l) * sqrt(sq(theta0) - sq(theta));
return 1/2.0 * m * sq(velocity);
}
}