Created
January 5, 2019 23:58
-
-
Save nombrekeff/7216cff51a47b0302893df6da51273e6 to your computer and use it in GitHub Desktop.
Lévy C Curve Algorithm (In P5.js)
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
let x; | |
let y; | |
let len; | |
let alpha_angle; | |
let iteration_n; | |
let B; | |
let G; | |
function toRadians(degrees) { | |
return degrees * (PI / 180); | |
} | |
function c_curve(x, y, len, alpha_angle, iteration_n) { | |
let fx = x; | |
let fy = y; | |
let length = len; | |
let alpha = alpha_angle; | |
let it_n = iteration_n; | |
if (it_n > 0) { | |
length = (length / sqrt(2)); | |
c_curve(fx, fy, length, (alpha + 45), (it_n - 1)); | |
fx = (fx + (length * cos(toRadians(alpha + 45)))); | |
fy = (fy + (length * sin(toRadians(alpha + 45)))); | |
c_curve(fx, fy, length, (alpha - 45), (it_n - 1)); | |
} else { | |
let d = dist(fx, fy, width / 2, height / 2); | |
strokeWeight(map(d, 0, width, 0, 6)); | |
stroke(map(d, 0, width, 0, 255), G, B); | |
line(fx, fy, (fx + (length * cos(toRadians(alpha)))), (fy + (length * sin(toRadians(alpha))))); | |
} | |
} | |
function setup() { | |
createCanvas(600, 500); | |
background(51); | |
x = 170; // Stating x value | |
y = 150; // Stating y value | |
len = 250; // Stating length value | |
alpha_angle = 0; // Stating angle value | |
iteration_n = 12; // Stating iteration value | |
B = random(40, 100); | |
G = random(40, 100); | |
c_curve(x, y, len, alpha_angle, iteration_n); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment