Created
April 30, 2013 20:29
-
-
Save ramayac/5491690 to your computer and use it in GitHub Desktop.
Superformula sketch with controlP5
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
/** | |
* Visualize: Superformula | |
* from Form+Code in Design, Art, and Architecture | |
* by Casey Reas, Chandler McWilliams, and LUST | |
* Princeton Architectural Press, 2010 | |
* ISBN 9781568989372 | |
* | |
* This code was written for Processing 1.2+ | |
* Get Processing at http://www.processing.org/download | |
* | |
* Added controls by @ramyac | |
*/ | |
import controlP5.*; | |
float scaler = 200; | |
int m = 2; | |
float n1 = 18; | |
float n2 = 1; | |
float n3 = 1; | |
ControlP5 cp5; | |
void setup() { | |
size(700, 700); | |
smooth(); | |
noFill(); | |
stroke(255); | |
cp5 = new ControlP5(this); | |
cp5.addSlider("scaler").setPosition(100,25).setRange(0,250); | |
cp5.addSlider("m").setPosition(100,50).setRange(0,255); | |
cp5.addSlider("n1").setPosition(100,75).setRange(0,50); | |
cp5.addSlider("n2").setPosition(100,100).setRange(0,10); | |
cp5.addSlider("n3").setPosition(100,125).setRange(0,10); | |
} | |
void draw() { | |
background(0); | |
pushMatrix(); | |
translate(width/2, height/2); | |
float newscaler = scaler; | |
for (int s = 16; s > 0; s--) { | |
beginShape(); | |
float mm = m + s; | |
float nn1 = n1 + s; | |
float nn2 = n2 + s; | |
float nn3 = n3 + s; | |
newscaler = newscaler * 0.98; | |
float sscaler = newscaler; | |
PVector[] points = superformula(mm, nn1, nn2, nn3); | |
curveVertex(points[points.length-1].x * sscaler, points[points.length-1].y * sscaler); | |
for (int i = 0;i < points.length; i++) { | |
curveVertex(points[i].x * sscaler, points[i].y * sscaler); | |
} | |
curveVertex(points[0].x * sscaler, points[0].y * sscaler); | |
endShape(); | |
} | |
popMatrix(); | |
} | |
PVector[] superformula(float m, float n1, float n2, float n3) { | |
int numPoints = 360; | |
float phi = TWO_PI / numPoints; | |
PVector[] points = new PVector[numPoints+1]; | |
for (int i = 0;i <= numPoints;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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment