Skip to content

Instantly share code, notes, and snippets.

@takawo
Created November 13, 2021 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takawo/378c574ba63ff53b0d7b23045e9b3876 to your computer and use it in GitHub Desktop.
Save takawo/378c574ba63ff53b0d7b23045e9b3876 to your computer and use it in GitHub Desktop.
function setup() {
createCanvas(800, 400);
angleMode(DEGREES);
}
function draw() {
background(220);
let d = width / 4;
textAlign(CENTER,CENTER);
push();
translate((width * 1) / 4, height / 2);
rectMode(CENTER);
rect(0, 0, d, d, d / 4);
text("rect(0, 0, d, d, d / 4)",0,height/3);
pop();
push();
translate((width * 3) / 4, height / 2);
rectMode(CENTER);
drawSuperEllipse(0, 0, d, d, 5);
text("drawSuperEllipse(0, 0, d, d, 5)",0,height/3);
pop();
}
function drawSuperEllipse(x, y, w, h, n = random(1.5, 5)) {
push();
translate(x, y);
let na = 2 / n;
beginShape();
for (let angle = 0; angle < 360; angle += 1) {
let nx = ((pow(abs(cos(angle)), na) * w) / 2) * sgn(cos(angle));
let ny = ((pow(abs(sin(angle)), na) * h) / 2) * sgn(sin(angle));
vertex(nx, ny);
}
endShape(CLOSE);
pop();
}
function sgn(val) {
if (val == 0) {
return 0;
}
return val / abs(val);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment