Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
- <title>Useful Complex Operations</title>
- <style>
- body {
- text-align: center;
- }
- </style>
- </head>
- <body onload="mandelbrot()">
- <canvas id=canvas width=1000 height=700>
- Maybe it's time to <a href="http://en.wikipedia.org/wiki/Canvas_element#Support">change</a> browser?
- </canvas>
- <script>
- function Complex(x, y) {
- this.x = x || 0;
- this.y = y || 0;
- } // Complex
- Complex.prototype.toString = function() {
- return this.y >= 0 ? this.x + " + " + this.y + "i" : this.x + " - " + (-this.y) + "i";
- } // toString
- Complex.prototype.modulus = function() {
- return Math.sqrt(this.x*this.x + this.y*this.y);
- } // modulus
- Complex.prototype.add = function(z) {
- return new Complex(this.x + z.x, this.y + z.y);
- } // sum
- Complex.prototype.square = function() {
- return new Complex(this.x*this.x - this.y*this.y, 2*this.x*this.y);
- } // square
- Complex.prototype.sqmod = function(){
- return this.x*this.x + this.y*this.y;
- } // square pow of modulus
- function limitCheck(c, maxIter){
- var z = new Complex(0, 0);
- var i = 1;
- for(; i <= maxIter; i++){
- z = c.add(z.square());
- if (z.modulus() > 2)
- return false; //i here is how fast z grow to fast... it's need to colorize
- }
- return true;
- }
- var scale = 0.005;
- var step = 0.0007;
- var Plot = function(canvas){
- this.ctx = canvas.getContext('2d');
- this.ctx.translate(canvas.width/2, canvas.height/2);
- this.ctx.scale(1/scale, -1/scale);
- this.ctx.fillStyle = "red";
- }
- Plot.prototype.drawRect = function(x, y, w, h, c){
- this.ctx.fillStyle = "rgb(" + c + "," + c + "," + c + ")";
- this.ctx.fillRect(x, y, w, h);
- }
- function mandelbrot(){
- var MAX_X = 2, MAX_Y = 2;
- var plot = new Plot(document.getElementById("canvas"));
- for(var i = -MAX_X; i < MAX_X; i += step){
- for(var j = -MAX_Y; j < MAX_Y; j += step){
- var c = new Complex(i, j);
- var lc = limitCheck(c, 32);
- if(lc == true)
- plot.drawRect(i, j, step, step, 0);
- }
- }
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement