# Problem with 'for' loops and geometric shapes

**URL:** https://discourse.processing.org/t/problem-with-for-loops-and-geometric-shapes/39387
**Category:** Coding Questions
**Tags:** homework
**Created:** [October 22, 2022, 7:31pm UTC](https://discourse.processing.org/t/problem-with-for-loops-and-geometric-shapes/39387 "2022-10-22T19:31:01Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![lemoney](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@lemoney](https://discourse.processing.org/u/lemoney)
#### Post date: [October 22, 2022, 7:31pm UTC](https://discourse.processing.org/t/problem-with-for-loops-and-geometric-shapes/39387/1 "2022-10-22T19:31:01Z")

</div>

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

```auto
//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);

  }

}
 
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [October 22, 2022, 9:26pm UTC](https://discourse.processing.org/t/problem-with-for-loops-and-geometric-shapes/39387/2 "2022-10-22T21:26:26Z")

</div>

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.

```auto

//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();
}
// 

```

---

<div class="post-metadata">

### Author: ![lemoney](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@lemoney](https://discourse.processing.org/u/lemoney)
#### Post date: [October 23, 2022, 10:44am UTC](https://discourse.processing.org/t/problem-with-for-loops-and-geometric-shapes/39387/3 "2022-10-23T10:44:16Z")

</div>

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

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [October 30, 2022, 12:11pm UTC](https://discourse.processing.org/t/problem-with-for-loops-and-geometric-shapes/39387/4 "2022-10-30T12:11:24Z")

</div>

Hello @lemoney,

> [@lemoney](#):
>
> show the use of an interative “for” loop and use a mouse command.

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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/a/c/acd9e52481075594ad619a5d3441ff5ba469712e.gif)

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:

- [https://processing.org/tutorials/interactivity](https://processing.org/tutorials/interactivity)
- [Rose (mathematics) - Wikipedia](https://en.wikipedia.org/wiki/Rose_(mathematics))
- [https://processing.org/reference/map\_.html](https://processing.org/reference/map_.html) \< map() with mouseX was used for the interactivity

I had fun with this! Thanks for the topic.

`:)`

---

<div class="post-metadata">

### Author: ![lemoney](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@lemoney](https://discourse.processing.org/u/lemoney)
#### Post date: [October 30, 2022, 5:53pm UTC](https://discourse.processing.org/t/problem-with-for-loops-and-geometric-shapes/39387/5 "2022-10-30T17:53:55Z")

</div>

Thank You so much 🙂
