Last active
May 28, 2024 09:54
-
-
Save sdkfz181tiger/ce474d6d8a3621be0de3aaea87dc07f8 to your computer and use it in GitHub Desktop.
フラクタル04_シェルピンスキーの曲線(L-System)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict" | |
// URL: https://openprocessing.org/sketch/2283736 | |
const WHITE = "#eeeeee"; | |
const BLACK = "#2f6690"; | |
function setup(){ | |
createCanvas(windowWidth, windowHeight); | |
angleMode(DEGREES); | |
noLoop(); stroke(WHITE); strokeWeight(2); | |
} | |
function draw(){ | |
background(BLACK); | |
drawFractal(); | |
} | |
function drawFractal(x, y){ | |
const depth = 5; | |
const cmds = createCmds("AA", depth); | |
const len = ((width<height)? width:height)/100; | |
drawCmds(width/2, 0, cmds, len); | |
} | |
function createCmds(str, depth){ | |
if(depth <= 0) return str; | |
let result = ""; | |
for(let s of str){ | |
if(s == "A") result += "BABA"; | |
if(s == "B") result += "BABB"; | |
} | |
return createCmds(result, depth-1); | |
} | |
function drawCmds(x, y, cmds, len){ | |
translate(x, y); | |
rotate(90); | |
for(let cmd of cmds){ | |
if(cmd == "A"){ | |
line(0, 0, len, 0); | |
translate(len, 0); | |
rotate(90); | |
line(0, 0, len, 0); | |
translate(len, 0); | |
rotate(90); | |
continue; | |
} | |
if(cmd == "B"){ | |
line(0, 0, len, 0); | |
translate(len, 0); | |
rotate(-45); | |
line(0, 0, len*2, 0); | |
translate(len*2, 0); | |
rotate(-45); | |
continue; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment