Files
p5js/sketches/L-System.js
Jürg Hallenbarter 9a9080c155 added more algorithms
Co-authored-by: Copilot <copilot@github.com>
2026-04-27 17:08:24 +00:00

184 lines
3.4 KiB
JavaScript

let angle = 3.141592653589*1.5;
let angleDecrement = 1;
let stepSize = 10;
let widthIncrement = 1;
let currentWidth = 1;
let currentPosition = [501 / 2, 500];
let fractalst = "FF+[+F-F-F]-[-F+F+F]";
let stackList = [];
let lineLenghtScaleFactor = 1.5;
let swapPlusMinusState = false;
let turningAngleIncrement = 0.1;
//FF+[+F-F-F]-[-F+F+F] fork
function setup() {
console.log("setup");
createCanvas(windowWidth, windowHeight);
background(255, 255, 255)
noFill();
stroke(0, 0, 0);
strokeWeight(1);
currentPosition = [width / 2, height];
generateFractal();
}
function generateFractal() {
for (let i = 0; i < fractalst.length; i++) {
let c = fractalst.charAt(i);
switch (c) {
case "F":
forward();
break;
case "+":
plus();
break;
case "-":
minus();
break;
case "f":
forwardNoLine();
break;
case "|":
reverseDirection();
break;
case "#":
lineWidthIncrement();
break;
case "!":
lineWidthDecrement();
break;
case "[":
pushStack(i);
break;
case "]":
popStack(i);
break;
case ">":
lineWidthMultiply();
break;
case "<":
lineWidthDivide();
break;
case "&":
swapPlusMinus();
break;
case "@":
drawDot();
break;
case ")":
angleIncrement();
break;
case "(":
angleDecrement();
break;
default:
console.log("unrecognized character",c);
break;
}
}
}
function minus() {
if (swapPlusMinusState) {
angle += angleDecrement;
return
}
angle -= angleDecrement;
}
function plus() {
if (swapPlusMinusState) {
angle -= angleDecrement;
return
}
angle += angleDecrement;
}
function lineWidthIncrement(){
currentWidth + widthIncrement;
strokeWeight(currentWidth);
}
function lineWidthDecrement(){
currentWidth - widthIncrement;
strokeWeight(currentWidth);
}
function forward() {
let x = currentPosition[0] + cos(angle) * stepSize;
let y = currentPosition[1] + sin(angle) * stepSize;
line(currentPosition[0], currentPosition[1], x, y);
currentPosition = [x, y];
}
function forwardNoLine(){
let x = currentPosition[0] + cos(angle) * stepSize;
let y = currentPosition[1] + sin(angle) * stepSize;
currentPosition = [x, y];
}
function backward() {
let x = currentPosition[0] - cos(angle) * stepSize;
let y = currentPosition[1] - sin(angle) * stepSize;
line(currentPosition[0], currentPosition[1], x, y);
currentPosition = [x, y];
}
function reverseDirection(){
angle - 3.14159265358979323;
}
function pushStack(i){
console.log(angle)
stackList.push([i,currentPosition[0],currentPosition[1],angle,currentWidth]);
}
function popStack(i){
stackToPop = stackList.length - 1;
console.log(stackList[stackToPop])
currentPosition = [stackList[stackToPop][1],stackList[stackToPop][2]];
angle = stackList[stackToPop][3];
strokeWeight[stackList[stackToPop][4]]
}
function lineWidthMultiply(){
currentWidth * lineLenghtScaleFactor;
strokeWeight(currentWidth);
}
function lineWidthDivide(){
currentWidth / lineLenghtScaleFactor;
strokeWeight(currentWidth);
}
function swapPlusMinus(){
if (swapPlusMinusState) {
swapPlusMinusState = false;
return
}
swapPlusMinusState = true;
}
function drawDot() {
let x = currentPosition[0];
let y = currentPosition[1];
strokeWeight(currentWidth);
point(x, y);
}
function angleIncrement() {
angle += turningAngleIncrement;
}
function angleDecrement() {
angle -= turningAngleIncrement;
}
function draw() {
}
function mousePressed() {
}