# How to use for loops with transformations in processing?

**URL:** <https://discourse.processing.org/t/how-to-use-for-loops-with-transformations-in-processing/12782>\
**Category:** Coding Questions\
**Created:** [July 18, 2019, 11:41am UTC](https://discourse.processing.org/t/how-to-use-for-loops-with-transformations-in-processing/12782 "2019-07-18T11:41:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![phanirithvij](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/phanirithvij/32/5861_2.png) [@phanirithvij](https://discourse.processing.org/u/phanirithvij)\
**Post date:** [July 18, 2019, 11:41am UTC](https://discourse.processing.org/t/how-to-use-for-loops-with-transformations-in-processing/12782/1 "2019-07-18T11:41:12Z")

</div>

So this is the code I wrote which I intend to draw `n` lines starting from a single point. Each separated by an angle of `2*PI / n`.

```java
int n;

void setup(){
  size(displayWidth, displayHeight);
  n = 7;
}
void draw(){
    background(0);
    push();
    translate(displayWidth/2, displayHeight/2);
    strokeWeight(4);

    for (int i=0; i < n; i++){
      stroke(random(255), random(255), random(255));
      //println(i);
      //println("theta is", i*(2*PI/n));
      //println("theta in deg is", i*(2*PI/n)*180/PI);
      rotate(i*(2*PI/n));
      line(0, 0, 200 * 2, 0);
    }
    //noLoop();
    pop();
}

void keyPressed(){
  if (key == '='){
    n++;
  } else if (key == '-'){
    n--;
    if (n <= 0) n = 1;
  } 
}

```

It is wrong for some reason as it doesn’t work for `n=3,5,6,7,9,10..`.  
I mean it doesn’t work.

It is working only when n is `1,2,4,8,16,32...` i.e. only 2 multiples.

I must be doing something wrong. Any help appreciated.

Use `-, =` keys to alter the spike count.

---

<div class="post-metadata">

**Author:** ![phanirithvij](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/phanirithvij/32/5861_2.png) [@phanirithvij](https://discourse.processing.org/u/phanirithvij)\
**Post date:** [July 18, 2019, 11:56am UTC](https://discourse.processing.org/t/how-to-use-for-loops-with-transformations-in-processing/12782/2 "2019-07-18T11:56:08Z")

</div>

@gotoloop can you help me?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 18, 2019, 12:07pm UTC](https://discourse.processing.org/t/how-to-use-for-loops-with-transformations-in-processing/12782/3 "2019-07-18T12:07:56Z")

</div>

Sorry, my trigonometry is almost nil. 😖

---

<div class="post-metadata">

**Author:** ![phanirithvij](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/phanirithvij/32/5861_2.png) [@phanirithvij](https://discourse.processing.org/u/phanirithvij)\
**Post date:** [July 18, 2019, 12:24pm UTC](https://discourse.processing.org/t/how-to-use-for-loops-with-transformations-in-processing/12782/4 "2019-07-18T12:24:58Z")

</div>

> [@phanirithvij](#):
>
> rotate(i\*(2\*PI/n)); line(0, 0, 200 \* 2, 0);

So I replaced these lines with this

```java
line(0, 0, 200 * 2 * cos(i*(2*PI/n)), 200 *2 * sin(i*(2*PI/n)));

```

i.e. normal polar coordinates `(r*cos(a), r*sin(a))`

And it works. Anyone knows why `rotate` didn’t work?

---

<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:** [July 18, 2019, 12:43pm UTC](https://discourse.processing.org/t/how-to-use-for-loops-with-transformations-in-processing/12782/5 "2019-07-18T12:43:32Z")

</div>

Hello!

Please remember that `rotate()` in the line `rotate(i*(2*PI/n));`_adds up_ the angles

So you have to use `push` and `pop` _inside_ the for-loop to avoid the adding up:

(using + and - here)

```auto
int n = 7;

void setup() {
  size(displayWidth, displayHeight);
}

void draw() {
  background(0);

  strokeWeight(4);

  for (int i=1; i <= n; i++) {

    stroke(random(255), random(255), random(255));
    pushMatrix();
    translate(displayWidth/2, displayHeight/2);
    //println(i);
    //println("theta is", i*(2*PI/n));
    //println("theta in deg is", i*(2*PI/n)*180/PI);
    rotate(i*(2*PI/n));
    line(0, 0, 200 * 2, 0);
    popMatrix();
  }// for 
  //noLoop();
  fill(255);
  text(n, 33, 33);
}

void keyPressed() {
  if (key == '+') {
    n++;
  } else if (key == '-') {
    n--;
    if (n <= 0) n = 1;
  }
}

```

* * *

OR

you **use the fact** that `rotate()` adds up the angles and then you just _don’t multiply_ by `i`

you just rotate a step further inside the for loop

Chrisir

```auto

int n = 7;

void setup() {
  size(displayWidth, displayHeight);
}

void draw() {
  background(0);
  strokeWeight(4);

  pushMatrix();
  translate(displayWidth/2, displayHeight/2);

  for (int i=1; i <= n; i++) {
    stroke(random(255), random(255), random(255));
    rotate(2*PI/n); // !!!!
    line(0, 0, 200 * 2, 0);
  }// for

  popMatrix(); 

  fill(255);
  text(n, 33, 33);
}

void keyPressed() {
  if (key == '+') {
    n++;
  } else if (key == '-') {
    n--;
    if (n <= 0) n = 1;
  }
}

```

---

<div class="post-metadata">

**Author:** ![phanirithvij](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/phanirithvij/32/5861_2.png) [@phanirithvij](https://discourse.processing.org/u/phanirithvij)\
**Post date:** [July 18, 2019, 12:52pm UTC](https://discourse.processing.org/t/how-to-use-for-loops-with-transformations-in-processing/12782/6 "2019-07-18T12:52:46Z")

</div>

Thank you very much. Now I understand. I thought I was doing push and pop in the loop all this time.
