From 2ebb39a98373cbb98d49d35f0df881b453e9801b Mon Sep 17 00:00:00 2001 From: Windspar Date: Sun, 25 Feb 2018 00:06:44 -0500 Subject: [PATCH 1/2] new Boundary class. replace RectBoundBox. change int to double --- src/bomberman/animations/Sprite.java | 8 +- src/bomberman/entity/Entity.java | 10 +- src/bomberman/entity/MovingEntity.java | 2 +- .../entity/boundedbox/RectBoundedBox.java | 35 --- src/bomberman/entity/player/Player.java | 63 ++--- .../entity/staticobjects/BlackBomb.java | 48 ++-- src/bomberman/entity/staticobjects/Wall.java | 27 +- src/bomberman/geometry/Boundary.java | 243 ++++++++++++++++++ 8 files changed, 302 insertions(+), 134 deletions(-) delete mode 100644 src/bomberman/entity/boundedbox/RectBoundedBox.java create mode 100644 src/bomberman/geometry/Boundary.java diff --git a/src/bomberman/animations/Sprite.java b/src/bomberman/animations/Sprite.java index b35c2ad..10b3937 100644 --- a/src/bomberman/animations/Sprite.java +++ b/src/bomberman/animations/Sprite.java @@ -24,9 +24,9 @@ public class Sprite public Image[] spriteImages; public boolean hasValidSpriteImages; - + public Entity entityReference; - + public Sprite(Entity e, int actualSize, double playSpeed, int spriteLocationOnSheetX, int spriteLocationOnSheetY, int numberOfFrames, double width, double height, int scale, boolean leftToRight) { @@ -44,11 +44,11 @@ public Sprite(Entity e, int actualSize, double playSpeed, int spriteLocationOnSh } public int getXPosition() { - return entityReference.getPositionX(); + return (int) entityReference.getPositionX(); } public int getYPosition() { - return entityReference.getPositionY(); + return (int) entityReference.getPositionY(); } diff --git a/src/bomberman/entity/Entity.java b/src/bomberman/entity/Entity.java index 64d2b9a..dd62e5d 100644 --- a/src/bomberman/entity/Entity.java +++ b/src/bomberman/entity/Entity.java @@ -5,7 +5,7 @@ */ package bomberman.entity; -import bomberman.entity.boundedbox.RectBoundedBox; +import bomberman.geometry.Boundary; /** * @@ -16,8 +16,8 @@ public interface Entity { boolean isPlayerCollisionFriendly(); void draw(); void removeFromScene(); - int getPositionX(); - int getPositionY(); - RectBoundedBox getBoundingBox(); - + double getPositionX(); + double getPositionY(); + Boundary getBoundary(); + } diff --git a/src/bomberman/entity/MovingEntity.java b/src/bomberman/entity/MovingEntity.java index d7a4966..dcdd9b4 100644 --- a/src/bomberman/entity/MovingEntity.java +++ b/src/bomberman/entity/MovingEntity.java @@ -14,6 +14,6 @@ public interface MovingEntity extends Entity { - public void move(int steps, Direction direction); + public void move(double steps, Direction direction); } diff --git a/src/bomberman/entity/boundedbox/RectBoundedBox.java b/src/bomberman/entity/boundedbox/RectBoundedBox.java deleted file mode 100644 index 4225b04..0000000 --- a/src/bomberman/entity/boundedbox/RectBoundedBox.java +++ /dev/null @@ -1,35 +0,0 @@ -package bomberman.entity.boundedbox; - -import javafx.geometry.Rectangle2D; - -public class RectBoundedBox { - - int x; - int y; - int width; - int height; - Rectangle2D boundary; - - public RectBoundedBox(int x,int y,int w,int h){ - this.x=x; - this.y=y; - width=w; - height=h; - boundary = new Rectangle2D(x, y, width, height); - } - - public Rectangle2D getBoundary() { - return boundary; - } - - public boolean checkCollision(RectBoundedBox b) { - return b.getBoundary().intersects(getBoundary()); - } - - public void setPosition(int x, int y) { - this.x = x; - this.y = y; - boundary = new Rectangle2D(x, y, width, height); - } - -} diff --git a/src/bomberman/entity/player/Player.java b/src/bomberman/entity/player/Player.java index 7cfcf13..28c0127 100644 --- a/src/bomberman/entity/player/Player.java +++ b/src/bomberman/entity/player/Player.java @@ -9,23 +9,19 @@ import bomberman.entity.Entity; import bomberman.entity.KillableEntity; import bomberman.entity.MovingEntity; -import bomberman.entity.boundedbox.RectBoundedBox; +import bomberman.geometry.Boundary; import bomberman.scenes.Sandbox; public class Player implements MovingEntity, KillableEntity { private int health; private boolean isAlive; - RectBoundedBox playerBoundary; + Boundary boundary; Sprite currentSprite; PlayerAnimations playerAnimations; Direction currentDirection; - - public int positionX = 0; - public int positionY = 0; - String name; public Player() { @@ -40,14 +36,8 @@ public Player(int posX, int posY) { private void init(int x, int y) { name = "Player"; - playerAnimations = new PlayerAnimations(this); - - positionX = x; - positionY = y; - - playerBoundary = new RectBoundedBox(positionX, positionY, GlobalConstants.PLAYER_WIDTH, GlobalConstants.PLAYER_HEIGHT); - + boundary = new Boundary(x, y, GlobalConstants.PLAYER_WIDTH, GlobalConstants.PLAYER_HEIGHT); currentSprite = playerAnimations.getPlayerIdleSprite(); } @@ -77,9 +67,7 @@ public String toString() { @Override public boolean isColliding(Entity b) { - // playerBoundary.setPosition(positionX, positionY); - RectBoundedBox otherEntityBoundary = (RectBoundedBox) b.getBoundingBox(); - return playerBoundary.checkCollision(otherEntityBoundary); + return boundary.intersects(b.getBoundary()); } @Override @@ -94,26 +82,22 @@ public void die() { setCurrentSprite(playerAnimations.getPlayerDying()); } - private boolean checkCollisions(int x, int y) { - playerBoundary.setPosition(x, y); + private boolean checkCollisions(double x, double y) { + Boundary collision = boundary.getMin(x, y); for (Entity e : Sandbox.getEntities()) { - if (e != this && isColliding(e) && !e.isPlayerCollisionFriendly()) { - playerBoundary.setPosition(positionX, positionY); - /* - System.out.println("Player x="+getPositionX()+" y=" - +getPositionY()+" colliding with x="+e.getPositionX() - +" y="+e.getPositionY()); - */ + if (e != this && !e.isPlayerCollisionFriendly() && + collision.intersects(e.getBoundary())) { return true; } } - playerBoundary.setPosition(positionX, positionY); + + boundary.setMin(x, y); return false; } @Override - public void move(int steps, Direction direction) { + public void move(double steps, Direction direction) { steps *= GameLoop.getDeltaTime(); @@ -123,29 +107,25 @@ public void move(int steps, Direction direction) { } else { switch (direction) { case UP: - if(!checkCollisions(positionX, positionY - steps)) { - positionY -= steps; + if(!checkCollisions(boundary.getMinX(), boundary.getMinY() - steps)) { setCurrentSprite(playerAnimations.getMoveUpSprite()); currentDirection = Direction.UP; } break; case DOWN: - if(!checkCollisions(positionX, positionY + steps)) { - positionY += steps; + if(!checkCollisions(boundary.getMinX(), boundary.getMinY() + steps)) { setCurrentSprite(playerAnimations.getMoveDownSprite()); currentDirection = Direction.DOWN; } break; case LEFT: - if(!checkCollisions(positionX - steps, positionY)) { - positionX -= steps; + if(!checkCollisions(boundary.getMinX() - steps, boundary.getMinY())) { setCurrentSprite(playerAnimations.getMoveLeftSprite()); currentDirection = Direction.LEFT; } break; case RIGHT: - if(!checkCollisions(positionX + steps, positionY)) { - positionX += steps; + if(!checkCollisions(boundary.getMinX() + steps, boundary.getMinY())) { setCurrentSprite(playerAnimations.getMoveRightSprite()); currentDirection = Direction.RIGHT; } @@ -171,19 +151,18 @@ public void removeFromScene() { } @Override - public int getPositionX() { - return positionX; + public double getPositionX() { + return boundary.getMinX(); } @Override - public int getPositionY() { - return positionY; + public double getPositionY() { + return boundary.getMinY(); } @Override - public RectBoundedBox getBoundingBox() { - playerBoundary.setPosition(positionX, positionY); - return playerBoundary; + public Boundary getBoundary() { + return boundary; } @Override diff --git a/src/bomberman/entity/staticobjects/BlackBomb.java b/src/bomberman/entity/staticobjects/BlackBomb.java index 92dcde3..9556272 100644 --- a/src/bomberman/entity/staticobjects/BlackBomb.java +++ b/src/bomberman/entity/staticobjects/BlackBomb.java @@ -10,7 +10,7 @@ import bomberman.animations.Sprite; import bomberman.entity.Entity; import bomberman.entity.StaticEntity; -import bomberman.entity.boundedbox.RectBoundedBox; +import bomberman.geometry.Boundary; import java.util.Date; /** @@ -18,17 +18,13 @@ * @author Ashish */ public class BlackBomb implements StaticEntity { - public int positionX = 0; - public int positionY = 0; - private int height; - private int width; private Sprite sprite; - RectBoundedBox entityBoundary; + Boundary entityBoundary; BombAnimations bomb_animations; Date addedDate; - int timerDurationInMillis = 2000; + int timerDurationInMillis = 2000; STATE bombState; - + enum STATE { INACTIVE, //INACTIVE when bomb's timer hasnt yet started @@ -36,19 +32,15 @@ enum STATE EXPLODING, //when bomb is exploding DEAD; //when the bomb has already exploded } - - public BlackBomb(int x, int y) { - positionX = x; - positionY = y; - width = 16; - height = 16; - bomb_animations=new BombAnimations(this); - sprite=bomb_animations.getBlackBomb(); - entityBoundary = new RectBoundedBox(positionX, positionY, width, height); - addedDate=new Date(); - bombState=STATE.ACTIVE; + + public BlackBomb(double x, double y) { + bomb_animations = new BombAnimations(this); + sprite = bomb_animations.getBlackBomb(); + entityBoundary = new Boundary(x, y, 16, 16); + addedDate = new Date(); + bombState = STATE.ACTIVE; } - + public boolean isAlive(){ STATE s = checkBombState(); if(s==STATE.DEAD){ @@ -61,7 +53,7 @@ public boolean isAlive(){ return true; } } - + public STATE checkBombState(){ if(new Date().getTime()>timerDurationInMillis+addedDate.getTime()){ return STATE.DEAD; @@ -69,7 +61,7 @@ public STATE checkBombState(){ return STATE.ACTIVE; } } - + @Override public boolean isColliding(Entity b) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. @@ -86,17 +78,17 @@ public void removeFromScene() { } @Override - public int getPositionX() { - return positionX; + public double getPositionX() { + return entityBoundary.getMinX(); } @Override - public int getPositionY() { - return positionY; + public double getPositionY() { + return entityBoundary.getMinY(); } @Override - public RectBoundedBox getBoundingBox() { + public Boundary getBoundary() { return entityBoundary; } @@ -104,5 +96,5 @@ public RectBoundedBox getBoundingBox() { public boolean isPlayerCollisionFriendly() { return true; } - + } diff --git a/src/bomberman/entity/staticobjects/Wall.java b/src/bomberman/entity/staticobjects/Wall.java index 9608896..367897c 100644 --- a/src/bomberman/entity/staticobjects/Wall.java +++ b/src/bomberman/entity/staticobjects/Wall.java @@ -9,7 +9,7 @@ import bomberman.animations.Sprite; import bomberman.entity.Entity; import bomberman.entity.StaticEntity; -import bomberman.entity.boundedbox.RectBoundedBox; +import bomberman.geometry.Boundary; import javafx.scene.paint.Color; /** @@ -17,24 +17,13 @@ * @author kdost */ public class Wall implements StaticEntity { - public int positionX = 0; - public int positionY = 0; - private int height; - private int width; private Color wallColor; private Sprite sprite; - RectBoundedBox entityBoundary; - + Boundary entityBoundary; public Wall (int x, int y) { - positionX = x; - positionY = y; - - width = 16; - height = 16; - sprite = new Sprite(this, 16, 0, 348, 123, 1, 16, 16, 2, false); - entityBoundary = new RectBoundedBox(positionX, positionY, width, height); + entityBoundary = new Boundary(x, y, 16, 16); } public void changeColor(Color color) { @@ -57,17 +46,17 @@ public void removeFromScene() { } @Override - public int getPositionX() { - return positionX; + public double getPositionX() { + return entityBoundary.getMinX(); } @Override - public int getPositionY() { - return positionY; + public double getPositionY() { + return entityBoundary.getMinY(); } @Override - public RectBoundedBox getBoundingBox() + public Boundary getBoundary() { return entityBoundary; } diff --git a/src/bomberman/geometry/Boundary.java b/src/bomberman/geometry/Boundary.java new file mode 100644 index 0000000..c926f4c --- /dev/null +++ b/src/bomberman/geometry/Boundary.java @@ -0,0 +1,243 @@ +package bomberman.geometry; +import javafx.geometry.Point2D; +import javafx.geometry.Dimension2D; +import javafx.geometry.Rectangle2D; + +public class Boundary { + private double minX; + private double minY; + private double width; + private double height; + + public Boundary() { + minX = 0; + minY = 0; + width = 0; + height = 0; + } + + public Boundary(double minX, double minY, double width, double height) { + this.minX = minX; + this.minY = minY; + this.width = width; + this.height = height; + } + + public Boundary(Rectangle2D r) { + minX = r.getMinX(); + minY = r.getMinY(); + width = r.getWidth(); + height = r.getHeight(); + } + + public boolean contains(double x, double y) { + return getRectangle2D().contains(x, y); + } + + public boolean contains(Point2D point) { + return getRectangle2D().contains(point); + } + + public boolean contains(double x, double y, double w, double h) { + return getRectangle2D().contains(x, y, w, h); + } + + public boolean contains(Boundary r) { + return getRectangle2D().contains(r.getRectangle2D()); + } + + public boolean contains(Rectangle2D r) { + return getRectangle2D().contains(r); + } + + public Boundary copy() { + return new Boundary(minX, minY, width, height); + } + + public boolean equals(Object obj) { + return obj == this; + } + + public Point2D getCenter() { + return new Point2D(minX + width / 2, minY + height / 2); + } + + public double getCenterX() { + return minX + width / 2; + } + + public double getCenterY() { + return minY + height / 2; + } + + // MAYBE getMinMax + public Point2D[] getCorners() { + return new Point2D[] { + new Point2D(minX, minY), + new Point2D(minX + width, minY), + new Point2D(minX, minY + height), + new Point2D(minX + width, minY + height) + }; + } + + public double getHeight() { + return height; + } + + public Point2D getMin() { + return new Point2D(minX, minY); + } + + public Boundary getMin(double x, double y) { + return new Boundary(x, y, width, height); + } + + public double getWidth() { + return width; + } + + public double getMinX() { + return minX; + } + + public double getMinY() { + return minY; + } + + public double getMaxX() { + return minX + width; + } + + public double getMaxY() { + return minY + height; + } + + public Rectangle2D getRectangle2D() { + return new Rectangle2D(minX, minY, width, height); + } + + public Dimension2D getSize() { + return new Dimension2D(width, height); + } + + public int hashCode() { + return getRectangle2D().hashCode(); + } + + public boolean intersects(double x, double y, double w, double h) { + return getRectangle2D().intersects(x, y, w, h); + } + + public boolean intersects(Boundary r) { + return getRectangle2D().intersects(r.getRectangle2D()); + } + + public boolean intersects(Rectangle2D r) { + return getRectangle2D().intersects(r); + } + + public Boundary inflate(double x, double y) { + Boundary boundary = copy(); + if(x != 0) { + boundary.minX -= (x - (x % 2)) / 2; + boundary.width += x; + } + + if(y != 0) { + boundary.minY -= (y - (y % 2)) / 2; + boundary.height += y; + } + + return boundary; + } + + public Boundary move(double x, double y) { + return new Boundary(minX + x, minY + y, width, height); + } + + public void setCenter(double centerX, double centerY) { + minX = centerX - width / 2; + minY = centerY - height / 2; + } + + public void setCenter(Point2D point) { + minX = point.getX() - width / 2; + minY = point.getY() - height / 2; + } + + public void setCenterX(double centerX) { + minX = centerX - width / 2; + } + + public void setCenterY(double centerY) { + minY = centerY - height / 2; + } + + public void setMin(double minX, double minY) { + this.minX = minX; + this.minY = minY; + } + + public void setMin(Point2D point) { + minX = point.getX(); + minY = point.getY(); + } + + public void setMinX(double minX) { + this.minX = minX; + } + + public void setMinY(double minY) { + this.minY = minY; + } + + public void setMax(double maxX, double maxY) { + minX = maxX - width; + minY = maxY - height; + } + + public void setMax(Point2D point) { + minX = point.getX() - width; + minY = point.getY() - height; + } + + public void setMaxX(double maxX) { + minX = maxX - width; + } + + public void setMaxY(double maxY) { + minX = maxY - height; + } + + public void setSize(double width, double height) { + this.width = width; + this.height = height; + } + + public void setSize(Dimension2D point) { + width = point.getWidth(); + height = point.getHeight(); + } + + public void translate(double x, double y) { + minX += x; + minY += y; + } + + public void translate(Point2D point) { + minX += point.getX(); + minY += point.getY(); + } + + public void translateX(double x) { + minX += x; + } + + public void translateY(double y) { + minY += y; + } + + public String toString() { + return "Boundary(" + minX + ", " + minY + ", " + width + ", " + height + ")"; + } +} From 0348e4597c49e0630db98cd186f63986b55d0bf6 Mon Sep 17 00:00:00 2001 From: Windspar Date: Sun, 25 Feb 2018 11:42:45 -0500 Subject: [PATCH 2/2] Boundary extends Rectangle2D --- src/bomberman/entity/player/Player.java | 24 +-- src/bomberman/geometry/Boundary.java | 222 +++++++----------------- 2 files changed, 71 insertions(+), 175 deletions(-) diff --git a/src/bomberman/entity/player/Player.java b/src/bomberman/entity/player/Player.java index 28c0127..39e094c 100644 --- a/src/bomberman/entity/player/Player.java +++ b/src/bomberman/entity/player/Player.java @@ -16,7 +16,7 @@ public class Player implements MovingEntity, KillableEntity { private int health; private boolean isAlive; - Boundary boundary; + Boundary playerBoundary; Sprite currentSprite; PlayerAnimations playerAnimations; @@ -37,7 +37,7 @@ public Player(int posX, int posY) { private void init(int x, int y) { name = "Player"; playerAnimations = new PlayerAnimations(this); - boundary = new Boundary(x, y, GlobalConstants.PLAYER_WIDTH, GlobalConstants.PLAYER_HEIGHT); + playerBoundary = new Boundary(x, y, GlobalConstants.PLAYER_WIDTH, GlobalConstants.PLAYER_HEIGHT); currentSprite = playerAnimations.getPlayerIdleSprite(); } @@ -67,7 +67,7 @@ public String toString() { @Override public boolean isColliding(Entity b) { - return boundary.intersects(b.getBoundary()); + return playerBoundary.intersects(b.getBoundary()); } @Override @@ -83,7 +83,7 @@ public void die() { } private boolean checkCollisions(double x, double y) { - Boundary collision = boundary.getMin(x, y); + Boundary collision = playerBoundary.setMin(x, y); for (Entity e : Sandbox.getEntities()) { if (e != this && !e.isPlayerCollisionFriendly() && @@ -92,7 +92,7 @@ private boolean checkCollisions(double x, double y) { } } - boundary.setMin(x, y); + playerBoundary = collision; return false; } @@ -107,25 +107,25 @@ public void move(double steps, Direction direction) { } else { switch (direction) { case UP: - if(!checkCollisions(boundary.getMinX(), boundary.getMinY() - steps)) { + if(!checkCollisions(playerBoundary.getMinX(), playerBoundary.getMinY() - steps)) { setCurrentSprite(playerAnimations.getMoveUpSprite()); currentDirection = Direction.UP; } break; case DOWN: - if(!checkCollisions(boundary.getMinX(), boundary.getMinY() + steps)) { + if(!checkCollisions(playerBoundary.getMinX(), playerBoundary.getMinY() + steps)) { setCurrentSprite(playerAnimations.getMoveDownSprite()); currentDirection = Direction.DOWN; } break; case LEFT: - if(!checkCollisions(boundary.getMinX() - steps, boundary.getMinY())) { + if(!checkCollisions(playerBoundary.getMinX() - steps, playerBoundary.getMinY())) { setCurrentSprite(playerAnimations.getMoveLeftSprite()); currentDirection = Direction.LEFT; } break; case RIGHT: - if(!checkCollisions(boundary.getMinX() + steps, boundary.getMinY())) { + if(!checkCollisions(playerBoundary.getMinX() + steps, playerBoundary.getMinY())) { setCurrentSprite(playerAnimations.getMoveRightSprite()); currentDirection = Direction.RIGHT; } @@ -152,17 +152,17 @@ public void removeFromScene() { @Override public double getPositionX() { - return boundary.getMinX(); + return playerBoundary.getMinX(); } @Override public double getPositionY() { - return boundary.getMinY(); + return playerBoundary.getMinY(); } @Override public Boundary getBoundary() { - return boundary; + return playerBoundary; } @Override diff --git a/src/bomberman/geometry/Boundary.java b/src/bomberman/geometry/Boundary.java index c926f4c..2d13bfe 100644 --- a/src/bomberman/geometry/Boundary.java +++ b/src/bomberman/geometry/Boundary.java @@ -3,241 +3,137 @@ import javafx.geometry.Dimension2D; import javafx.geometry.Rectangle2D; -public class Boundary { - private double minX; - private double minY; - private double width; - private double height; - +public class Boundary extends Rectangle2D { public Boundary() { - minX = 0; - minY = 0; - width = 0; - height = 0; + super(0,0,0,0); } public Boundary(double minX, double minY, double width, double height) { - this.minX = minX; - this.minY = minY; - this.width = width; - this.height = height; - } - - public Boundary(Rectangle2D r) { - minX = r.getMinX(); - minY = r.getMinY(); - width = r.getWidth(); - height = r.getHeight(); - } - - public boolean contains(double x, double y) { - return getRectangle2D().contains(x, y); - } - - public boolean contains(Point2D point) { - return getRectangle2D().contains(point); - } - - public boolean contains(double x, double y, double w, double h) { - return getRectangle2D().contains(x, y, w, h); - } - - public boolean contains(Boundary r) { - return getRectangle2D().contains(r.getRectangle2D()); - } - - public boolean contains(Rectangle2D r) { - return getRectangle2D().contains(r); + super(minX, minY, width, height); } public Boundary copy() { - return new Boundary(minX, minY, width, height); - } - - public boolean equals(Object obj) { - return obj == this; + return new Boundary(getMinX(), getMinY(), getWidth(), getHeight()); } public Point2D getCenter() { - return new Point2D(minX + width / 2, minY + height / 2); + return new Point2D(getMinX() + getWidth() / 2, getMinY() + getHeight() / 2); } public double getCenterX() { - return minX + width / 2; + return getMinX() + getWidth() / 2; } public double getCenterY() { - return minY + height / 2; + return getMinY() + getHeight() / 2; } // MAYBE getMinMax public Point2D[] getCorners() { return new Point2D[] { - new Point2D(minX, minY), - new Point2D(minX + width, minY), - new Point2D(minX, minY + height), - new Point2D(minX + width, minY + height) + new Point2D(getMinX(), getMinY()), + new Point2D(getMaxX(), getMinY()), + new Point2D(getMinX(), getMaxY()), + new Point2D(getMaxX(), getMaxY()) }; } - public double getHeight() { - return height; - } - public Point2D getMin() { - return new Point2D(minX, minY); - } - - public Boundary getMin(double x, double y) { - return new Boundary(x, y, width, height); - } - - public double getWidth() { - return width; - } - - public double getMinX() { - return minX; - } - - public double getMinY() { - return minY; - } - - public double getMaxX() { - return minX + width; - } - - public double getMaxY() { - return minY + height; - } - - public Rectangle2D getRectangle2D() { - return new Rectangle2D(minX, minY, width, height); + return new Point2D(getMinX(), getMinY()); } public Dimension2D getSize() { - return new Dimension2D(width, height); - } - - public int hashCode() { - return getRectangle2D().hashCode(); - } - - public boolean intersects(double x, double y, double w, double h) { - return getRectangle2D().intersects(x, y, w, h); - } - - public boolean intersects(Boundary r) { - return getRectangle2D().intersects(r.getRectangle2D()); - } - - public boolean intersects(Rectangle2D r) { - return getRectangle2D().intersects(r); + return new Dimension2D(getWidth(), getHeight()); } public Boundary inflate(double x, double y) { - Boundary boundary = copy(); + double mx = getMinX(); + double my = getMinY(); + double width = getWidth(); + double height = getHeight(); + if(x != 0) { - boundary.minX -= (x - (x % 2)) / 2; - boundary.width += x; + mx -= (x - (x % 2)) / 2; + width += x; } if(y != 0) { - boundary.minY -= (y - (y % 2)) / 2; - boundary.height += y; + my -= (y - (y % 2)) / 2; + height += y; } - return boundary; - } - - public Boundary move(double x, double y) { - return new Boundary(minX + x, minY + y, width, height); - } - - public void setCenter(double centerX, double centerY) { - minX = centerX - width / 2; - minY = centerY - height / 2; + return new Boundary(mx, my, width, height); } - public void setCenter(Point2D point) { - minX = point.getX() - width / 2; - minY = point.getY() - height / 2; + public Boundary setCenter(double centerX, double centerY) { + return setMin(centerX - getWidth() / 2, centerY - getHeight() / 2); } - public void setCenterX(double centerX) { - minX = centerX - width / 2; + public Boundary setCenter(Point2D point) { + return setMin(point.getX() - getWidth() / 2, point.getY() - getHeight() / 2); } - public void setCenterY(double centerY) { - minY = centerY - height / 2; + public Boundary setCenterX(double centerX) { + return setMin(centerX - getWidth() / 2, getMinY()); } - public void setMin(double minX, double minY) { - this.minX = minX; - this.minY = minY; + public Boundary setCenterY(double centerY) { + return setMin(getMinX(), centerY - getHeight() / 2); } - public void setMin(Point2D point) { - minX = point.getX(); - minY = point.getY(); + public Boundary setMin(double minX, double minY) { + return new Boundary(minX, minY, getWidth(), getHeight()); } - public void setMinX(double minX) { - this.minX = minX; + public Boundary setMin(Point2D point) { + return new Boundary(point.getX(), point.getY(), getWidth(), getHeight()); } - public void setMinY(double minY) { - this.minY = minY; + public Boundary setMinX(double minX) { + return new Boundary(minX, getMinY(), getWidth(), getHeight()); } - public void setMax(double maxX, double maxY) { - minX = maxX - width; - minY = maxY - height; + public Boundary setMinY(double minY) { + return new Boundary(getMinX(), minY, getWidth(), getHeight()); } - public void setMax(Point2D point) { - minX = point.getX() - width; - minY = point.getY() - height; + public Boundary setMax(double maxX, double maxY) { + return new Boundary(maxX - getWidth(), maxY - getHeight(), getWidth(), getHeight()); } - public void setMaxX(double maxX) { - minX = maxX - width; + public Boundary setMax(Point2D point) { + return new Boundary(point.getX() - getWidth(), point.getY() - getHeight(), getWidth(), getHeight()); } - public void setMaxY(double maxY) { - minX = maxY - height; + public Boundary setMaxX(double maxX) { + return new Boundary(maxX - getWidth(), getMinY(), getWidth(), getHeight()); } - public void setSize(double width, double height) { - this.width = width; - this.height = height; + public Boundary setMaxY(double maxY) { + return new Boundary(getMinX(), maxY - getHeight(), getWidth(), getHeight()); } - public void setSize(Dimension2D point) { - width = point.getWidth(); - height = point.getHeight(); + public Boundary setSize(double width, double height) { + return new Boundary(getMinX(), getMinY(), width, height); } - public void translate(double x, double y) { - minX += x; - minY += y; + public Boundary setSize(Dimension2D point) { + return new Boundary(getMinX(), getMinY(), point.getWidth(), point.getHeight()); } - public void translate(Point2D point) { - minX += point.getX(); - minY += point.getY(); + public Boundary translate(double x, double y) { + return setMin(getMinX() + x, getMinY() + y); } - public void translateX(double x) { - minX += x; + public Boundary translate(Point2D p) { + return setMin(getMinX() + p.getX(), getMinY() + p.getY()); } - public void translateY(double y) { - minY += y; + public Boundary translateX(double x) { + return setMinX(getMinX() + x); } - public String toString() { - return "Boundary(" + minX + ", " + minY + ", " + width + ", " + height + ")"; + public Boundary translateY(double y) { + return setMinY(getMinY() + y); } }