#Can anybody help me to create a generative code on processing?

12 messages · Page 1 of 1 (latest)

drifting patrol
#

hello! I am a beginner in processing and I have an idea of drawing a flower. Starting from the middle of the canvas, lines are drawn to create petals while rotating and eventually creating a flower shape. I attempted to code my idea but it's still off? I also tried getting help from AI but it is not much of a help? If anyone can help me create my idea illustrated in the provided photo, that would be deeply appreciated. I have provided the code I have now as well. Thank you.

float angle = 0;
float radius = 1;
float petalLength = 100;
int numPetals = 100;

void setup() {
size(1200, 800);
background(255);
noFill();
stroke(0);
}

void draw() {
translate(width / 2, height / 2); //starting point

if (angle < TWO_PI) {
float x1 = cos(angle) * radius;
float y1 = sin(angle) * radius;
float x2 = cos(angle) * (radius + petalLength);
float y2 = sin(angle) * (radius + petalLength);

line(x1, y1, x2, y2);

angle += TWO_PI / numPetals;

} else {
// circle as flower's core
fill(100,0,20);
ellipse(0, 0, 20, 20);

// Reset angle for the next flower
angle = 0;

// Increase the radius for the next set of petals
radius += 20;

// Change the number of petals for variety
numPetals = int(random(5, 15));

}
}

reef plover
#

Hello @drifting patrol , the flower seems to be quite a bit off due to the straight lines you're using...

#

Had you used ellipses, this would improve

drifting patrol
reef plover
#

Here is the code I have written:

  size(1200, 800);
  background(255);
}

void draw() {
  translate(width / 2, height / 2); //starting point
  
  // the layers of petals
  fill(255, 102, 26);
  noStroke();
  for (int i = 0; i < 22; i ++) {
    ellipse(0, 30, 20, 190);
    rotate(PI/11);
  }
  fill(255, 166, 77);
  for (int i = 0; i < 16; i ++) {
    ellipse(0, 30, 20, 160);
    rotate(PI/8);
  }
  fill(255, 191, 128);
  for (int i = 0; i < 14; i ++) {
    ellipse(0, 30, 20, 115);
    rotate(PI/7);
  }
  
  // centre of the flower is drawn at the last
  fill(100, 0, 10);
  circle(0, 0, 85);
  // detailing of the centre
  fill(204, 153, 0);
  circle(0, 0, 65);
  
  fill(255, 191, 0);
  circle(0, 0, 40);
  
} ```
#

The result:

drifting patrol
#

float angle = 0;
float radius = 300;
float petalLength = 50;
int petalNumber = 10;

void setup() {
size(1200, 900);
background(255);
stroke(0);
frameRate(15);
}

void draw() {
translate(width/2, height/2);
fill(255, 153, 153);

if (radius > 0) { // Check if the radius is greater than zero
if (angle < TWO_PI) {
float x1 = cos(angle) * radius;
float y1 = sin(angle) * radius;
float x2 = cos(angle) * (radius - petalLength);
float y2 = sin(angle) * (radius - petalLength);

  // Draw ellipse petals
  float petalWidth = 50;
  float petalHeight = random(200, 250);
  float petalRotation = angle - PI / 2;

  pushMatrix();
  translate(x1, y1);
  rotate(petalRotation);
  ellipseMode(CORNER);
  ellipse(0, 0, petalWidth, petalHeight);
  popMatrix();

  angle += TWO_PI / petalNumber;
  
}  else {
  // Draw a circle at the center to represent the flower's core
  ellipseMode(CENTER);
  ellipse(0, 0, 80, 80);

  // Reset angle for the next flower
  angle = 0;

  // Decrease the radius for the next set of petals
  radius -= 30;

  // Change the number of petals for variety
  petalNumber = int(random(5, 60));
}

}
}

#

So far, I have this : D. Just one question, what can i do if i want the petal colour to gradually change?

reef plover
#

I have modified your code a bit, see the difference : )
The code:

float radius = 300;
float petalLength = 50;
int petalNumber = 10;

void setup() {
  size(1200, 900);
  background(255);
  //stroke(0);
  noStroke();
  frameRate(15);
}

void draw() {
  translate(width/2, height/2);
  // change into pretty colors of the petals
  fill(255, random(70,150), random(153));

  if (radius > 0) { // Check if the radius is greater than zero
    if (angle < TWO_PI) {
      float x1 = cos(angle) * radius;
      float y1 = sin(angle) * radius;
      //float x2 = cos(angle) * (radius - petalLength);
      //float y2 = sin(angle) * (radius - petalLength);

      // Draw ellipse petals
      float petalWidth = 50;
      float petalHeight = random(200, 250);
      float petalRotation = angle - PI / 2;

      pushMatrix();
      translate(x1, y1);
      rotate(petalRotation);
      ellipseMode(CORNER);
      ellipse(0, 0, petalWidth, petalHeight);
      popMatrix();

      angle += TWO_PI / petalNumber;

    }  else {
      // Draw a circle at the center to represent the flower's core
      fill(153, 77, 0);
      ellipseMode(CENTER);
      ellipse(0, 0, 200, 200);

      // Reset angle for the next flower
      angle = 0;

      // Decrease the radius for the next set of petals
      radius -= 30;

      // Change the number of petals for variety
      petalNumber = int(random(5, 60));
    }
  }
} 
#

float x2 and y2 are not required in this code

#

It looks like a sunflower, if I'm right