Skip to content

Instantly share code, notes, and snippets.

@illucent
Forked from brysonian/superformula.pde
Created September 5, 2016 16:07
Show Gist options
  • Save illucent/39bea633e1f653d6010d4e0e72204d03 to your computer and use it in GitHub Desktop.
Save illucent/39bea633e1f653d6010d4e0e72204d03 to your computer and use it in GitHub Desktop.
Sketch to draw the Superformula and save it to a PDF.
import controlP5.*;
import processing.pdf.*;
float scaler = 50;
int m = 2;
float n1 = 1;
float n2 = 1;
float n3 = 1;
boolean save = false;
ControlP5 controlP5;
ControlGroup group;
void setup() {
size(500, 500);
smooth();
controlP5 = new ControlP5(this);
group = controlP5.addGroup("myGroup",0,0);
controlP5.addSlider("scaler", 0, 250, scaler, 10, 420, width - 50, 10).setGroup(group);
controlP5.addSlider("m", 0.001, 20, m, 10, 432, width - 50, 10).setGroup(group);
controlP5.addSlider("n1", 0.001, 20, n1, 10, 444, width - 50, 10).setGroup(group);
controlP5.addSlider("n2", 0.001, 20, n2, 10, 456, width - 50, 10).setGroup(group);
controlP5.addSlider("n3", 0.001, 20, n3, 10, 468, width - 50, 10).setGroup(group);
controlP5.addButton("save", 1, 10, 480, 100, 10).setGroup(group);
}
void draw() {
noFill();
if (save) {
beginRecord(PDF, "superformula_"+m+"_"+n1+"_"+n2+"_"+n3+".pdf");
PFont f = createFont("", 10);
textFont(f);
fill(0);
text("m: "+m, 10, 420);
text("n1: "+n1, 10, 432);
text("n2: "+n2, 10, 444);
text("n3: "+n3, 10, 456);
noFill();
} else {
stroke(255);
background(0);
}
pushMatrix();
translate(width/2, height/2 - 50);
beginShape();
PVector[] points = superformula(m, n1, n2, n3);
for (int i=0;i<points.length;i++) {
vertex(points[i].x * scaler, points[i].y * scaler);
}
endShape();
popMatrix();
if (save) {
endRecord();
save = false;
}
}
PVector[] superformula(float m,float n1,float n2,float n3) {
int NP = 360;
float phi = TWO_PI / NP;
PVector[] points = new PVector[NP+1];
for (int i=0;i<=NP;i++) {
points[i] = superformulaPoint(m,n1,n2,n3,phi * i);
}
return points;
}
PVector superformulaPoint(float m,float n1,float n2,float n3,float phi) {
float r;
float t1,t2;
float a=1,b=1;
float x = 0;
float y = 0;
t1 = cos(m * phi / 4) / a;
t1 = abs(t1);
t1 = pow(t1,n2);
t2 = sin(m * phi / 4) / b;
t2 = abs(t2);
t2 = pow(t2,n3);
r = pow(t1+t2,1/n1);
if (abs(r) == 0) {
x = 0;
y = 0;
} else {
r = 1 / r;
x = r * cos(phi);
y = r * sin(phi);
}
return new PVector(x, y);
}
void keyPressed() {
if (group.isVisible()) {
group.hide();
} else {
group.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment