-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
297 lines (237 loc) · 6.75 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<script>
var GRAPH1 = null;
var GRAPH2 = null;
function ComplexGraph(canvas, origin)
{
this.Color = "#0000ff";
this.Origin = origin;
this.Scale = 100.0; // Unit
this.Canvas = canvas;
this.Context = null;
this.R=0.25;
this.I=0.5;
this.Dragable = true;
this.Active = false;
this.Dragging = false;
}
ComplexGraph.prototype.RelativeMouseCoordinates = function (event) {
var totalOffsetX = 0;
var totalOffsetY = 0;
var canvasX = 0;
var canvasY = 0;
var currentElement = this.Canvas;
do{
totalOffsetX += currentElement.offsetLeft - currentElement.scrollLeft;
totalOffsetY += currentElement.offsetTop - currentElement.scrollTop;
}
while(currentElement = currentElement.offsetParent)
canvasX = event.pageX - totalOffsetX;
canvasY = event.pageY - totalOffsetY;
graphX = canvasX - this.Origin[0];
graphY = -(canvasY - this.Origin[1]);
return {x:graphX, y:graphY}
}
ComplexGraph.prototype.MouseMove = function(event) {
coords = this.RelativeMouseCoordinates(event);
if (this.Dragging) {
// Just move the tip to the mouse.
this.R = coords.x / this.Scale;
this.I = coords.y / this.Scale;
this.Draw(true);
} else {
// Haw far is the event away from the tip (in pixels).
var tx = this.R*100.0;
var ty = this.I*100.0;
tx = tx - coords.x;
ty = ty - coords.y;
// Distance squared
dist = tx * tx + ty * ty;
if (dist <= 25.0 && ! this.Active) {
this.Active = true;
this.Draw(true);
}
if (dist > 25.0 && this.Active) {
this.Active = false;
this.Draw(true);
}
}
}
ComplexGraph.prototype.MouseDown = function() {
if (this.Active) {
this.Dragging = true;
}
}
ComplexGraph.prototype.MouseUp = function() {
this.Dragging = false;
}
ComplexGraph.prototype.Draw = function (vectorFlag) {
var c=this.Canvas;
var r = this.R;
var i = this.I;
var canvOK=1;
try {c.getContext("2d");}
catch (er) {canvOK=0;}
if (canvOK==1) {
this.Radius = Math.sqrt(r*r + i*i);
var ctx=c.getContext("2d");
// Draw the unit circle.
ctx.beginPath();
ctx.arc(this.Origin[0],this.Origin[1],100,0,2*Math.PI);
ctx.strokeStyle="#A0A0A0";
ctx.stroke();
ctx.closePath();
// Draw the Axes vector
ctx.beginPath();
ctx.moveTo(this.Origin[0]-180,this.Origin[1]);
ctx.lineTo(this.Origin[0]+180,this.Origin[1]);
ctx.strokeStyle="#000000";
ctx.stroke();
ctx.moveTo(this.Origin[0],this.Origin[1]-180);
ctx.lineTo(this.Origin[0],this.Origin[1]+180);
ctx.strokeStyle="#000000";
ctx.stroke();
ctx.closePath();
if (vectorFlag) {
this.DrawVector([r,i], this.Color, [0.0,0.0]);
}
// Draw the complex coordinates label
ctx.font="30px Arial";
var coordStr = "";
ctx.fillStyle=this.Color;
coordStr = coordStr + r.toFixed(2);
coordStr = coordStr + " + ";
coordStr = coordStr + i.toFixed(2) + "i";
ctx.fillText(coordStr, this.Origin[0]-195, this.Origin[1]-170);
}
}
// For drawing the multiplcation composition.
// Base is for the multiplication demonstration.
// Transformation so [1,0] moves onto base.
ComplexGraph.prototype.DrawVector = function (vect, color, base) {
var ctx=this.Canvas.getContext("2d");
var r = vect[0] + base[0];
var i = vect[1] + base[1];
// Draw the complex vector
ctx.beginPath();
ctx.moveTo(this.Origin[0] + base[0]*this.Scale, this.Origin[1] - base[1]*this.Scale);
ctx.lineTo(this.Origin[0] + r*this.Scale, this.Origin[1] - i*this.Scale);
ctx.strokeStyle=color;
ctx.stroke();
ctx.closePath();
// Active end spot
if (this.Dragable) {
var spotRadius = 6;
ctx.beginPath();
ctx.arc(this.Origin[0] + r*this.Scale, this.Origin[1] - i*this.Scale, spotRadius, 0, 2*Math.PI);
ctx.strokeStyle="#FFFF00";
ctx.stroke();
ctx.closePath();
if (this.Active) {
ctx.fillStyle="#FFFF00";
} else {
ctx.fillStyle=this.Color;
}
ctx.fill();
} else {
// Draw an arrow tip.
ctx.beginPath();
var tx = this.Origin[0] + r*this.Scale;
var ty = this.Origin[1] - i*this.Scale;
// Tip is origin. end point of barb.
var x = -15;
var y = +5;
// sin and cos from normalized complex vector
var magnitude = Math.sqrt(vect[0]*vect[0] + vect[1]*vect[1]);
s = -vect[1]/magnitude; // Rotation is opposite because y is flipped.
c = vect[0]/magnitude;
// Rotate barb
var t1x = tx + x*c - y*s;
var t1y = ty + x*s + y*c;
var t2x = tx + x*c + y*s;
var t2y = ty + x*s - y*c;
// draw the barb
ctx.beginPath();
ctx.moveTo(tx,ty);
ctx.lineTo(t1x,t1y);
ctx.stroke();
ctx.moveTo(tx,ty);
ctx.lineTo(t2x,t2y);
ctx.stroke();
ctx.closePath();
}
}
function mouseMove(event)
{
if (GRAPH1) GRAPH1.MouseMove(event);
if (GRAPH2) GRAPH2.MouseMove(event);
if (GRAPH1.Dragging || GRAPH2.Dragging) {
GRAPH3.R = GRAPH1.R + GRAPH2.R;
GRAPH3.I = GRAPH1.I + GRAPH2.I;
draw();
}
}
function mouseDown()
{
if (GRAPH1) GRAPH1.MouseDown();
if (GRAPH2) GRAPH2.MouseDown();
}
function mouseUp()
{
if (GRAPH1) GRAPH1.MouseUp();
if (GRAPH2) GRAPH2.MouseUp();
}
function draw() {
var ctx=GRAPH1.Canvas.getContext("2d");
ctx.clearRect(0, 0, 1200, 470);
GRAPH1.Draw(true);
GRAPH2.Draw(true);
GRAPH3.Draw(true);
GRAPH3.DrawVector([GRAPH1.R,GRAPH1.I], GRAPH1.Color, [0.0,0.0]);
GRAPH3.DrawVector([GRAPH2.R,GRAPH2.I], GRAPH2.Color, [GRAPH1.R,GRAPH1.I]);
// Draw titles on the graphs
ctx.beginPath();
ctx.font="30px Arial";
ctx.fillStyle=GRAPH1.Color;
ctx.fillText("A", GRAPH1.Origin[0]-10, GRAPH1.Origin[1]-225);
ctx.fillStyle=GRAPH2.Color;
ctx.fillText("B", GRAPH2.Origin[0]-10, GRAPH2.Origin[1]-225);
ctx.fillStyle=GRAPH3.Color;
ctx.fillText("A+B", GRAPH3.Origin[0]-20, GRAPH3.Origin[1]-225);
ctx.closePath();
}
function main()
{
var canvas = document.getElementById("myCanvas")
var canvOK=1;
try {canvas.getContext("2d");}
catch (er) {
alert("Could not initialize canvas");
return;
}
var ctx=canvas.getContext("2d");
GRAPH1=new ComplexGraph(canvas, [200, 250]);
GRAPH1.Color = "#0000FF"
GRAPH2=new ComplexGraph(canvas, [600, 250]);
GRAPH2.Color = "#00A000"
GRAPH3=new ComplexGraph(canvas, [1000, 250]);
GRAPH3.Color = "#A00000"
GRAPH3.Dragable = false;
GRAPH3.R = GRAPH1.R + GRAPH2.R;
GRAPH3.I = GRAPH1.I + GRAPH2.I;
draw();
}
</script>
<body onload="main()">
<canvas id="myCanvas" width="1200" height="470" style="border:1px solid #d3d3d3;"
onmousemove="mouseMove(event)"
onmousedown="mouseDown()"
onmouseup="mouseUp()"
>
Your browser does not support the HTML5 canvas tag.</canvas>
</br>
Next: <a href="multiplication.htm">Multiplication of complex numbers</a>
</body>
</html>