p5.js:マンデルブロ集合
code:Mandelbrot.js
const N = 255;
const L = 255;
const SCALE = 3.8;
function setup() {
createCanvas(600, 600);
noLoop();
}
function draw() {
translate(width / 2, height / 2);
background(0);
for (let a = -width / 2; a <= width / 2; a++) {
for (let b = -height / 2; b <= height / 2; b++) {
let x = SCALE * a / width;
let y = SCALE * b / height;
let r = calc(x, y);
stroke(r * 12 % 256, r * 4 % 256, r * 16 % 256);
rect(a, b, 1, 1);
}
}
}
code:Mandelbrot.js
function calc(x, y) {
let tx, ty;
let zx = 0.0;
let zy = 0.0;
for (let i = 1; i <= N; i++) {
tx = zx;
ty = zy;
zx = tx * tx - ty * ty + x;
zy = 2 * tx * ty + y;
if (zx * zx + zy * zy > L)
return i;
}
return 0;
}
p5.js.icon
https://img.shields.io/badge/p5.js-マンデルブロ集合-ED225D.svg?logo=p5.js&style=for-the-badge