Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@AlcaDesign
Last active February 9, 2017 08:58
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 AlcaDesign/004d471dc8879c65794208cf29d1283d to your computer and use it in GitHub Desktop.
Save AlcaDesign/004d471dc8879c65794208cf29d1283d to your computer and use it in GitHub Desktop.
class Rose {
PShape shape;
PVector stemStart, stemEnd;
float targetStemLength, growthTimeStart, growthTimeEnd;
float n, d, targetR, r;
float i;
Rose(int index) {
stemStart = new PVector(
random(
width / 16.0,
width / 16.0 * 15.0
),
//randomGaussian() * (width / 16.0 * 7),
height
);
stemEnd = stemStart.copy();
targetStemLength = random(
height / 6.0,
height / 4.0 * 3.0
);
growthTimeStart = millis();
growthTimeEnd = growthTimeStart + map(
targetStemLength,
height / 6.0, height / 4.0 * 3.0,
2000, 5000
);
n = round(random(4, 7));
d = round(random(4, 9));
if(n == d) {
d--;
}
targetR = random(
width / ((float)roses.length * 4.0),
width / ((float)roses.length * 2.0)
);
r = 0;
i = index;
generateShape();
}
void grow() {
float now = millis();
if(now < growthTimeEnd) {
stemEnd.y = lerp(
height,
height - targetStemLength,
map(
now,
growthTimeStart, growthTimeEnd,
0, 1
)
);
}
else if(now < growthTimeEnd * 1.2) {
r = lerp(
0, targetR,
map(
now,
growthTimeEnd, growthTimeEnd * 1.2,
0, 1
)
);
generateShape();
}
}
void update() {
grow();
stemEnd.x = cos((frameCount + i * 4) / 30.0) * r + stemStart.x;
}
void render() {
strokeWeight(3);
stroke(100, 255, 0);
noFill();
//line(stemStart.x, stemStart.y, stemEnd.x, stemEnd.y);
bezier(
stemStart.x, stemStart.y,
stemStart.x + stemEnd.x * 0.1, stemStart.y - stemEnd.y * 0.1,
(stemStart.x + stemEnd.x) / 2.0, (stemStart.y + stemEnd.y) / 2.0,
stemEnd.x, stemEnd.y
);
float now = millis();
if(now < growthTimeEnd) return;
translate(stemEnd.x, stemEnd.y);
rotate(frameCount / 30.0);
shape(shape, 0, 0);
}
void generateShape() {
shape = createShape();
float k = n / d;
shape.beginShape();
shape.strokeWeight(3);
shape.stroke(255, 50, 0);
shape.fill(255, 50, 0, 200);
for(float i = 0; i <= TWO_PI * d; i += 0.1) {
shape.vertex(
cos(k * i) * cos(i) * r,
cos(k * i) * sin(i) * r
);
}
shape.endShape(CLOSE);
}
}
Rose[] roses;
void setup() {
size(600, 600);
roses = new Rose[round(random(2, 7))];
for(int i = 0; i < roses.length; i++) {
Rose r = new Rose(i);
roses[i] = r;
}
noFill();
}
void draw() {
background(0);
for(Rose r : roses) {
pushMatrix();
r.render();
r.update();
popMatrix();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment