-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig-work.js
112 lines (103 loc) · 2.69 KB
/
config-work.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
102
103
104
105
106
107
108
109
110
111
112
/*
Robot manipulation functions (mostly)
*/
// compute the next position of the robot arm. Update the global vars.
function updateWorkspace() {
elbowAngle = elbowAngle + deltaElbowDirection * deltaElbowAngle;
if (elbowAngle > Math.PI) {
elbowAngle = elbowAngle - 2 * Math.PI;
} else if (elbowAngle < -Math.PI) {
elbowAngle = elbowAngle + 2 * Math.PI;
}
shoulderAngle = shoulderAngle + deltaShoulderDirection * deltaShoulderAngle;
if (shoulderAngle > Math.PI) {
shoulderAngle = shoulderAngle - 2 * Math.PI;
} else if (shoulderAngle < -Math.PI) {
shoulderAngle = shoulderAngle + 2 * Math.PI;
}
}
// returns a line segment in wrold units that represents
// the arm starting at (wx, wy) with length and angle from
// the positive horizontal
function arm(x1, y1, theta, len) {
var dx = Math.cos(theta) * len;
var dy = Math.sin(theta) * len;
var x2 = x1 + dx;
var y2 = y1 + dy;
return { x1: x1, y1: y1, x2: x1 + dx, y2: y1 + dy };
}
// returns true iff the line from (a,b)->(c,d) intersects with (p,q)->(r,s)
function intersects(a, b, c, d, p, q, r, s) {
var det, gamma, lambda;
det = (c - a) * (s - q) - (r - p) * (d - b);
if (det === 0) {
return false;
} else {
lambda = ((s - q) * (r - a) + (p - r) * (s - b)) / det;
gamma = ((b - d) * (r - a) + (c - a) * (s - b)) / det;
return 0 < lambda && lambda < 1 && (0 < gamma && gamma < 1);
}
}
// returns true if any of the boxes (obstacles) in the workspace
// intersects with line segment
function hitAnyBlock(segment) {
for (var b = 0; b < blocks.length; b++) {
if (hitBlock(segment, b)) return true;
}
return false;
}
// returns true if the line segment intersects with any of the
// box's line segments.
function hitBlock(segment, b) {
var block = blocks[b];
var bottom = intersects(
segment.x1,
segment.y1,
segment.x2,
segment.y2,
block.x1,
block.y1,
block.x2,
block.y1
);
var top = intersects(
segment.x1,
segment.y1,
segment.x2,
segment.y2,
block.x1,
block.y2,
block.x2,
block.y2
);
var left = intersects(
segment.x1,
segment.y1,
segment.x2,
segment.y2,
block.x1,
block.y1,
block.x1,
block.y2
);
var right = intersects(
segment.x1,
segment.y1,
segment.x2,
segment.y2,
block.x2,
block.y1,
block.x2,
block.y2
);
return bottom | top | left | right;
}
// returns true if the line segment intersects with any of the
// the workspace's walls, floor or ceiling.
function hitWall(segment) {
var left = segment.x2 <= 0;
var right = segment.x2 >= worldWidth;
var top = segment.y2 >= worldHeight;
var bottom = segment.y2 <= 0;
return left || right || top || bottom;
}