Problem with 'for' loops and geometric shapes

Hi Everyone. I’m stuck with a homework assignment. I need to draw a geometric shape (I’ve chosen the rhodonea curve), show the use of an interative “for” loop and use a mouse command. (we have to show all the math).

I came up with code for the geometric rose shape. See first set of code below. Its probably not great code but works.

I then tried to recreate it using a ‘for’ loop in the second set of code below. It gets all messed up. I tried stepping through the debugger to see what I’m doing wrong, but the variable values are basically identical. Any suggestions on how to fix the code would be very appreciated. Thank you in advance.

CODE SET 1

//VARIABLES
float k = 2/9.0;
color persianBlue = color(44, 47, 222, 20);
int dcount = 0;// this a variable to track how many lines are drawn.
//SETUP
void setup()
{
  size(800,800);
  background(255);
  strokeWeight (0.01);
 
}

void draw()
{
  translate (width/2, height/2);
  scale (200,200);
 
 
  float t=frameCount/20.0;
  float x=cos(k*t)*sin(t);
  float y=cos(k*t)*cos(t);
 
  stroke (persianBlue);
  line (0,0,x,y);
  dcount++;
  if (dcount > 1067)
  {
    noLoop();
  }



CODE SET 2




//VARIABLES

float k = 2/9.0;

color persianBlue = color(44, 47, 222, 20);

int dcount = 0;// this a variable to track how many lines are drawn.

//SETUP

void setup()

{

  size(800,800);

  background(255);

  strokeWeight (0.01);

}



void draw()

{

  translate (width/2, height/2);

  scale (200,200);

  for(float t = 0.05; t<54; t = t + 0.05)

  {

  //float t=frameCount/20.0;

  float x=cos(k*t)*sin(t);

  float y=cos(k*t)*cos(t);

  stroke (persianBlue);

  line (0,0,x,y);

  }

}
 
}
1 Like

draw() runs automatically again and again, about 60 times per second

So first thing: insert noLoop();

here is my version, but for (float t = 0.05; t<54; t = t + 0.05) { seems to work as well.



//VARIABLES
float k = 2/9.0;
color persianBlue = color(44, 47, 222, 20);
int dcount = 0;// this a variable to track how many lines are drawn.

//SETUP
void setup() {
  size(800, 800);
  background(255);
  strokeWeight (0.01);
}

void draw() {
  translate (width/2, height/2);
  scale (200, 200);

  //  for (float t = 0.05; t<54; t = t + 0.05) {
  for (float i = 0; i<1067; i++) {

    float t = (float) i / 20.0;
    float x=cos(k*t)*sin(t);
    float y=cos(k*t)*cos(t);

    stroke (persianBlue);
    line (0, 0, x, y);
  }
  noLoop();
}
// 

2 Likes

Thank you, Chris. This is my 2nd week of learning Processing - much appreciated and I will try your solution.

2 Likes

Hello @lemoney,

For an interactive animation with the mouse you will need to use draw() with a background(). Do not use noLoop() for interactive sketches.

I created this animation with your original code:

flowers.gif.mov

Changes:

line() was replaced with point()

for(float t = 0.05; t<54; t = t + 0.05)

was replaced with:

for (float t = 0; t<f*TWO_PI; t = t + TWO_PI/360)

Try the f variable from 0 to 9 and see what happens!

References:

I had fun with this! Thanks for the topic.

:)

2 Likes

Thank You so much :slight_smile:

2 Likes