-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
101 lines (75 loc) · 2.23 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const armLength = 150;
const startPos = [250, 350];
let x = armLength;
let y = armLength;
let angle1 = 90;
let angle2 = 90;
let armStart = [0, 0];
let jointPos = [0, armLength];
let armEnd = [armLength, armLength];
let sliderX;
let sliderY;
function setup() {
createCanvas(800, 400);
frameRate(30);
sliderX = createSlider(0.1, 300, 150, 2);
sliderX.position(500, 550);
sliderY = createSlider(0.1, 200, 150, 2);
sliderY.position(700, 550);
}
function draw() {
background(80);
// angleMode(DEGREES);
translate(startPos[0], startPos[1]);
scale(1, -1);
x = sliderX.value();
y = sliderY.value();
sliderX.elt.max = Math.sqrt(sq(300) - sq(y));
sliderY.elt.max = Math.sqrt(sq(300) - sq(x));
// Ground
push();
strokeWeight(10);
line(-startPos[0], 0, 800, 0);
pop();
// Calculate Joint
let joint = calcJoint(x, y);
jointPos = [joint[0], joint[1]];
armEnd[0] = x;
armEnd[1] = y;
// Arms - Lines
push();
stroke(0, 150, 50);
strokeWeight(5);
line(armStart[0], armStart[1], jointPos[0], jointPos[1]);
line(jointPos[0], jointPos[1], armEnd[0], armEnd[1]);
pop();
// Joints - Circles
push();
stroke(0, 10, 150);
strokeWeight(5);
noFill();
circle(armStart[0], armStart[1], 15);
circle(jointPos[0], jointPos[1], 15);
circle(armEnd[0], armEnd[1], 15);
pop();
push();
translate(-startPos[0], startPos[1]);
scale(1, -1);
fill('green');
text(Math.round(mouseX - startPos[0]), 10, 15);
text(Math.round(startPos[1] - mouseY), 40, 15);
fill('white');
text(Math.round(x), 10, 35);
text(Math.round(y), 40, 35);
pop();
}
function calcJoint(a, b) {
let theta = Math.atan(b/a) + Math.acos((sq(a) + sq(b))/(300 * Math.sqrt(sq(a) + sq(b))));
// let theta = Math.atan(b/a) + Math.acos(150/sqrt(sq(x) + sq(y))); // DOESNT WORK
// console.log(degrees(theta), Math.round((150 * Math.cos(theta))), Math.round((150 * Math.sin(theta))));
// theta = Math.atan(b/a) + Math.acos(150/sqrt(sq(x) + sq(y)));
// console.log(degrees(theta), Math.round((150 * Math.cos(theta))), Math.round((150 * Math.sin(theta))));
// angle1 = theta;
// angle2 = Math.round(degrees(Math.acos(150/(sq(a) + sq(b)))));
return [Math.round((150 * Math.cos(theta))), Math.round((150 * Math.sin(theta)))];
}