Generative Art - Sunflower - Seed
class SunflowerSeed { | |
PVector position; | |
float n1 = 1; | |
float n2 = 1; | |
float n3 = 1; | |
float m = 1; | |
float a = 1; | |
float b = 1; | |
float t = 0; | |
SunflowerSeed(float tempN1, float tempN2, float tempN3, float tempM) { | |
n1 = tempN1; | |
n2 = tempN2; | |
n3 = tempN3; | |
m = tempM; | |
} | |
void toUpdateShape(float tempX, float tempY) { | |
int radius = 10; | |
int total = 200; | |
float increment = TWO_PI / total; // Higher the total, smoother the line | |
translate(tempX, tempY); | |
beginShape(); | |
for (float angle = 0; angle < TWO_PI; angle += increment) { | |
float r = supershape(angle); | |
float deformedShape = 0.6; | |
position = new PVector(radius * deformedShape * r * cos(angle), radius * r * sin(angle)); | |
vertex(position.x, position.y); | |
} | |
endShape(); | |
} | |
float supershape(float theta) { | |
// supershape() receives an angle and needs to return back r | |
// The formula is coming from http://paulbourke.net/geometry/supershape/ | |
// Theta represents an angle | |
float part1 = (1 / a) * cos(theta * m / 4); | |
part1 = abs(part1); | |
part1 = pow(part1, n2); // raise to an exponent | |
float part2 = (1 / b) * sin(theta * m / 4); | |
part2 = abs(part2); | |
part2 = pow(part2, n3); // raise to an exponent | |
float part3 = pow(part1 + part2, 1 / n1); | |
if (part3 == 0) { | |
return 0; | |
} | |
return (1 / part3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment