-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandelbrot.htm
435 lines (351 loc) · 9.66 KB
/
mandelbrot.htm
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
432
<!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=1.0;
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();
} 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();
}
if (dist > 25.0 && this.Active) {
this.Active = false;
this.Draw();
}
}
}
ComplexGraph.prototype.MouseDown = function() {
if (this.Active) {
this.Dragging = true;
}
}
ComplexGraph.prototype.MouseUp = function() {
this.Dragging = false;
}
ComplexGraph.prototype.Draw = function () {
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);
this.Theta = Math.atan2(i,r) * 180 / Math.PI;
if (this.Theta < 0.0) {
this.Theta += 360.0;
}
var ctx=c.getContext("2d");
ctx.clearRect (this.Origin[0]-200 , this.Origin[1]-200, 400 , 410);
if (this.Dragable) {
var img=document.getElementById("mandelbrot");
ctx.drawImage(img,135,150);
}
// 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();
// Draw the complex vector
ctx.beginPath();
ctx.moveTo(this.Origin[0],this.Origin[1]);
ctx.lineTo(this.Origin[0] + r*this.Scale, this.Origin[1] - i*this.Scale);
ctx.strokeStyle=this.Color;
ctx.stroke();
ctx.closePath();
// Draw the angle theta arc.
if (this.Dragable) {
ctx.beginPath();
ctx.strokeStyle=this.Color;
ctx.arc(this.Origin[0],this.Origin[1],20,-this.Theta * Math.PI/ 180.0, 0.0);
ctx.stroke();
ctx.closePath();
}
if (this.Dragable) {
// Active end spot
ctx.beginPath();
ctx.arc(this.Origin[0] + r*this.Scale, this.Origin[1] - i*this.Scale, 5, 0, 2*Math.PI);
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 = +8;
// sin and cos from normalized complex vector
s = -i/this.Radius; // Rotation is opposite because y is flipped.
c = r/this.Radius;
// 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.strokeStyle=this.Color;
ctx.moveTo(tx,ty);
ctx.lineTo(t1x,t1y);
ctx.stroke();
ctx.moveTo(tx,ty);
ctx.lineTo(t2x,t2y);
ctx.stroke();
ctx.closePath();
}
// Draw the complex coordinates label
ctx.font="30px Arial";
var coordStr = "";
ctx.fillStyle=this.Color;
coordStr = coordStr + r.toFixed(1);
coordStr = coordStr + " + ";
coordStr = coordStr + i.toFixed(1) + "i";
ctx.fillText(coordStr, this.Origin[0]-195, this.Origin[1]-170);
// Draw the polar coordinates
var coordStr = "Radius: ";
coordStr = coordStr + this.Radius.toFixed(1);
coordStr = coordStr + ", Theta: ";
coordStr = coordStr + this.Theta.toFixed(1);
ctx.font="20px Arial";
ctx.fillStyle=this.Color;
ctx.fillText(coordStr, this.Origin[0]-195, this.Origin[1]+205);
}
}
// I should really make draw use this method.
// For drawing the multiplcation composition.
ComplexGraph.prototype.DrawVector = function (r,i, color) {
var c=this.Canvas;
var ctx=c.getContext("2d");
// Draw the complex vector
ctx.beginPath();
ctx.moveTo(this.Origin[0],this.Origin[1]);
ctx.lineTo(this.Origin[0] + r*this.Scale, this.Origin[1] - i*this.Scale);
ctx.strokeStyle=color;
ctx.stroke();
ctx.closePath();
if (this.Dragable) {
// Active end spot
ctx.beginPath();
ctx.arc(this.Origin[0] + r*this.Scale, this.Origin[1] - i*this.Scale, 5, 0, 2*Math.PI);
ctx.closePath();
if (this.Active) {
ctx.fillStyle="#FFFF00";
} else {
ctx.fillStyle=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 = +8;
// sin and cos from normalized complex vector
var radius = Math.sqrt(i*i + r*r);
s = -i/radius; // Rotation is opposite because y is flipped.
c = r/radius;
// 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.strokeStyle=color;
ctx.moveTo(tx,ty);
ctx.lineTo(t1x,t1y);
ctx.stroke();
ctx.moveTo(tx,ty);
ctx.lineTo(t2x,t2y);
ctx.stroke();
ctx.closePath();
}
}
// I should really make draw use this method.
// For drawing the multiplcation composition.
ComplexGraph.prototype.DrawArc = function (color, startTheta, endTheta) {
var c=this.Canvas;
var ctx=c.getContext("2d");
// Draw the angle theta arc.
ctx.beginPath();
ctx.strokeStyle=color;
ctx.arc(this.Origin[0],this.Origin[1],20, -endTheta*Math.PI/ 180.0, -startTheta*Math.PI/ 180.0);
ctx.stroke();
ctx.closePath();
}
var RUN = false;
var RADIUS = 0.0;
var COUNT = 0;
var X = 0.0;
var Y = 0.0;
function run() {
GRAPH2.R = X = 0.0;
GRAPH2.I = Y = 0.0;
GRAPH2.Draw();
RADIUS = 0;
COUNT = 0;
RUN = true;
iterate();
}
function iterate() {
if ( ! RUN) { return }
++COUNT;
X = (X * X) - (Y * Y);
Y = (X * Y) + (Y * X);
//GRAPH2.R = X;
//GRAPH2.I = Y;
//GRAPH2.Draw();
X += GRAPH1.R;
Y += GRAPH1.I;
GRAPH2.R = X;
GRAPH2.I = Y;
RADIUS = Math.sqrt(X*X + Y*Y);
if (RADIUS > 2.0) {
GRAPH2.Color = "#FF0000";
}
GRAPH2.Draw();
GRAPH2.Color = "#000000";
var ctx=GRAPH1.Canvas.getContext("2d");
ctx.font="30px Arial";
var str = "";
str += COUNT;
ctx.beginPath();
ctx.font="30px Arial";
ctx.fillStyle="#000000";
ctx.fillText(str, 850, 240);
ctx.closePath();
if (RADIUS > 2.0) {
RUN = false;
} else {
setTimeout(function(){iterate()},1000);
}
}
function stop() {
RUN = false;
}
function mouseMove(event)
{
if (GRAPH1.Dragging) { RUN = false; }
if (GRAPH1) GRAPH1.MouseMove(event);
}
function mouseDown()
{
if (GRAPH1) GRAPH1.MouseDown();
}
function mouseUp()
{
if (GRAPH1) GRAPH1.MouseUp();
}
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, [300, 250]);
GRAPH1.Color = "#0000FF"
GRAPH2=new ComplexGraph(canvas, [700, 250]);
GRAPH2.Color = "#00A000"
GRAPH2.R = 0.0;
GRAPH2.I = 0.0;
GRAPH2.Dragable = false;
GRAPH1.Draw();
GRAPH2.Draw();
// Draw titles on the graphs
ctx.beginPath();
ctx.font="30px Arial";
ctx.fillStyle=GRAPH1.Color;
ctx.fillText("C", GRAPH1.Origin[0]-10, GRAPH1.Origin[1]-225);
ctx.fillStyle=GRAPH2.Color;
ctx.fillText("Z = Z*Z + C", GRAPH2.Origin[0]-90, GRAPH2.Origin[1]-225);
ctx.closePath();
}
</script>
<body onload="main()">
<img id="mandelbrot" src="mandelbrot3.jpg" width="254" height="200" hidden="true">
<canvas id="myCanvas" width="900" 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>
<button type="button" onclick="run()">Go!</button>
<button type="button" onclick="stop()">Stop</button>
</br>
<a href="http://www.youtube.com/watch?v=RYVmd7H7gzw">Fast Zoom Video</a>
</br>
<a href="http://www.youtube.com/watch?v=9G6uO7ZHtK8">Slow Zoom Video</a>
</br>
<a href="http://www.youtube.com/watch?v=VSDI84Mm-hI">Scale of the Universe</a>
</body>
</html>