-
Notifications
You must be signed in to change notification settings - Fork 4
/
belt-problem.js
205 lines (183 loc) · 8.44 KB
/
belt-problem.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// From https://redblobgames.github.io/circular-obstacle-pathfinding/
// Copyright 2017 Red Blob Games <[email protected]>
// License: Apache v2.0 <http://www.apache.org/licenses/LICENSE-2.0.html>
/** Return point a given distance and direction from a start point */
function direction_step(start, distance, angle) {
return vec_add(start, vec_polar(distance, angle));
}
/** Intersection between segment AB and circle C */
function segment_circle_intersection(A, B, C) {
const CA = vec_sub(C, A), BA = vec_sub(B, A);
let u = (CA.x * BA.x + CA.y * BA.y) / (BA.x * BA.x + BA.y * BA.y);
if (u < 0.0) { u = 0.0; }
if (u > 1.0) { u = 1.0; }
const E = vec_interpolate(A, B, u);
const d = vec_distance(C, E);
return { u: u, d: d, E: E, intersects: d <= C.r };
}
/** Calculations needed for internal bitangents */
class InternalBitangents {
constructor (A, B) {
this.A = A;
this.B = B;
}
get theta() {
const P = vec_distance(this.A, this.B);
const cos_angle = (this.A.r + this.B.r) / P;
return Math.acos(cos_angle);
}
get AB_angle() { return vec_facing(this.A, this.B); }
get BA_angle() { return vec_facing(this.B, this.A); }
get C() { return direction_step(this.A, this.A.r, this.AB_angle - this.theta); }
get D() { return direction_step(this.A, this.A.r, this.AB_angle + this.theta); }
get E() { return direction_step(this.B, this.B.r, this.BA_angle + this.theta); }
get F() { return direction_step(this.B, this.B.r, this.BA_angle - this.theta); }
}
/** Calculations needed for external bitangents */
class ExternalBitangents {
constructor (A, B) {
this.A = A;
this.B = B;
}
get theta() {
const P = vec_distance(this.A, this.B);
const cos_angle = (this.A.r - this.B.r) / P;
return Math.acos(cos_angle);
}
get AB_angle() { return vec_facing(this.A, this.B); }
get C() { return direction_step(this.A, this.A.r, this.AB_angle - this.theta); }
get D() { return direction_step(this.A, this.A.r, this.AB_angle + this.theta); }
get E() { return direction_step(this.B, this.B.r, this.AB_angle + this.theta); }
get F() { return direction_step(this.B, this.B.r, this.AB_angle - this.theta); }
}
/* Diagrams */
let belt_problem = new Vue({
el: "#diagram-belt-problem",
data: {
A: {x: 150, y: 150, r: 130},
B: {x: 450, y: 150, r: 50}
},
computed: {
non_overlapping: function() { return !isNaN(this.bitangent.theta); },
bitangent: function() { return new InternalBitangents(this.A, this.B); },
C: function() { return this.bitangent.C; },
D: function() { return this.bitangent.D; },
E: function() { return this.bitangent.E; },
F: function() { return this.bitangent.F; },
theta_AC: function() { return direction_step(this.A, 20, this.bitangent.AB_angle - this.bitangent.theta/2); },
theta_AD: function() { return direction_step(this.A, 20, this.bitangent.AB_angle + this.bitangent.theta/2); },
theta_BE: function() { return direction_step(this.B, 20, this.bitangent.BA_angle + this.bitangent.theta/2); },
theta_BF: function() { return direction_step(this.B, 20, this.bitangent.BA_angle - this.bitangent.theta/2); }
}
});
let pulley_problem = new Vue({
el: "#diagram-pulley-problem",
data: {
A: {x: 150, y: 150, r: 130},
B: {x: 450, y: 150, r: 50}
},
computed: {
containing: function() { return vec_distance(this.A, this.B) < this.A.r - this.B.r; },
bitangent: function() { return new ExternalBitangents(this.A, this.B); },
C: function() { return this.bitangent.C; },
D: function() { return this.bitangent.D; },
E: function() { return this.bitangent.E; },
F: function() { return this.bitangent.F; },
theta_AC: function() { return direction_step(this.A, 20, this.bitangent.AB_angle - this.bitangent.theta/2); },
theta_AD: function() { return direction_step(this.A, 20, this.bitangent.AB_angle + this.bitangent.theta/2); },
theta_BE: function() { return direction_step(this.B, 20, this.bitangent.AB_angle + this.bitangent.theta/2); },
theta_BF: function() { return direction_step(this.B, 20, this.bitangent.AB_angle - this.bitangent.theta/2); }
}
});
let hugging_edge = new Vue({
el: "#diagram-hugging-edge",
data: {
A: {x: 150, y: 200},
C: {x: 300, y: 100, r: 40},
E: {x: 450, y: 200},
},
computed: {
valid: function() { return !isNaN(this.angle_to_A) && !isNaN(this.angle_to_E); },
mid_BD: function() { return vec_interpolate(this.B, this.D, 0.5); },
angle_to_A: function() {
return vec_facing(this.C, this.A) + Math.acos(this.C.r / vec_distance(this.C, this.A));
},
angle_to_E: function() {
return vec_facing(this.C, this.E) - Math.acos(this.C.r / vec_distance(this.C, this.E));
},
B: function() { return direction_step(this.C, this.C.r, this.angle_to_A); },
D: function() { return direction_step(this.C, this.C.r, this.angle_to_E); },
arc_path: function() {
let angle = this.angle_to_E - this.angle_to_A;
while (angle < 0) { angle += 2 * Math.PI; }
let large_arc = angle >= Math.PI ? 1 : 0;
return `M ${this.B.x},${this.B.y} A ${this.C.r},${this.C.r} 0 ${large_arc} 1 ${this.D.x},${this.D.y}`;
},
},
});
let surfing_line_of_sight = new Vue({
el: "#diagram-surfing-line-of-sight",
data: {
A: {x: 150, y: 150},
B: {x: 450, y: 150},
C: {x: 300, y: 125, r: 50}
},
computed: {
calculation: function() { return segment_circle_intersection(this.A, this.B, this.C); },
intersects: function() { return this.calculation.intersects; },
d: function() { return this.calculation.d; },
r: function() { return this.C.r; },
E: function() { return vec_add(this.calculation.E, {x: 0, y: 1e-6}); /* need epsilon for label placement when C.y == 0 */ },
F: function() { // the end of the line showing 'r'
let angle = Math.PI * 0.75 + vec_facing(this.C, this.E);
return direction_step(this.C, this.C.r, angle);
},
dashed_line_offset: function() { return this.C.y < this.A.y? 70 : -70; },
signed_r: function() { return this.C.y < this.A.y? this.r : -this.r; }
}
});
["#diagram-circle-overlap", "#diagram-hugging-overlap"].forEach(el => new Vue({
el: el,
data: {
A: {x: 220, y: 120, r: 100},
B: {x: 350, y: 180, r: 80}
},
computed: {
valid: function() { return !isNaN(this.theta); },
containing: function() { return this.AB_distance < this.A.r - this.B.r; },
overlapping: function() { return this.AB_distance < this.A.r + this.B.r; },
AB_distance: function() { return vec_distance(this.A, this.B); },
AB_angle: function() { return vec_facing(this.A, this.B); },
C: function() { return vec_interpolate(this.A, this.B, this.a / this.AB_distance); },
D: function() { return direction_step(this.A, this.A.r, this.AB_angle + this.theta); },
E: function() { return direction_step(this.A, this.A.r, this.AB_angle - this.theta); },
AD_angle: function() { return vec_facing(this.A, this.D); },
AE_angle: function() { return vec_facing(this.A, this.E); },
theta_AC: function() { return direction_step(this.A, 20, this.AB_angle - this.theta/2); },
theta_AD: function() { return direction_step(this.A, 20, this.AB_angle + this.theta/2); },
a: function() {
const d = this.AB_distance;
const Ar = this.A.r, Br = this.B.r;
return (Ar*Ar - Br*Br + d*d) / (2*d);
},
theta: function() {
return Math.acos(this.a / this.A.r);
},
opposite_C: function() {
return direction_step(this.C, 1, vec_facing(this.A, this.B) + Math.PI/4);
}
}
}));
let bitangents_overlap = new Vue({
el: "#diagram-bitangents-overlap",
data: {
A: {x: 150, y: 150, r: 130},
B: {x: 450, y: 150, r: 50},
},
computed: {
overlapping: function() { return vec_distance(this.A, this.B) < this.A.r + this.B.r; },
containing: function() { return vec_distance(this.A, this.B) < this.A.r - this.B.r; },
internal: function() { return new InternalBitangents(this.A, this.B); },
external: function() { return new ExternalBitangents(this.A, this.B); }
}
});