# Random and If Else help

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

}
``````
1 Like

My original approach was more simple in that I set the float values for the coordinates I wanted as:

``````  float x1 = random(0,width/1.8);
float y1 = random(0,height/2.46);
float x2 = random(0,width/1.8);
float y2 = random(0,height/2.46);
``````

(allowing them to extend past the boundary of the quadrant, not important)

which would be used to draw:

``````stroke(0);
//upper left
line(x1,y1,x2,y1);
line(x2,y1,x2,y2);
line(x2,y2,x1,y2);
line(x1,y2,x1,y1);
``````

and was looking for a way to say if(x1>= width/4)&&(y1>=height/4) DO NOTHING. Is that a thing? Maybe I make the drawing a class? And if that statement evaluates to true, skip the drawing? If it evaluates to false, do the drawing?

Sorry if this is super confusing. Let me know if there is other info I should be providing.

1 Like

-a- please first learn how to post code here correctly,
use the

``````</> code tag
``````

and see
```
type or paste code here
```

if setting variables and you not understand the result
there are 2 easy ways:

• please use print statements for the variables
• or set them to fix value

make it a function ( not a class )
to draw a rectangle by lines.

never use

``````random( 0, max )
``````

for this usage, as you might end up to see nothing, better use a minimum

``````random(10,max)
``````

Thank you for the advice on posting code… edited it.

In this case

`random(0,width)`

0 would be the left boundary of the screen? So it is still something? I get your point, but haven’t had this be an issue yet.

And yes, I’ll look into making it a function and printing statements for the variables.

you can just do a random rect and the check for collision / intersection

if we have a intersection, don’t draw it.

(please note that you use 2 points to define a rect but the code in the link uses x,y and w,h instead;
also get rid of rectMode(CENTER); )

1 Like

@Chrisir, I may try that approach (and realize it would be much easier), but I really want to be using x,y coordinates to “draw” the rectangles with lines connecting the randomly determined coordinate locations. I think I will turn the four lines into a function (as @kll suggested) and then try to use collision detection.

I’m struggling to understand why the approach in my first post isn’t working when

`//x1 = random(0,width/2);` is commented on.

I would think that is saying, “okay, chose a value for x1 between 0 and half width” and the next line “if” x1 is greater than width/4, make y1 be between 0 and height/4, “if else” let y1 be anywhere between 0 and height/2… anyone have thoughts on that?

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

when you say

``````x1 = random(0,200);
``````

shouldn’t x2 be

``````x2 = random(x1+1,200);
``````

or what ever but random not starting at 0 but at x1 ?

1 Like

@Chrisir I haven’t had a chance to figure out WHY this is working correctly, but !!! it is!!!

Thank you so much!!! Maybe I’ll post the real/final version when I get it sorted.

reason is, when x2 < x1 it still looks like a normal rectangle, but your lower right corner is left from your upper right corner so both corners are swapped.

And then your ifs won’t work I guess

Code

here is my way is collision detection according to the link above:

``````//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();

// frameRate(3);
}

void draw() {
stroke(0, 255, 0); //green
rect(width/2-100, height/2-100, 200, 200);

x1 = random(0, width-10);
y1 = random(0, height-10);

x2 = random(x1+1, width);
y2 = random(y1+1, height);

// if ok
if (!intersectionRectRect (
width/2-100, height/2-100, 200, 200,
x1, y1, x2-x1, y2-y1)) {

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

//----------------------------------------------------------------------------------

// RECTANGLE/RECTANGLE intersection
boolean intersectionRectRect(float r1x, float r1y, float r1w, float r1h,
float r2x, float r2y, float r2w, float r2h) {

// are the sides of one rectangle touching the other?

if (r1x + r1w >= r2x &&  // r1 right edge past r2 left
r1x <= r2x + r2w &&    // r1 left edge past r2 right
r1y + r1h >= r2y &&    // r1 top edge past r2 bottom
r1y <= r2y + r2h) {    // r1 bottom edge past r2 top
return true;
}
return false;
}
``````
1 Like

@Chrisir Thank you so much for your help. Your explanations were extremely helpful and I can’t wait to check out the collision detection code you worked out. This has been such a great learning experience.

1 Like