Path: blob/master/SLICK_HOME/src/org/newdawn/slick/geom/Polygon.java
1461 views
package org.newdawn.slick.geom;12import java.util.ArrayList;3import java.util.Arrays;45/**6* A polygon implementation meeting the <code>Shape</code> contract.7*8* @author Mark9*/10public class Polygon extends Shape {11/** Allow duplicated points */12private boolean allowDups = false;13/** True if the polygon is closed */14private boolean closed = true;1516/**17* Construct a new polygon with 3 or more points.18* This constructor will take the first set of points and copy them after19* the last set of points to create a closed shape.20*21* @param points An array of points in x, y order.22*/23public Polygon(float points[]) {24int length = points.length;2526this.points = new float[length];27maxX = -Float.MIN_VALUE;28maxY = -Float.MIN_VALUE;29minX = Float.MAX_VALUE;30minY = Float.MAX_VALUE;31x = Float.MAX_VALUE;32y = Float.MAX_VALUE;3334for(int i=0;i<length;i++) {35this.points[i] = points[i];36if(i % 2 == 0) {37if(points[i] > maxX) {38maxX = points[i];39}40if(points[i] < minX) {41minX = points[i];42}43if(points[i] < x) {44x = points[i];45}46}47else {48if(points[i] > maxY) {49maxY = points[i];50}51if(points[i] < minY) {52minY = points[i];53}54if(points[i] < y) {55y = points[i];56}57}58}5960findCenter();61calculateRadius();62pointsDirty = true;63}64/**65* Create an empty polygon66*67*/68public Polygon(){69points = new float[0];70maxX = -Float.MIN_VALUE;71maxY = -Float.MIN_VALUE;72minX = Float.MAX_VALUE;73minY = Float.MAX_VALUE;74}7576/**77* Indicate if duplicate points are allow78*79* @param allowDups True if duplicate points are allowed80*/81public void setAllowDuplicatePoints(boolean allowDups) {82this.allowDups = allowDups;83}8485/**86* Add a point to the polygon87*88* @param x The x coordinate of the point89* @param y The y coordinate of the point90*/91public void addPoint(float x, float y) {92if (hasVertex(x,y) && (!allowDups)) {93return;94}9596ArrayList tempPoints = new ArrayList();97for(int i=0;i<points.length;i++) {98tempPoints.add(new Float(points[i]));99}100tempPoints.add(new Float(x));101tempPoints.add(new Float(y));102int length = tempPoints.size();103points = new float[length];104for(int i=0;i<length;i++) {105points[i] = ((Float)tempPoints.get(i)).floatValue();106}107if(x > maxX) {108maxX = x;109}110if(y > maxY) {111maxY = y;112}113if(x < minX) {114minX = x;115}116if(y < minY) {117minY = y;118}119findCenter();120calculateRadius();121122pointsDirty = true;123}124125126/**127* Apply a transformation and return a new shape. This will not alter the current shape but will128* return the transformed shape.129*130* @param transform The transform to be applied131* @return The transformed shape.132*/133public Shape transform(Transform transform) {134checkPoints();135136Polygon resultPolygon = new Polygon();137138float result[] = new float[points.length];139transform.transform(points, 0, result, 0, points.length / 2);140resultPolygon.points = result;141resultPolygon.findCenter();142resultPolygon.closed = closed;143144return resultPolygon;145}146147/**148* @see org.newdawn.slick.geom.Shape#setX(float)149*/150public void setX(float x) {151super.setX(x);152153pointsDirty = false;154}155156/**157* @see org.newdawn.slick.geom.Shape#setY(float)158*/159public void setY(float y) {160super.setY(y);161162pointsDirty = false;163}164165/**166* @see org.newdawn.slick.geom.Shape#createPoints()167*/168protected void createPoints() {169// This is empty since a polygon must have it's points all the time.170}171172/**173* @see org.newdawn.slick.geom.Shape#closed()174*/175public boolean closed() {176return closed;177}178179/**180* Indicate if the polygon should be closed181*182* @param closed True if the polygon should be closed183*/184public void setClosed(boolean closed) {185this.closed = closed;186}187188/**189* Provide a copy of this polygon190*191* @return A copy of this polygon192*/193public Polygon copy() {194float[] copyPoints = new float[points.length];195System.arraycopy(points, 0, copyPoints, 0, copyPoints.length);196197return new Polygon(copyPoints);198}199}200201202