0
votes

I am using Processing to try and draw something and I'm stuck on a FOR loop that I'm trying to run.

I have a sketch that I would like to make shorter by taking advantage of the Translate and Rotate commands. However, for some reason I can't get this to work. If I comment out the first FOR loop ("for (int k = 0;...)" it draws, correctly, in one of my 8 quadrants. I just want to then rotate that 45 degrees, draw again, and continue until it goes full circle.

int hHeight, hWidth;


void setup(){
 size(800,800);
 hHeight = height/2;
 hWidth = width/2;
 background(0);
 strokeWeight(.5);


}
void grid(){
  stroke(255);
  line(hWidth,0,hWidth,height);
  line(0,hHeight,width,hHeight);
  line(width,0,0,height);
  line(0,0,width,height);
}


void rotatingGrid(float steps){  // This is the one I am having trouble with...
   for (int k = 0; k == 8; k ++){  // loop this 8 times, to complete 8 rotations
      pushMatrix();
      translate(hWidth,hHeight);
      rotate(radians(45));
      for (int i = 0; i < hWidth; i+= steps){
        line(i,0,hWidth-i,hHeight-i);  
      }
     popMatrix();
  }
}

void draw(){
   stroke(255);
   grid();
   rotatingGrid(10);
 }

So if you comment out the first FOR loop (and the closing bracket), it correctly works. How can I loop that, rotating it 7 times, instead of just putting the "rotate(radians(45))" and the FOR loop 7 more times?

Thanks for any advice or help.

1

1 Answers

1
votes

the first for loop should be (int k = 0; k< 8; k++) if you want it to run 8 times. If you want the angle to go 45, 90, ... 360, then you can add a variable like this:

int angle = 0;
....
angle+=45;
rotate(radians(angle));
....

also, you may want to swap rotate and translate if you want to rotate about the object's origin instead of having it in a bigger circle, not sure what effect you want here.