Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Create your own rectangle
- * @author sedran
- *
- */
- public class Rectangle {
- private int x, y, width, height;
- /**
- * Construct a new rectangle
- * @param x = the x value of the top left corner of the rectangle
- * @param y = the y value of the top left corner of the rectangle
- * @param width = width of the rectangle
- * @param height = height of the rectangle
- */
- public Rectangle(int x, int y, int width, int height) {
- this.x = x;
- this.y = y;
- this.width = width;
- this.height = height;
- }
- /**
- * Get the x value of the top left corner
- * @return the x value of the top left corner
- */
- public int getX() {
- return x;
- }
- /**
- * Get the y value of the top left corner
- * @return the y value of the top left corner
- */
- public int getY() {
- return y;
- }
- /**
- * Get width of the rectangle
- * @return width of rectangle
- */
- public int getWidth() {
- return width;
- }
- /**
- * Get height of the rectangle
- * @return height of rectangle
- */
- public int getHeight() {
- return height;
- }
- public String toString() {
- return "Rectangle[x=" + x + ",y=" + y + ",width="+ width +", height="+ height +"]";
- }
- public boolean contains(int x, int y) {
- if(x >= this.x && x <= this.x+this.width && y <= this.y && y >= this.y-height)
- return true;
- return false;
- }
- public Rectangle intersect(Rectangle rect) {
- boolean first = false;
- int x1=0, x2=0, y1=0, y2=0;
- for(int i=this.x; i<=(this.x+this.width); i++) {
- for(int k=this.y; k>=(this.y-this.height); k--) {
- if(rect.contains(i, k)) {
- if( !first ) {
- first = true;
- x1 = i;
- y1 = k;
- } else {
- x2 = i;
- y2 = k;
- }
- }
- }
- }
- return new Rectangle(x1,y1,x2-x1,y1-y2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement