isefire

Rectangle

Jul 27th, 2014
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. /* Rectangle
  2.  * <Description>
  3.  * Creates a Rectangle object
  4.  * <Usage>
  5.  * javac Rectangle.java StadDraw.java
  6.  *
  7.  * API:
  8.  * class Rectangle
  9.  * public Rectangle(double x0, double y0, double w, double h)
  10.  * public double area()
  11.  * public double perimeter()
  12.  * public boolean intersects(Rectangle b)
  13.  * public void show(Rectangle b)
  14.  *
  15.  * <References>
  16.  * http://introcs.cs.princeton.edu/java/stdlib/
  17.  * UAB CIS201L-2014 Summer Dr. Sloan
  18.  * @author
  19.  * Dan Latham
  20.  * @version 0.0.1
  21.  *
  22.  */
  23.  
  24. public class Rectangle
  25. {
  26.     private final double x;
  27.     private final double y;
  28.     private final double width;
  29.     private final double height;
  30.  
  31.     public Rectangle(double x0, double y0, double w, double h)
  32.     {
  33.         x = x0;
  34.         y = y0;
  35.         width = w;
  36.         height = h;
  37.     }
  38.     public double area()
  39.     {
  40.         return (double) width * height;
  41.     }
  42.     public double perimeter()
  43.     {
  44.         return (double) 2*width + 2*height;
  45.     }
  46.     public boolean intersects(Rectangle b)
  47.     {
  48.         //check if x and y points are the same
  49.         if (x == b.x && y == b.y)
  50.         {
  51.             if (width == b.width || height == b.height) return true;
  52.             else if (width > b.width && height < b.height) return true;
  53.             else if (width < b.width && height > b.height) return true;
  54.         }
  55.         //check if rightside of rect.a.x goes past rect.b.x or at rect.b.x
  56.         else if (x <= b.x) if (x+(width/2) >= b.x-(width/2)) return true;
  57.         //opposite
  58.         else if (x > b.x) if (b.x+(width/2) >= x-(width/2)) return true;
  59.         //check if top of a.y goes past b.y
  60.         else if (y <= b.y) if (y+(height/2) >= b.y-(height/2)) return true;
  61.         //opposite
  62.         else if (y > b.y) if (b.y+(height/2) >= y-(height/2)) return true;
  63.         return false; //base
  64.     }
  65.     public void show(Rectangle b)
  66.     {
  67.         StdDraw.setPenRadius();
  68.         StdDraw.setPenColor(StdDraw.BOOK_BLUE);
  69.         StdDraw.rectangle(b.x,b.y,b.width/2,b.height/2);
  70.         StdDraw.show(1);
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment