processinggenerative-art

Failed attempt at goto 10 in Processing


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

Solution

  • 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