Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by danie on 9/25/2016.
- */
- //18-22 rectangle class
- public class Rectangle {
- private int x;
- private int y;
- private int width;
- private int height;
- public Rectangle(int x, int y,int width,int height){
- this.x = x;
- this.y = y;
- if(width <0 || height <0)throw new IllegalArgumentException("The width and height must be positive");
- this.height = height;
- this.width = width;
- }
- public Rectangle(Point p, int width, int height){
- x = p.getX();
- x = p.getY();
- if(width <0 || height <0)throw new IllegalArgumentException("The width and height must be positive");
- this.height = height;
- this.width = width;
- }
- public int getHeight(){
- return height;
- }
- public int getWidth(){
- return width;
- }
- public int getX(){
- return x;
- }
- public int getY(){
- return y;
- }
- public boolean contains(int x, int y){
- return(x>=this.x && x <= this.x+width && y>=this.y && y <=this.y +height);
- }
- public boolean contains(Point p){
- return(p.getX()>=this.x && p.getX() <= this.x+width && p.getY()>=this.y && p.getY() <=this.y +height);
- }
- // I don't think the formula for the length/width of the union and intersections are correct
- public Rectangle union(Rectangle rect){
- 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()));
- }
- public Rectangle intersection(Rectangle rect){
- 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()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment