Path: blob/master/SLICK_HOME/src/org/newdawn/slick/geom/Rectangle.java
1461 views
package org.newdawn.slick.geom;12/**3* An axis oriented used for shape bounds4*5* @author Kevin Glass6*/7public class Rectangle extends Shape {8/** The width of the box */9protected float width;10/** The height of the box */11protected float height;1213/**14* Create a new bounding box15*16* @param x The x position of the box17* @param y The y position of the box18* @param width The width of the box19* @param height The hieght of the box20*/21public Rectangle(float x, float y, float width, float height) {22this.x = x;23this.y = y;24this.width = width;25this.height = height;26maxX = x+width;27maxY = y+height;28checkPoints();29}3031/**32* Check if this rectangle contains a point33*34* @param xp The x coordinate of the point to check35* @param yp The y coordinate of the point to check36* @return True if the point is within the rectangle37*/38public boolean contains(float xp, float yp) {39if (xp <= getX()) {40return false;41}42if (yp <= getY()) {43return false;44}45if (xp >= maxX+1) {46return false;47}48if (yp >= maxY+1) {49return false;50}5152return true;53}5455/**56* Set the bounds of this rectangle based on the given rectangle57*58* @param other The other rectangle whose bounds should be applied59*/60public void setBounds(Rectangle other) {61setBounds(other.getX(), other.getY(), other.getWidth(), other.getHeight());62}6364/**65* Set the bounds of this rectangle66*67* @param x The x coordinate of this rectangle68* @param y The y coordinate of this rectangle69* @param width The width to set in this rectangle70* @param height The height to set in this rectangle71*/72public void setBounds(float x, float y, float width, float height) {73setX(x);74setY(y);75setSize(width, height);76}7778/**79* Set the size (widtha and height) of this rectangle80*81* @param width The width to set in this rectangle82* @param height The height to set in this rectangle83*/84public void setSize(float width, float height) {85setWidth(width);86setHeight(height);87}888990/**91* Get the width of the box92*93* @return The width of the box94*/95public float getWidth() {96return width;97}9899/**100* Get the height of the box101*102* @return The height of the box103*/104public float getHeight() {105return height;106}107108/**109* Grow the rectangle at all edges by the given amounts. This will result in the110* rectangle getting larger around it's centre.111*112* @param h The amount to adjust horizontally113* @param v The amount to ajust vertically114*/115public void grow(float h, float v) {116setX(getX() - h);117setY(getY() - v);118setWidth(getWidth() + (h*2));119setHeight(getHeight() + (v*2));120}121122/**123* Grow the rectangle based on scaling it's size124*125* @param h The scale to apply to the horizontal126* @param v The scale to appy to the vertical127*/128public void scaleGrow(float h, float v) {129grow(getWidth() * (h-1), getHeight() * (v-1));130}131132/**133* Set the width of this box134*135* @param width The new width of this box136*/137public void setWidth(float width) {138if (width != this.width) {139pointsDirty = true;140this.width = width;141maxX = x+width;142}143}144145/**146* Set the heightof this box147*148* @param height The height of this box149*/150public void setHeight(float height) {151if (height != this.height) {152pointsDirty = true;153this.height = height;154maxY = y+height;155}156}157158/**159* Check if this box touches another160*161* @param shape The other shape to check against162* @return True if the rectangles touch163*/164public boolean intersects(Shape shape) {165if(shape instanceof Rectangle) {166Rectangle other = (Rectangle)shape;167if ((x > (other.x + other.width)) || ((x + width) < other.x)) {168return false;169}170if ((y > (other.y + other.height)) || ((y + height) < other.y)) {171return false;172}173return true;174}175else if(shape instanceof Circle) {176return intersects((Circle)shape);177}178else {179return super.intersects(shape);180}181}182183protected void createPoints() {184// -1 so that the dimensions include the left and top lines of the rectangle185float useWidth = width - 1;186float useHeight = height - 1;187points = new float[8];188189points[0] = x;190points[1] = y;191192points[2] = x + useWidth;193points[3] = y;194195points[4] = x + useWidth;196points[5] = y + useHeight;197198points[6] = x;199points[7] = y + useHeight;200201maxX = points[2];202maxY = points[5];203minX = points[0];204minY = points[1];205206findCenter();207calculateRadius();208}209210/**211* Check if a circle touches this rectangle212*213* @param other The circle to check against214* @return True if they touch215*/216private boolean intersects(Circle other) {217return other.intersects(this);218}219220/**221* @see java.lang.Object#toString()222*/223public String toString() {224return "[Rectangle "+width+"x"+height+"]";225}226227/**228* Check if a rectangle contains a point (static to use it everywhere)229*230* @param xp231* The x coordinate of the point to check232* @param yp233* The y coordinate of the point to check234* @param xr235* The x coordinate of the rectangle236* @param yr237* The y coordinate of the rectangle238* @param widthr239* The width of the rectangle240* @param heightr The height of the rectangle241* @return True if the point is within the rectangle242*/243public static boolean contains(float xp, float yp, float xr, float yr,244float widthr, float heightr) {245return (xp >= xr) && (yp >= yr) && (xp <= xr + widthr)246&& (yp <= yr + heightr);247}248249/**250* Apply a transformation and return a new shape. This will not alter the current shape but will251* return the transformed shape.252*253* @param transform The transform to be applied254* @return The transformed shape.255*/256public Shape transform(Transform transform) {257checkPoints();258259Polygon resultPolygon = new Polygon();260261float result[] = new float[points.length];262transform.transform(points, 0, result, 0, points.length / 2);263resultPolygon.points = result;264resultPolygon.findCenter();265resultPolygon.checkPoints();266267return resultPolygon;268}269}270271272