Skip to content

Instantly share code, notes, and snippets.

@sdkfz181tiger
Last active May 28, 2024 09:53
Show Gist options
  • Save sdkfz181tiger/60258fccb599e9912cbf055ae9c34f7c to your computer and use it in GitHub Desktop.
Save sdkfz181tiger/60258fccb599e9912cbf055ae9c34f7c to your computer and use it in GitHub Desktop.
フラクタル01_Lévy C curve
"use strict"
// URL: https://openprocessing.org/sketch/2278937
const WHITE = "#eeeeee";
const BLACK = "#2f6690";
function setup(){
createCanvas(windowWidth, windowHeight);
angleMode(DEGREES); noLoop();
stroke(WHITE); strokeWeight(2);
}
function draw(){
background(BLACK);
const aX = width * 0.3;
const aY = height * 0.25;
const bX = width * 0.7;
const bY = height * 0.25;
drawFractal(aX, aY, bX, bY, 14);
}
function drawFractal(aX, aY, bX, bY, depth){
if(depth <= 0){
line(aX, aY, bX, bY);
return;
}
const mX = (aX + bX) / 2;
const mY = (aY + bY) / 2;
const len = sqrt((bX-aX)**2+(bY-aY)**2) / 2;
const deg = atan2(bY-aY, bX-aX) + 90;
const cX = mX + len * cos(deg);
const cY = mY + len * sin(deg);
drawFractal(aX, aY, cX, cY, depth-1);
drawFractal(cX, cY, bX, bY, depth-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment