daniel_lawson9999

Daniel Lawson Exercises 18-22 Rectangle Class

Sep 26th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. /**
  2.  * Created by danie on 9/25/2016.
  3.  */
  4. //18-22 rectangle class
  5. public class Rectangle {
  6.     private int x;
  7.     private int y;
  8.     private int width;
  9.     private int height;
  10.     public Rectangle(int x, int y,int width,int height){
  11.         this.x = x;
  12.         this.y = y;
  13.         if(width <0 || height <0)throw new IllegalArgumentException("The width and height must be positive");
  14.         this.height = height;
  15.         this.width = width;
  16.     }
  17.     public Rectangle(Point p, int width, int height){
  18.         x = p.getX();
  19.         x = p.getY();
  20.         if(width <0 || height <0)throw new IllegalArgumentException("The width and height must be positive");
  21.         this.height = height;
  22.         this.width = width;
  23.     }
  24.     public int getHeight(){
  25.         return height;
  26.     }
  27.     public int getWidth(){
  28.         return width;
  29.     }
  30.     public int getX(){
  31.         return x;
  32.     }
  33.     public int getY(){
  34.         return y;
  35.     }
  36.     public boolean contains(int x, int y){
  37.         return(x>=this.x && x <= this.x+width && y>=this.y && y <=this.y +height);
  38.     }
  39.     public boolean contains(Point p){
  40.         return(p.getX()>=this.x && p.getX() <= this.x+width && p.getY()>=this.y && p.getY() <=this.y +height);
  41.     }
  42.     // I don't think the formula for the length/width of the union and intersections are correct
  43.     public Rectangle union(Rectangle rect){
  44.         return new Rectangle(Math.min(x,rect.getX()),Math.min(y,rect.getY()),Math.max(x+width,rect.getX()+rect.getWidth())- Math.max(x,rect.getX()),Math.max(y+height,rect.getY()+rect.getHeight())- Math.max(y,rect.getY()));
  45.     }
  46.     public Rectangle intersection(Rectangle rect){
  47.         return new Rectangle(Math.max(y,rect.getY()),Math.max(x,rect.getX()),Math.min(x+width,rect.getX()+rect.getWidth())- Math.min(x,rect.getX()),Math.min(y+height,rect.getY()+rect.getHeight())- Math.min(y,rect.getY()));
  48.     }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment