Created
September 24, 2025 06:10
-
-
Save WilliamLivingood/2705e0f2af053ea5818ca4ca32d4e415 to your computer and use it in GitHub Desktop.
Processing Code to Generate a Flower (rose)
This file contains hidden or 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
| import Turtle.*; | |
| import processing.svg.*; | |
| Turtle t; | |
| void setup() { | |
| size(1050, 1050); | |
| noLoop(); | |
| } | |
| void draw() { | |
| beginRecord(SVG, "rose.svg"); | |
| background(255); | |
| stroke(0); | |
| strokeWeight(3); | |
| noFill(); | |
| t = new Turtle(this); | |
| float cx = width*0.5f, cy = height*0.5f; | |
| hex(cx, cy, 500); | |
| lotus(cx, cy, 360); | |
| endRecord(); | |
| } | |
| /* helpers */ | |
| PVector polar(float cx, float cy, float r, float a) { | |
| return new PVector(cx + r*cos(a), cy + r*sin(a)); | |
| } | |
| /* shapes */ | |
| void hex(float cx, float cy, float r) { | |
| float rot = -PI/6f; | |
| PVector p0 = polar(cx, cy, r, rot); | |
| t.penUp(); t.goToPoint(p0.x, p0.y); t.penDown(); | |
| for (int i = 1; i <= 6; i++) { | |
| float a = rot + TWO_PI*i/6f; | |
| PVector p = polar(cx, cy, r, a); | |
| t.goToPoint(p.x, p.y); | |
| } | |
| t.penUp(); | |
| } | |
| void lotus(float cx, float cy, float R) { | |
| int n = 8; | |
| float r1i = R*0.28f, r1o = R*0.92f, w1 = radians(48); | |
| for (int k = 0; k < n; k++) { | |
| petal(cx, cy, r1i, r1o, TWO_PI*k/n, w1, 5, 0.06f); | |
| } | |
| float r2i = r1i+R*0.08f, r2o = r1o+R*0.12f, w2 = radians(42); | |
| for (int k = 0; k < n; k++) { | |
| petal(cx, cy, r2i, r2o, TWO_PI*k/n + w2*0.5f, w2, 5, 0.06f); | |
| } | |
| centerPoly(cx, cy, r1i*0.45f, 8); | |
| } | |
| void petal(float cx, float cy, float rIn, float rOut, float theta, float angW, int facets, float notchK) { | |
| float aL = theta - angW*0.5f, aR = theta + angW*0.5f; | |
| PVector p0 = polar(cx, cy, rIn, aL); | |
| t.penUp(); t.goToPoint(p0.x, p0.y); t.penDown(); | |
| PVector pL = polar(cx, cy, rOut, aL); | |
| t.goToPoint(pL.x, pL.y); | |
| int N = max(3, facets); | |
| for (int i = 1; i <= N; i++) { | |
| float u = i/(float)N; | |
| float a = lerp(aL, aR, u); | |
| float bulge = 1.06f + 0.06f*sin(PI*u); | |
| float notch = (abs(u-0.5f) < 0.18f) ? -notchK*rOut*(1f - abs(u-0.5f)/0.18f) : 0f; | |
| PVector p = polar(cx, cy, rOut*bulge + notch, a); | |
| t.goToPoint(p.x, p.y); | |
| } | |
| t.goToPoint(polar(cx, cy, rOut, aR).x, polar(cx, cy, rOut, aR).y); | |
| t.goToPoint(polar(cx, cy, rIn, aR).x, polar(cx, cy, rIn, aR).y); | |
| t.goToPoint(p0.x, p0.y); | |
| t.penUp(); | |
| } | |
| void centerPoly(float cx, float cy, float r, int sides) { | |
| float rot = -PI/sides; | |
| PVector p0 = polar(cx, cy, r, rot); | |
| t.penUp(); t.goToPoint(p0.x, p0.y); t.penDown(); | |
| for (int i = 1; i <= sides; i++) { | |
| float a = rot + TWO_PI*i/sides; | |
| PVector p = polar(cx, cy, r, a); | |
| t.goToPoint(p.x, p.y); | |
| } | |
| t.penUp(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment