Path: blob/master/SLICK_HOME/src/org/newdawn/slick/geom/Circle.java
1461 views
package org.newdawn.slick.geom;12/**3* A simple Circle geometry4*5* @author Kevin Glass6*/7public strictfp class Circle extends Ellipse {8/** The radius of the circle */9public float radius;1011/**12* Create a new circle based on its radius13*14* @param centerPointX The x location of the center of the circle15* @param centerPointY The y location of the center of the circle16* @param radius The radius of the circle17*/18public Circle(float centerPointX, float centerPointY, float radius) {19this(centerPointX, centerPointY, radius, DEFAULT_SEGMENT_COUNT);20}2122/**23* Create a new circle based on its radius24*25* @param centerPointX The x location of the center of the circle26* @param centerPointY The y location of the center of the circle27* @param radius The radius of the circle28* @param segmentCount The number of segments to build the circle out of29*/30public Circle(float centerPointX, float centerPointY, float radius, int segmentCount) {31super(centerPointX, centerPointY, radius, radius, segmentCount);32this.x = centerPointX - radius;33this.y = centerPointY - radius;34this.radius = radius;35boundingCircleRadius = radius;36}3738/**39* Get the x coordinate of the centre of the circle40*41* @return The x coordinate of the centre of the circle42*/43public float getCenterX() {44return getX() + radius;45}4647/**48* Get the y coordinate of the centre of the circle49*50* @return The y coordinate of the centre of the circle51*/52public float getCenterY() {53return getY() + radius;54}5556/**57* Set the radius of this circle58*59* @param radius The radius of this circle60*/61public void setRadius(float radius) {62if (radius != this.radius) {63pointsDirty = true;64this.radius = radius;65setRadii(radius, radius);66}67}6869/**70* Get the radius of the circle71*72* @return The radius of the circle73*/74public float getRadius() {75return radius;76}7778/**79* Check if this circle touches another80*81* @param shape The other circle82* @return True if they touch83*/84public boolean intersects(Shape shape) {85if(shape instanceof Circle) {86Circle other = (Circle)shape;87float totalRad2 = getRadius() + other.getRadius();8889if (Math.abs(other.getCenterX() - getCenterX()) > totalRad2) {90return false;91}92if (Math.abs(other.getCenterY() - getCenterY()) > totalRad2) {93return false;94}9596totalRad2 *= totalRad2;9798float dx = Math.abs(other.getCenterX() - getCenterX());99float dy = Math.abs(other.getCenterY() - getCenterY());100101return totalRad2 >= ((dx*dx) + (dy*dy));102}103else if(shape instanceof Rectangle) {104return intersects((Rectangle)shape);105}106else {107return super.intersects(shape);108}109}110111/**112* Check if a point is contained by this circle113*114* @param x The x coordinate of the point to check115* @param y The y coorindate of the point to check116* @return True if the point is contained by this circle117*/118public boolean contains(float x, float y) {119return intersects(new Circle(x,y,0));120}121122/**123* @see org.newdawn.slick.geom.Ellipse#findCenter()124*/125protected void findCenter() {126center = new float[2];127center[0] = x + radius;128center[1] = y + radius;129}130131/**132* @see org.newdawn.slick.geom.Ellipse#calculateRadius()133*/134protected void calculateRadius() {135boundingCircleRadius = radius;136}137138/**139* Check if this circle touches a rectangle140*141* @param other The rectangle to check against142* @return True if they touch143*/144private boolean intersects(Rectangle other) {145Rectangle box = other;146Circle circle = this;147148if (box.contains(x,y)) {149return true;150}151152float x1 = box.getX();153float y1 = box.getY();154float x2 = box.getX() + box.getWidth();155float y2 = box.getY() + box.getHeight();156157Line[] lines = new Line[4];158lines[0] = new Line(x1,y1,x2,y1);159lines[1] = new Line(x2,y1,x2,y2);160lines[2] = new Line(x2,y2,x1,y2);161lines[3] = new Line(x1,y2,x1,y1);162163float r2 = circle.getRadius() * circle.getRadius();164165Vector2f pos = new Vector2f(circle.getCenterX(), circle.getCenterY());166167for (int i=0;i<4;i++) {168float dis = lines[i].distanceSquared(pos);169if (dis < r2) {170return true;171}172}173174return false;175}176177}178179180