Hello, I am making a “drawing” program and running into an issue. I’ve written a smaller, less complex sample of the program pasted below and am hoping someone can help. Essentially, I want to draw rectangles within a certain area of the screen (for the sample case, the upper left quad) but not within the space of the center area. I want to “draw” the rectangles by determining x,y coordinates instead of calling rectangles through the program, and am doing this by allowing them to be random float values within parameters. I would think I could declare the limit I want (x1 = random(0,width/2);), and then make a conditional statement that determines the other locations, however, when this statement is commented on, it seems the if/else statement is ignored? If it is commented off, it seems to be doing what I want for all the other variables (y1,x2,y2), but makes x1 = 0. Any advice?
int time;
//coordinates for drawing rectangles
float x1;
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(0,height/2);
}
//y1 = random(0,height/2); //don't need because y1 is defined above
if(y1 >= height/4){
x2 = random(0,width/4);
}
else{x2 = random(0,width/2);
}
//x2 = random(0,width/2);
if(x2 >= width/4){
y2 = random(0,height/4);
}
else{y2 = random(0,height/2);
}
//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);
}