1

I've tried a Goto 10 exercise on Processing and have no idea where is my mistake. The intent was to make a Goto 10 like scenario but there's something I'm missing. I believe the loop may be off.

//line size
int a = 15;

void setup(){
  background(255);
  size(500,500);
  noLoop();
  strokeWeight(2);
}

void draw(){
  //    Y
  for(int lineY = 0; lineY < height/a; lineY++){
    
    //   X
  for(int lineX = 0; lineX < width/a; lineX++){
  float randomN = random(1);
  
  pushMatrix();
  if (randomN >= 0.5){
  rotate(-90);
  }
  else {
  rotate(-45);
  }
  
  line(0,0,a,0);
  popMatrix();
  translate(a, 0);
  }
  translate((-width), a);
 }
}
ggorlen
  • 44,755
  • 7
  • 76
  • 106
ques
  • 13
  • 2
  • Your answer is in the documentation : https://processing.org/reference/noLoop_.html – Kokodoko Nov 29 '21 at 23:42
  • Hi, in the future you'll get better answers if you can more clearly explain your question. What are you expecting to see and what exactly is the problem? It helps to avoid confusion and makes it easier for others to give help. – Cadin Nov 30 '21 at 00:35

1 Answers1

1

The amount by which you're translating the x movements of each column don't add up to the full width of the sketch:
for(int lineX = 0; lineX < width/a; lineX++)

width/a will be 33.333 in your example (500 / 15). So you'll end up with 33 loops and 33 columns. But 33 columns * 15 px wide = 495 (not 500).

So when you try to translate back to the beginning of the new row with translate((-width), a); you're moving back a little bit too far each row (-500 instead of -495).

To fix it, make sure you only move back the distance that you moved forward while drawing the columns:

int numCols = floor(width/a); // calculate the number of columns
translate((-numCols * a), a); // move back the correct x distance
Cadin
  • 4,275
  • 1
  • 14
  • 22