12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- // Mandelbrot demo
- //
- // Copyright (C) 2015 Legimet
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- // Based on Micro Python demo by Fabian Vogt
- var t = require('nsp/texture');
- var k = require('nsp/keys');
- function mandel(x, y, max_iters) {
- var zr = 0;
- var zi = 0;
- var tmp;
- for (var i = 0; i < max_iters; i++) {
- tmp = zr * zr - zi * zi + x;
- zi = 2 * zr * zi + y;
- zr = tmp;
- if (zr * zr + zi * zi >= 4) {
- return i;
- }
- }
- return max_iters;
- }
- var screen = new t.Texture(320, 240);
- screen.fill(0x0000);
- var width = 320;
- var height = 240;
- var iter = 16;
- var min_x = -2.0;
- var max_x = 1.0;
- var min_y = -1.0;
- var max_y = 1.0;
- var pixel_size_x = (max_x - min_x) / width;
- var pixel_size_y = (max_y - min_y) / height;
- var time_start = Date.now();
- var real, imag, color;
- for (var x = 0; x < height; x++) {
- real = min_x + x * pixel_size_x;
- for (var y = 0; y < width; y++) {
- imag = min_y + y * pixel_size_y;
- color = mandel(real, imag, iter);
- screen.setPx(y, x, color << 14 | color << 8 | color << 3);
- }
- screen.display();
- }
- print(width+"x"+height+" ("+iter+" iters) in "+(Date.now()-time_start)/1000+" seconds");
- k.waitKeyPressed();
|