This repository was archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 865
/
Copy pathunit-circle.js
431 lines (363 loc) · 18.5 KB
/
unit-circle.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/* TODO(csilvers): fix these lint errors (http://eslint.org/docs/rules): */
/* eslint-disable comma-dangle, indent, max-len, no-trailing-spaces, no-undef, no-var */
/* To fix, remove an entry above, run ka-lint, and fix errors. */
define(function(require) {
require("../third_party/jquery.mobile.vmouse.js");
$.extend(KhanUtil, {
initUnitCircle: function(degrees) {
var graph = KhanUtil.currentGraph;
// Create a properly scaled 600x600px graph
var options = {
xpixels: 514,
ypixels: 514,
range: [[-1.2, 1.2], [-1.2, 1.2]]
};
options.scale = [options.xpixels / (options.range[0][1] - options.range[0][0]),
options.ypixels / (options.range[1][1] - options.range[1][0])];
graph.init(options);
// Attach the metrics to the graph for later reference
graph.xpixels = options.xpixels;
graph.ypixels = options.ypixels;
graph.range = options.range;
graph.scale = options.scale;
graph.angle = 0;
graph.revolutions = 0;
graph.quadrant = 1;
graph.dragging = false;
graph.highlight = false;
graph.degrees = degrees;
// Axes and circle
graph.style({
stroke: "#ddd",
strokeWidth: 1,
arrows: "->"
}, function() {
graph.circle([0, 0], 1);
graph.line([-1.2, 0], [1.2, 0]);
graph.line([0, -1.2], [0, 1.2]);
graph.line([1.2, 0], [-1.2, 0]);
graph.line([0, 1.2], [0, -1.2]);
});
// Tick marks at -1, 1
graph.style({
strokeWidth: 2
}, function() {
graph.line([-1, -5 / graph.scale[0]], [-1, 5 / graph.scale[0]]);
graph.line([1, -5 / graph.scale[0]], [1, 5 / graph.scale[0]]);
graph.line([-5 / graph.scale[0], -1], [5 / graph.scale[0], -1]);
graph.line([-5 / graph.scale[0], 1], [5 / graph.scale[0], 1]);
});
// Declare all the graphic elements that get manipulated each time the angle changes
graph.triangle = KhanUtil.bogusShape;
graph.rightangle = KhanUtil.bogusShape;
graph.spiral = KhanUtil.bogusShape;
graph.arrow = KhanUtil.bogusShape;
graph.cosLabel = KhanUtil.bogusShape;
graph.sinLabel = KhanUtil.bogusShape;
graph.radiusLabel = KhanUtil.bogusShape;
graph.angleLabel = KhanUtil.bogusShape;
graph.angleLines = KhanUtil.bogusShape;
KhanUtil.initMouseHandlers();
KhanUtil.setAngle(graph.angle);
},
// Not all shapes are needed to depict every angle. If a shape isn't
// needed, it's replaced with bogusShape which just has stub methods
// that successfully do nothing.
// The alternative would be 'if..typeof' checks all over the place.
bogusShape: {
animate: function() {},
attr: function() {},
remove: function() {}
},
initMouseHandlers: function() {
var graph = KhanUtil.currentGraph;
// Another SVG element on top of everything else where we can add
// invisible shapes with mouse handlers wherever we want.
graph.mouselayer = Raphael("unitcircle", graph.xpixels, graph.ypixels);
$(graph.mouselayer.canvas).css("z-index", 1);
Khan.scratchpad.disable();
// Visible orange point that gets dragged
graph.style({
stroke: KhanUtil.ORANGE,
fill: KhanUtil.ORANGE
}, function() {
graph.dragPoint = graph.circle([1, 0], 4 / graph.scale[0]);
});
// The invisible circle that gets mouse events.
graph.mouseTarget = graph.mouselayer.circle(
(1 - graph.range[0][0]) * graph.scale[0],
(graph.range[1][1] - 0) * graph.scale[1], 15);
graph.mouseTarget.attr({fill: "#000", "opacity": 0.0});
$(graph.mouseTarget[0]).css("cursor", "move");
$(graph.mouseTarget[0]).bind("vmousedown vmouseover vmouseout", function(event) {
var graph = KhanUtil.currentGraph;
if (event.type === "vmouseover") {
graph.highlight = true;
if (!graph.dragging) {
KhanUtil.highlightAngle();
}
} else if (event.type === "vmouseout") {
graph.highlight = false;
if (!graph.dragging) {
KhanUtil.unhighlightAngle();
}
} else if (event.type === "vmousedown" && (event.which === 1 || event.which === 0)) {
event.preventDefault();
$(document).bind("vmousemove vmouseup", function(event) {
event.preventDefault();
graph.dragging = true;
// mouseY is in pixels relative to the SVG; coordY is the scaled y-coordinate value
var mouseY = event.pageY - $("#unitcircle").offset().top;
var mouseX = event.pageX - $("#unitcircle").offset().left;
var coordX = (mouseX / graph.scale[0]) + graph.range[0][0];
var coordY = graph.range[1][1] - mouseY / graph.scale[1];
if (event.type === "vmousemove") {
// Find the angle from the origin to the mouse pointer
var angle;
if (coordX) {
angle = Math.atan(coordY / coordX);
} else {
// Fill in where atan is undefined
if (coordY > 0) {
angle = -Math.PI / 2;
} else {
angle = -Math.PI / 2;
}
}
// Round the angle to the nearest 5 degree increment
angle = Math.round(angle / (Math.PI / 36)) * (Math.PI / 36);
// Figure out what quadrant the mouse is in. Since atan
// is only defined in Q1 and Q4 (and is negative in Q4),
// adjust the angle appropriately to represent the correct
// positive angle in the unit circle.
//
// If moving between Q1 and Q4, keep track of the number of revolutions.
if (coordX > 0 && coordY >= 0) {
if (graph.quadrant === 4) {
++graph.revolutions;
}
graph.quadrant = 1;
} else if (coordX <= 0 && coordY > 0) {
angle += Math.PI;
graph.quadrant = 2;
} else if (coordX < 0 && coordY <= 0) {
angle += Math.PI;
graph.quadrant = 3;
} else if (coordX >= 0 && coordY < 0) {
angle += 2 * Math.PI;
if (graph.quadrant === 1) {
--graph.revolutions;
}
graph.quadrant = 4;
}
// Limit the number of revolutions to 2 in either direction.
if (graph.revolutions <= -3) {
graph.revolutions = -3;
angle = 2 * Math.PI;
} else if (graph.revolutions >= 2) {
graph.revolutions = 2;
angle = 0;
}
// Now ((2pi * revolutions) + angle) represents the full angle
// Redraw the angle only if it's changed
if (graph.angle !== angle + (graph.revolutions * 2 * Math.PI)) {
KhanUtil.setAngle(angle + (graph.revolutions * 2 * Math.PI));
}
} else if (event.type === "vmouseup") {
$(document).unbind("vmousemove vmouseup");
graph.dragging = false;
if (!graph.highlight) {
KhanUtil.unhighlightAngle();
}
}
});
}
});
},
highlightAngle: function() {
var graph = KhanUtil.currentGraph;
graph.dragPoint.animate({ scale: 2 }, 50);
graph.angleLines.animate({ stroke: KhanUtil.ORANGE }, 100);
graph.spiral.animate({ stroke: KhanUtil.ORANGE }, 100);
graph.arrow.animate({ fill: KhanUtil.ORANGE }, 100);
$(graph.angleLabel).animate({ color: KhanUtil.ORANGE }, 100);
//$(graph.angleLabel).css({ color: KhanUtil.ORANGE });
},
unhighlightAngle: function() {
var graph = KhanUtil.currentGraph;
graph.dragPoint.animate({ scale: 1 }, 50);
graph.angleLines.animate({ stroke: KhanUtil.BLUE }, 100);
graph.spiral.animate({ stroke: KhanUtil.BLUE }, 100);
graph.arrow.animate({ fill: KhanUtil.BLUE }, 100);
$(graph.angleLabel).animate({ color: KhanUtil.BLUE }, 100);
//$(graph.angleLabel).css({ color: KhanUtil.BLUE });
},
// Redraw the angle
setAngle: function(angle) {
var graph = KhanUtil.currentGraph;
graph.angle = angle;
graph.quadrant = (Math.floor((angle + 10 * Math.PI) / (Math.PI / 2)) % 4) + 1;
graph.revolutions = Math.floor(angle / (2 * Math.PI));
// Remove everything dynamic. It should be safe to call remove()
// on everything since unused stuff should be instances of bogusShape
graph.triangle.remove();
graph.rightangle.remove();
graph.spiral.remove();
graph.arrow.remove();
graph.cosLabel.remove();
graph.sinLabel.remove();
graph.radiusLabel.remove();
graph.angleLabel.remove();
graph.angleLines.remove();
var highlightColor = KhanUtil.BLUE;
if (graph.dragging || graph.highlight) {
highlightColor = KhanUtil.ORANGE;
}
// Draw the bold angle lines
graph.style({ stroke: highlightColor, strokeWidth: 3 });
graph.angleLines = graph.path([[1, 0], [0, 0], [Math.cos(angle), Math.sin(angle)]]);
graph.style({ stroke: KhanUtil.BLUE, strokeWidth: 1 });
graph.triangle = graph.path([[0, 0], [Math.cos(angle), 0], [Math.cos(angle), Math.sin(angle)], [0, 0]]);
var cosText = KhanUtil.roundTo(3, Math.cos(angle));
var sinText = KhanUtil.roundTo(3, Math.sin(angle));
// Include radicals for common 45-45-90 and 30-60-90 values
var prettyAngles = {
"0.866": "\\frac{\\sqrt{3}}{2}\\;(0.866)",
"-0.866": "-\\frac{\\sqrt{3}}{2}\\;(-0.866)",
"0.707": "\\frac{\\sqrt{2}}{2}\\;(0.707)",
"-0.707": "-\\frac{\\sqrt{2}}{2}\\;(-0.707)",
"0.5": "\\frac{1}{2}\\;(0.5)",
"-0.5": "-\\frac{1}{2}\\;(-0.5)"
};
cosText = prettyAngles[cosText] ? prettyAngles[cosText] : cosText;
sinText = prettyAngles[sinText] ? prettyAngles[sinText] : sinText;
// Position the distance labels and right-angle marker based on quadrant
if (angle % Math.PI === 0) {
graph.cosLabel = graph.label([Math.cos(angle) / 2, 0], cosText, "below");
} else if (angle % (Math.PI / 2) === 0) {
graph.sinLabel = graph.label([Math.cos(angle), Math.sin(angle) / 2], sinText, "right");
} else if (graph.quadrant === 1) {
graph.cosLabel = graph.label([Math.cos(angle) / 2, 0], cosText, "below");
graph.sinLabel = graph.label([Math.cos(angle), Math.sin(angle) / 2], sinText, "right");
graph.radiusLabel = graph.label([Math.cos(angle) / 2, Math.sin(angle) / 2], 1, "above left");
graph.rightangle = graph.path([[Math.cos(angle) - 0.04, 0], [Math.cos(angle) - 0.04, 0.04], [Math.cos(angle), 0.04]]);
} else if (graph.quadrant === 2) {
graph.cosLabel = graph.label([Math.cos(angle) / 2, 0], cosText, "below");
graph.sinLabel = graph.label([Math.cos(angle), Math.sin(angle) / 2], sinText, "left");
graph.radiusLabel = graph.label([Math.cos(angle) / 2, Math.sin(angle) / 2], 1, "above right");
graph.rightangle = graph.path([[Math.cos(angle) + 0.04, 0], [Math.cos(angle) + 0.04, 0.04], [Math.cos(angle), 0.04]]);
} else if (graph.quadrant === 3) {
graph.cosLabel = graph.label([Math.cos(angle) / 2, 0], cosText, "above");
graph.sinLabel = graph.label([Math.cos(angle), Math.sin(angle) / 2], sinText, "left");
graph.radiusLabel = graph.label([Math.cos(angle) / 2, Math.sin(angle) / 2], 1, "below right");
graph.rightangle = graph.path([[Math.cos(angle) + 0.04, 0], [Math.cos(angle) + 0.04, -0.04], [Math.cos(angle), -0.04]]);
} else if (graph.quadrant === 4) {
graph.cosLabel = graph.label([Math.cos(angle) / 2, 0], cosText, "above");
graph.sinLabel = graph.label([Math.cos(angle), Math.sin(angle) / 2], sinText, "right");
graph.radiusLabel = graph.label([Math.cos(angle) / 2, Math.sin(angle) / 2], 1, "below left");
graph.rightangle = graph.path([[Math.cos(angle) - 0.04, 0], [Math.cos(angle) - 0.04, -0.04], [Math.cos(angle), -0.04]]);
}
// Draw the spiral angle indicator
var points = [];
for (var i = 0; i <= 50; ++i) {
points.push([Math.cos(i * angle / 50) * (0.1 + ((i * Math.abs(angle) / 50 / Math.PI) * 0.02)),
Math.sin(i * angle / 50) * (0.1 + ((i * Math.abs(angle) / 50 / Math.PI) * 0.02))]);
}
graph.style({ strokeWidth: 2, stroke: highlightColor });
graph.spiral = graph.path(points);
// Draw an arrow at the end of the spiral angle indicator
var spiralEndX = points[50][0];
var spiralEndY = points[50][1];
graph.style({ stroke: false, fill: highlightColor }, function() {
if (angle > Math.PI / 12) {
// positive angles big enough to need an arrow
graph.arrow = graph.path([[spiralEndX, spiralEndY - 0.005],
[spiralEndX - 0.02, spiralEndY - 0.03],
[spiralEndX + 0.02, spiralEndY - 0.03],
[spiralEndX, spiralEndY - 0.005]]);
graph.arrow.rotate((angle - Math.PI / 20) * (-180 / Math.PI), (spiralEndX - graph.range[0][0]) * graph.scale[0], (graph.range[1][1] - spiralEndY) * graph.scale[1]);
} else if (angle < -Math.PI / 12) {
// negative angles "big" enough to need an arrow
graph.arrow = graph.path([[spiralEndX, spiralEndY + 0.005],
[spiralEndX - 0.02, spiralEndY + 0.03],
[spiralEndX + 0.02, spiralEndY + 0.03],
[spiralEndX, spiralEndY + 0.005]]);
graph.arrow.rotate((angle + Math.PI / 20) * (-180 / Math.PI), (spiralEndX - graph.range[0][0]) * graph.scale[0], (graph.range[1][1] - spiralEndY) * graph.scale[1]);
} else {
// no room for an arrow
graph.arrow = KhanUtil.bogusShape;
}
});
// Figure out how to display the angle
var angleText = angle;
if (graph.degrees) {
angleText *= (180 / Math.PI);
angleText = Math.round(angleText);
angleText += "^{\\circ}";
} else if (-15 < angle && angle < 15 && angle !== 0) {
angleText = KhanUtil.piFraction(angle);
}
// Put the angle value somewhere obvious, but not in the way of anything else. This
// could probably be improved, but it at least prevents text on top of other text.
if (angle < -3.5 * Math.PI) {
graph.angleLabel = graph.label([-0.2, 0.2], angleText, "center");
} else if (angle < -0.15 * Math.PI) {
graph.angleLabel = graph.label([Math.cos(angle / 2) / 5, Math.sin(angle / 2) / 5], angleText, "center");
} else if (angle < 0.15 * Math.PI) {
graph.angleLabel = graph.label([0, 0], angleText, "left");
} else if (angle < 3.5 * Math.PI) {
graph.angleLabel = graph.label([Math.cos(angle / 2) / 5, Math.sin(angle / 2) / 5], angleText, "center");
} else {
graph.angleLabel = graph.label([-0.2, -0.2], angleText, "center");
}
$(graph.angleLabel).css("color", highlightColor);
// Reposition the mouse target and indicator
graph.mouseTarget.attr("cx", (Math.cos(angle) - graph.range[0][0]) * graph.scale[0]);
graph.mouseTarget.attr("cy", (graph.range[1][1] - Math.sin(angle)) * graph.scale[1]);
graph.dragPoint.attr("cx", (Math.cos(angle) - graph.range[0][0]) * graph.scale[0]);
graph.dragPoint.attr("cy", (graph.range[1][1] - Math.sin(angle)) * graph.scale[1]);
graph.angleLines.toFront();
graph.dragPoint.toFront();
},
goToAngle: function(angle) {
var graph = KhanUtil.currentGraph;
if (graph.degrees) {
angle *= (Math.PI / 180);
}
var duration = 1000 * Math.abs(angle - graph.angle) / Math.PI;
$(graph).animate({
angle: angle
}, {
duration: duration,
easing: "linear",
step: function(now, fx) {
KhanUtil.setAngle(now);
}
});
},
showCoordinates: function(angle, highlightCoord) {
var graph = KhanUtil.currentGraph;
if (graph.degrees) {
angle *= (Math.PI / 180);
}
graph.style({stroke: 0, fill: KhanUtil.BLUE}, function() {
graph.circle([Math.cos(angle), Math.sin(angle)], 4 / graph.scale[0]);
});
graph.dragPoint.toFront();
var xCoord = KhanUtil.roundTo(3, Math.cos(angle));
var yCoord = KhanUtil.roundTo(3, Math.sin(angle));
if (highlightCoord === 'x') {
xCoord = "\\pink{" + xCoord + "}";
}
if (highlightCoord === 'y') {
yCoord = "\\pink{" + yCoord + "}";
}
var coordText = "(" + xCoord + ", " + yCoord + ")";
if (Math.floor(angle / Math.PI) % 2) {
graph.coordLabel = graph.label([Math.cos(angle), Math.sin(angle)], coordText, "below");
} else {
graph.coordLabel = graph.label([Math.cos(angle), Math.sin(angle)], coordText, "above");
}
}
});
});