-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
86 lines (71 loc) · 1.78 KB
/
sketch.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
let r1 = 125;
let r2 = 125;
let m1 = 10;
let m2 = 10;
let a1 = 0;
let a2 = 0;
let a1_v = 0;
let a2_v = 0;
let g = 1;
let px2 = -1;
let py2 = -1;
let cx, cy;
let buffer;
function setup() {
var canvas = createCanvas(windowWidth, windowHeight);
canvas.parent("canvas");
//Issue with wrong rendering on a retina Mac. See issue: https://github.com/CodingTrain/website/issues/574
pixelDensity(1);
a1 = PI / 2;
a2 = PI / 2;
cx = windowWidth / 2;
cy = windowHeight / 2 ;
buffer = createGraphics(windowWidth, windowHeight);
buffer.background(175);
buffer.translate(cx, cy);
}
function draw() {
background(175);
imageMode(CORNER);
image(buffer, 0, 0, windowWidth, windowHeight);
let num1 = -g * (2 * m1 + m2) * sin(a1);
let num2 = -m2 * g * sin(a1 - 2 * a2);
let num3 = -2 * sin(a1 - a2) * m2;
let num4 = a2_v * a2_v * r2 + a1_v * a1_v * r1 * cos(a1 - a2);
let den = r1 * (2 * m1 + m2 - m2 * cos(2 * a1 - 2 * a2));
let 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));
let a2_a = (num1 * (num2 + num3 + num4)) / den;
translate(cx, cy);
stroke(0);
strokeWeight(2);
let x1 = r1 * sin(a1);
let y1 = r1 * cos(a1);
let x2 = x1 + r2 * sin(a2);
let 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;
// a1_v *= 0.99;
// a2_v *= 0.99;
buffer.stroke(0);
if (frameCount > 1) {
buffer.line(px2, py2, x2, y2);
}
px2 = x2;
py2 = y2;
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}