Random and If Else help

Maybe this is a clearer example and illustrates the problem better…

int time;
//coordinates for drawing rectangles
float x1;     //= random(0,width/2);
float y1; 
float x2; 
float y2;

void setup()  {
  size(400, 400);
  background(255);
  
  //turn of any fill
  noFill();
  
  //change Rectangle draw mode
  rectMode(CENTER);
  
}

void draw()  {
  frameRate(3);

  
  stroke(0,255,0);
  rect(width/2,height/2,200,200); 

   x1 = random(0,200); 
    if(x1 >= 100){
      y1 = random(0,100);
    }
    else{y1 = random(100,200);
    }
    
    //y1 = random(0,height/2); //don't need because y1 is defined above
    if(y1 >= 100){
      x2 = random(0,100);
    }
    else{x2 = random(100,200);
    }
    
    //x2 = random(0,width/2);
    if(x2 >= 100){
      y2 = random(0,100);
    }
    else{y2 = random(100,200);    
    }
    
  //draw rectangles based on x,y coordinates in each quad
  stroke(0);
  //upper left
  line(x1,y1,x2,y1);
  line(x2,y1,x2,y2);
  line(x2,y2,x1,y2);
  line(x1,y2,x1,y1);
  
}

Why is it if x1 is less than 100, y1 is only greater than 100?

NEVER MIND!! Oh my god… because I was only letting it be between 100,200 with the else statement!! Still not working correctly… see below:

int time;
//coordinates for drawing rectangles
float x1;     //= random(0,width/2);
float y1; 
float x2; 
float y2;

void setup()  {
  size(400, 400);
  background(255);
  
  //turn of any fill
  noFill();
  
  //change Rectangle draw mode
  rectMode(CENTER);
  
}

void draw()  {
  frameRate(3);

  
  stroke(0,255,0);
  rect(width/2,height/2,200,200); 
  
    //upper left
    //looking to let x1 be anywhere between 0 and width/2, BUT "if" it is
    //greater than width/4, then y1 needs to be between 0 and height/4. "IF" it is less than width/4
    //then y1 can be anywhere between 0 and height/2. 

    //x1 = random(0,width/2); //when commented ON, still allows x1 to fall inside area not wanted.
    //if(x1 >= width/4){
    //  y1 = random(0,height/4);
    //}
    //else{y1 = random(height/4,height/2);
    //}
    
    x1 = random(0,200); 
    if(x1 >= 100){
      y1 = random(0,100);
    }
    else{y1 = random(0,200);
    }
    
    //y1 = random(0,height/2); //don't need because y1 is defined above
    if(y1 >= 100){
      x2 = random(0,100);
    }
    else{x2 = random(0,200);
    }
    
    //x2 = random(0,width/2);
    if(x2 >= 100){
      y2 = random(0,100);
    }
    else{y2 = random(0,200);    
    }
    
  //draw rectangles based on x,y coordinates in each quad
  stroke(0);
  //upper left
  line(x1,y1,x2,y1);
  line(x2,y1,x2,y2);
  line(x2,y2,x1,y2);
  line(x1,y2,x1,y1);
  
}
1 Like