# Random and If Else help

**URL:** <https://discourse.processing.org/t/random-and-if-else-help/11718>\
**Category:** Coding Questions\
**Created:** [May 30, 2019, 7:12pm UTC](https://discourse.processing.org/t/random-and-if-else-help/11718 "2019-05-30T19:12:37Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![b3pkay](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@b3pkay](https://discourse.processing.org/u/b3pkay)\
**Post date:** [May 30, 2019, 7:12pm UTC](https://discourse.processing.org/t/random-and-if-else-help/11718/1 "2019-05-30T19:12:37Z")

</div>

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?

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

```

---

<div class="post-metadata">

**Author:** ![b3pkay](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@b3pkay](https://discourse.processing.org/u/b3pkay)\
**Post date:** [May 31, 2019, 12:13am UTC](https://discourse.processing.org/t/random-and-if-else-help/11718/2 "2019-05-31T00:13:06Z")

</div>

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

```auto
  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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [May 31, 2019, 12:37am UTC](https://discourse.processing.org/t/random-and-if-else-help/11718/3 "2019-05-31T00:37:58Z")

</div>

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

```auto
</> 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

```auto
random( 0, max ) 

```

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

```auto
random(10,max) 

```

---

<div class="post-metadata">

**Author:** ![b3pkay](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@b3pkay](https://discourse.processing.org/u/b3pkay)\
**Post date:** [May 31, 2019, 1:08am UTC](https://discourse.processing.org/t/random-and-if-else-help/11718/4 "2019-05-31T01:08:41Z")

</div>

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.  
I’ve had minimal instruction with Processing and appreciate any advice.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 31, 2019, 8:56am UTC](https://discourse.processing.org/t/random-and-if-else-help/11718/5 "2019-05-31T08:56:51Z")

</div>

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

> **[Collision Detection](http://www.jeffreythompson.org/collision-detection/rect-rect.php)**
>
> An online book about collision detection using Processing.

---

<div class="post-metadata">

**Author:** ![b3pkay](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@b3pkay](https://discourse.processing.org/u/b3pkay)\
**Post date:** [May 31, 2019, 1:22pm UTC](https://discourse.processing.org/t/random-and-if-else-help/11718/6 "2019-05-31T13:22:11Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![b3pkay](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@b3pkay](https://discourse.processing.org/u/b3pkay)\
**Post date:** [May 31, 2019, 1:34pm UTC](https://discourse.processing.org/t/random-and-if-else-help/11718/7 "2019-05-31T13:34:01Z")

</div>

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

```auto
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:

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

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 31, 2019, 7:47pm UTC](https://discourse.processing.org/t/random-and-if-else-help/11718/8 "2019-05-31T19:47:12Z")

</div>

when you say

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

```

shouldn’t x2 be

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

```

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

---

<div class="post-metadata">

**Author:** ![b3pkay](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@b3pkay](https://discourse.processing.org/u/b3pkay)\
**Post date:** [May 31, 2019, 7:58pm UTC](https://discourse.processing.org/t/random-and-if-else-help/11718/9 "2019-05-31T19:58:08Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 31, 2019, 8:02pm UTC](https://discourse.processing.org/t/random-and-if-else-help/11718/10 "2019-05-31T20:02:01Z")

</div>

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:

```auto
//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;
}

```

---

<div class="post-metadata">

**Author:** ![b3pkay](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@b3pkay](https://discourse.processing.org/u/b3pkay)\
**Post date:** [June 1, 2019, 12:22am UTC](https://discourse.processing.org/t/random-and-if-else-help/11718/11 "2019-06-01T00:22:09Z")

</div>

@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.
