Path: blob/master/SLICK_HOME/src/org/newdawn/slick/geom/Ellipse.java
1461 views
package org.newdawn.slick.geom;12import java.util.ArrayList;34import org.newdawn.slick.util.FastTrig;56/**7* An ellipse meeting the <code>Shape</code> contract. The ellipse is actually an approximation using8* a series of points generated around the contour of the ellipse.9*10* @author Mark11*/12public class Ellipse extends Shape {13/**14* Default number of segments to draw this ellipse with15*/16protected static final int DEFAULT_SEGMENT_COUNT = 50;1718/**19* The number of segments for graphical representation.20*/21private int segmentCount;22/**23* horizontal radius24*/25private float radius1;26/**27* vertical radius28*/29private float radius2;3031/**32* Creates a new Ellipse object.33*34* @param centerPointX x coordinate of the center of the ellipse35* @param centerPointY y coordinate of the center of the ellipse36* @param radius1 horizontal radius37* @param radius2 vertical radius38*/39public Ellipse(float centerPointX, float centerPointY, float radius1, float radius2) {40this(centerPointX, centerPointY, radius1, radius2, DEFAULT_SEGMENT_COUNT);41}4243/**44* Creates a new Ellipse object.45*46* @param centerPointX x coordinate of the center of the ellipse47* @param centerPointY y coordinate of the center of the ellipse48* @param radius1 horizontal radius49* @param radius2 vertical radius50* @param segmentCount how fine to make the ellipse.51*/52public Ellipse(float centerPointX, float centerPointY, float radius1, float radius2, int segmentCount) {53this.x = centerPointX - radius1;54this.y = centerPointY - radius2;55this.radius1 = radius1;56this.radius2 = radius2;57this.segmentCount = segmentCount;58checkPoints();59}6061/**62* Change the shape of this Ellipse63*64* @param radius1 horizontal radius65* @param radius2 vertical radius66*/67public void setRadii(float radius1, float radius2) {68setRadius1(radius1);69setRadius2(radius2);70}7172/**73* Get the horizontal radius of the ellipse74*75* @return The horizontal radius of the ellipse76*/77public float getRadius1() {78return radius1;79}8081/**82* Set the horizontal radius of the ellipse83*84* @param radius1 The horizontal radius to set85*/86public void setRadius1(float radius1) {87if (radius1 != this.radius1) {88this.radius1 = radius1;89pointsDirty = true;90}91}9293/**94* Get the vertical radius of the ellipse95*96* @return The vertical radius of the ellipse97*/98public float getRadius2() {99return radius2;100}101102/**103* Set the vertical radius of the ellipse104*105* @param radius2 The vertical radius to set106*/107public void setRadius2(float radius2) {108if (radius2 != this.radius2) {109this.radius2 = radius2;110pointsDirty = true;111}112}113114/**115* Generate the points to outline this ellipse.116*117*/118protected void createPoints() {119ArrayList tempPoints = new ArrayList();120121maxX = -Float.MIN_VALUE;122maxY = -Float.MIN_VALUE;123minX = Float.MAX_VALUE;124minY = Float.MAX_VALUE;125126float start = 0;127float end = 359;128129float cx = x + radius1;130float cy = y + radius2;131132int step = 360 / segmentCount;133134for (float a=start;a<=end+step;a+=step) {135float ang = a;136if (ang > end) {137ang = end;138}139float newX = (float) (cx + (FastTrig.cos(Math.toRadians(ang)) * radius1));140float newY = (float) (cy + (FastTrig.sin(Math.toRadians(ang)) * radius2));141142if(newX > maxX) {143maxX = newX;144}145if(newY > maxY) {146maxY = newY;147}148if(newX < minX) {149minX = newX;150}151if(newY < minY) {152minY = newY;153}154155tempPoints.add(new Float(newX));156tempPoints.add(new Float(newY));157}158points = new float[tempPoints.size()];159for(int i=0;i<points.length;i++) {160points[i] = ((Float)tempPoints.get(i)).floatValue();161}162}163164/**165* @see org.newdawn.slick.geom.Shape#transform(org.newdawn.slick.geom.Transform)166*/167public Shape transform(Transform transform) {168checkPoints();169170Polygon resultPolygon = new Polygon();171172float result[] = new float[points.length];173transform.transform(points, 0, result, 0, points.length / 2);174resultPolygon.points = result;175resultPolygon.checkPoints();176177return resultPolygon;178}179180/**181* @see org.newdawn.slick.geom.Shape#findCenter()182*/183protected void findCenter() {184center = new float[2];185center[0] = x + radius1;186center[1] = y + radius2;187}188189/**190* @see org.newdawn.slick.geom.Shape#calculateRadius()191*/192protected void calculateRadius() {193boundingCircleRadius = (radius1 > radius2) ? radius1 : radius2;194}195}196197198