title: Intersection of Rectangles for Java (Freeze Tag)
body: <p>**Edited still not getting the right response **
  I am not quite understanding how to figure out the intersection aspect of my project. So far I have determined the top, bottom, left and right but I am not sure where to go from there. </p>
  
  <p>The main driver should call to check if my moving rectangles are     intersecting and if the rectangle is froze the moving one intersecting with it should unfreeze it and change its color. I understand how to unfreeze it and change the color but for whatever the reason it isn't returning the value as true when they are intersecting and I know this code is wrong. Any helpful tips are appreciated.</p>
  
  <pre><code>*CLASS CODE*
  
  import edu.princeton.cs.introcs.StdDraw;
  import java.util.Random;
  import java.awt.Color;
  
  public class MovingRectangle {
  Random rnd = new Random();
  private int xCoord;
  private int yCoord;
  private int width;
  private int height;
  private int xVelocity;
  private int yVelocity;
  private Color color;
  private boolean frozen;
  private int canvas;
  
  public MovingRectangle(int x, int y, int w, int h, int xv, int yv, int canvasSize) {
      canvas = canvasSize;
      xCoord = x;
      yCoord = y;
      width = w;
      height = h;
      xVelocity = xv;
      yVelocity = yv;
      frozen = false;
      int c = rnd.nextInt(5);
  
      if (c == 0) {
          color = StdDraw.MAGENTA;
      }
      if (c == 1) {
          color = StdDraw.BLUE;
      }
      if (c == 2) {
          color = StdDraw.CYAN;
      }
      if (c == 3) {
          color = StdDraw.ORANGE;
      }
      if (c == 4) {
          color = StdDraw.GREEN;
      }
  
  }
  
  public void draw() {
      StdDraw.setPenColor(color);
      StdDraw.filledRectangle(xCoord, yCoord, width, height);
  }
  
  public void move() {
      if (frozen == false) {
          xCoord = xCoord + xVelocity;
          yCoord = yCoord + yVelocity;
      }
      else {
          xCoord +=0;
          yCoord +=0;
      }
      if (xCoord &gt;= canvas || xCoord &lt; 0) {
          xVelocity *= -1;
          this.setRandomColor();
      }
  
      if (yCoord &gt;= canvas || yCoord &lt; 0) {
          yVelocity *= -1;
          this.setRandomColor();
      }
  
  
  }
  
  public void setColor(Color c) {
      StdDraw.setPenColor(color);
  }
  
  public void setRandomColor() {
      int c = rnd.nextInt(5);
  
      if (c == 0) {
          color = StdDraw.MAGENTA;
      }
      if (c == 1) {
          color = StdDraw.BLUE;
      }
      if (c == 2) {
          color = StdDraw.CYAN;
      }
      if (c == 3) {
          color = StdDraw.ORANGE;
      }
      if (c == 4) {
          color = StdDraw.GREEN;
      }
  
  }
  
  public boolean containsPoint(double x, double y) {
      int bottom = yCoord - height / 2;
      int top = yCoord + height / 2;
      int left = xCoord - width / 2;
      int right = xCoord + width / 2;
  
      if (x &gt; left &amp;&amp; x &lt; right &amp;&amp; y &gt; bottom &amp;&amp; y &lt; top) {
          color = StdDraw.RED;
          return true;
      } else {
          return false;
      }
  
  }
  
  public boolean isFrozen() {
      if (frozen) {
          return true;
  
      } else {
          return false;
      }
  
  }
  
  public void setFrozen(boolean val) {
      frozen = val;
  
  
  }
  
  public boolean isIntersecting(MovingRectangle r) {
  int top = xCoord + height/2;
  int bottom = xCoord - height/2;
  int right = yCoord + width/2;
  int left = yCoord - width/2;
  
  int rTop = r.xCoord + r.height/2;
  int rBottom = r.xCoord - r.height/2;
  int rRight = r.yCoord + r.width/2;
  int rLeft = r.yCoord - r.width/2;
  
    if(right &lt;= rRight &amp;&amp; right &gt;= rLeft || bottom &lt;= rBottom &amp;&amp; bottom    
     &gt;= rTop){
  
    return true;
  
      } else {
          return false;
      }
  
      }
  
  }
  </code></pre>
  
  <p>Here is my main driver as well, because I might be doing something wrong here too. </p>
  
  <pre><code> import edu.princeton.cs.introcs.StdDraw;
   import java.util.Random;
  
  public class FreezeTagDriver {
  public static final int CANVAS_SIZE = 800;
  
  public static void main(String[] args) {
      StdDraw.setCanvasSize(CANVAS_SIZE, CANVAS_SIZE);
      StdDraw.setXscale(0, CANVAS_SIZE);
      StdDraw.setYscale(0, CANVAS_SIZE);
  
      Random rnd = new Random();
      MovingRectangle[] recs;
      recs = new MovingRectangle[5];
      boolean frozen = false;
  
      for (int i = 0; i &lt; recs.length; i++) {
          int xv = rnd.nextInt(4);
          int yv = rnd.nextInt(4);
          int x = rnd.nextInt(400);
          int y = rnd.nextInt(400);
          int h = rnd.nextInt(100) + 10;
          int w = rnd.nextInt(100) + 10;
  
          recs[i] = new MovingRectangle(x, y, w, h, xv, yv, CANVAS_SIZE);
      }
      while (true) {
          StdDraw.clear();
  
          for (int i = 0; i &lt; recs.length; i++) {
  
              recs[i].draw();
              recs[i].move();
  
          }
  
          if (StdDraw.mousePressed()) {
  
              for (int i = 0; i &lt; recs.length; i++) {
                  double x = StdDraw.mouseX();
                  double y = StdDraw.mouseY();
  
                  if (recs[i].containsPoint(x, y)) {
                      recs[i].setFrozen(true);
                  }
              }
          }
  
          for (int i = 0; i &lt; recs.length; i++) {
  //for 0
          if(recs[0].isFrozen() &amp;&amp; recs[0].isIntersecting(recs[1])){
              recs[0].setFrozen(false);
                  }
  
          if(recs[0].isFrozen() &amp;&amp; recs[0].isIntersecting(recs[2])){
              recs[0].setFrozen(false);
                  }
  
          if(recs[0].isFrozen() &amp;&amp; recs[0].isIntersecting(recs[3])){
              recs[0].setFrozen(false);
  
                  }
  
   //for 1    
          if(recs[1].isFrozen() &amp;&amp; recs[1].isIntersecting(recs[2])){
              recs[1].setFrozen(false);
  
                  }
          if(recs[1].isFrozen() &amp;&amp; recs[1].isIntersecting(recs[3])){
              recs[1].setFrozen(false);
                  }
  
          if(recs[1].isFrozen() &amp;&amp; recs[1].isIntersecting(recs[4])){
              recs[1].setFrozen(false);
                  }
  
  //for 2
          if(recs[2].isFrozen() &amp;&amp; recs[2].isIntersecting(recs[0])){
              recs[2].setFrozen(false);
  
                  }
          if(recs[2].isFrozen() &amp;&amp; recs[2].isIntersecting(recs[1])){
              recs[2].setFrozen(false);
                  }
  
          if(recs[2].isFrozen() &amp;&amp; recs[2].isIntersecting(recs[3])){
              recs[2].setFrozen(false);
  
                  }
          if(recs[2].isFrozen() &amp;&amp; recs[2].isIntersecting(recs[4])){
              recs[2].setFrozen(false);
  
                  }
  //for 3                 
          if(recs[3].isFrozen() &amp;&amp; recs[3].isIntersecting(recs[0])){
              recs[3].setFrozen(false);
  
                  }
  
          if(recs[3].isFrozen() &amp;&amp; recs[3].isIntersecting(recs[1])){
              recs[3].setFrozen(false);
                  }
  
          if(recs[3].isFrozen() &amp;&amp; recs[3].isIntersecting(recs[2])){
              recs[3].setFrozen(false);
                  }
  
          if(recs[3].isFrozen() &amp;&amp; recs[3].isIntersecting(recs[4])){
              recs[3].setFrozen(false);
                  }
  //for 4         
          if(recs[4].isFrozen() &amp;&amp; recs[4].isIntersecting(recs[0])){
              recs[4].setFrozen(false);
                  }
  
          if(recs[4].isFrozen() &amp;&amp; recs[4].isIntersecting(recs[1])){
              recs[4].setFrozen(false);
                  }
  
          if(recs[4].isFrozen() &amp;&amp; recs[4].isIntersecting(recs[3])){
                  recs[4].setFrozen(false);
                  }
  
          if(recs[4].isFrozen() &amp;&amp; recs[4].isIntersecting(recs[2]))
                  recs[4].setFrozen(false);
                  }
  
  
  
  
  
  
  
  
  
  
          if (recs[0].isFrozen() &amp;&amp; recs[1].isFrozen() &amp;&amp;      
     recs[2].isFrozen() &amp;&amp; recs[3].isFrozen()
                  &amp;&amp; recs[4].isFrozen()) {
              StdDraw.text(400, 400, "YOU WIN");
  
          }
          StdDraw.show(20);
  
      }
  
  }
  </code></pre>
  
  <p>}</p>