Skip to content

Instantly share code, notes, and snippets.

@jeesunikim
Last active January 24, 2019 02:42
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 jeesunikim/d2227ce4d314c9fd18f22b775610a10c to your computer and use it in GitHub Desktop.
Save jeesunikim/d2227ce4d314c9fd18f22b775610a10c to your computer and use it in GitHub Desktop.
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