public abstract class SimplePolygon {
public abstract Vertex2D getVertex(int index) throws IllegalArgumentException;
public abstract int getNumVertices();
public double getArea() {
int area = 0;
for (int i = 0; i < getNumVertices(); i++) {
area += (getVertex(i).getX() * getVertex(i + 1).getY() - getVertex(i + 1).getX() * getVertex(i).getY() );
}
return 0.5 * area;
}
public double getWidth() {
double max = -Double.MAX_VALUE,
min = Double.MAX_VALUE;
for (int i = 0; i < getNumVertices(); i++) {
max = Math.max(max, getVertex(i).getX() );
min = Math.min(min, getVertex(i).getX() );
}
return max - min;
}
public double getHeight() {
double max = -Double.MAX_VALUE,
min = Double.MAX_VALUE;
for (int i = 0; i < getNumVertices(); i++) {
max = Math.max(max, getVertex(i).getY() );
min = Math.min(min, getVertex(i).getY() );
}
return max - min;
}
public double getLength() {
double length = 0;
for (int i = 0; i < getNumVertices(); i++) {
length += getVertex(i).distance(getVertex(i + 1));
}
return length;
}
public String toString() {
StringBuilder result = new StringBuilder();
for (int i = 0; i < getNumVertices(); i++) {
result.append(" "+ getVertex(i).toString());
}
return "Polygon: vertices ="+ result;
}
}
public class ArrayPolygon extends SimplePolygon {
private Vertex2D[] vertices;
public ArrayPolygon(Vertex2D[] vertices) {
if (vertices == null) throw new IllegalArgumentException();
for (int i = 0; i < vertices.length; i++) {
if (vertices[i] == null) throw new NullPointerException();
}
this.vertices = new Vertex2D[vertices.length];
System.arraycopy(vertices, 0, this.vertices, 0, vertices.length);
}
public Vertex2D getVertex(int index) throws IllegalArgumentException {
if (index < 0) throw new IllegalArgumentException();
return vertices[index % vertices.length];
}
public int getNumVertices() {
return vertices.length;
}
public ArrayPolygon invert() {
Vertex2D[] temp = new Vertex2D[vertices.length];
for (int i = vertices.length - 1, y = 0; i >= 0; i--) {
temp[++y] = vertices[i];
}
return new ArrayPolygon(temp);
}
public boolean compare(ArrayPolygon pol) {
int i = 0;
for (; i < vertices.length; i++) {
if (vertices[0].equals(pol.getVertex(i) ) ) break;
}
System.out.println(i +"; "+ vertices.length);
return true;
}
}
public class Vertex2D {
private double x,y;
public Vertex2D(double newX, double newY) {
x = newX;
y = newY;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public String toString() {
return "["+ x +", "+ y +"]";
}
public double distance (Vertex2D vert) {
if(vert == null) {
return -1.0;
}
else {
return Math.sqrt( (vert.x - x) * (vert.x - x)
+ (vert.y - y) * (vert.y - y) );
}
}
}
public class Tester {
public static void main(String[] args) {
Vertex2D[] vert1 = {
new Vertex2D(-100,-100),
new Vertex2D( -40, 10),
new Vertex2D( 50, 20),
new Vertex2D( 10, -20),
new Vertex2D( 60, -40)
};
Vertex2D[] vert2 = {
new Vertex2D( 50, 20),
new Vertex2D( 10, -20),
new Vertex2D( 60, -40),
new Vertex2D(-100,-100),
new Vertex2D( -40, 10)
};
Vertex2D[] vert3 = {
new Vertex2D( -40, 10),
new Vertex2D(-100,-100),
new Vertex2D( 60, -40),
new Vertex2D( 10, -20),
new Vertex2D( 50, 20)
};
Vertex2D[] vert4 = {
new Vertex2D( -40, 10),
new Vertex2D(-100,-100),
new Vertex2D( 50, 20),
new Vertex2D( 10, -20),
new Vertex2D( 60, -40)
};
System.out.println(new ArrayPolygon(vert1).compare(new ArrayPolygon(vert1)));
System.out.println("");
System.out.println(new ArrayPolygon(vert1).compare(new ArrayPolygon(vert2)));
System.out.println("");
System.out.println(new ArrayPolygon(vert1).compare(new ArrayPolygon(vert3)));
System.out.println("");
System.out.println(new ArrayPolygon(vert1).compare(new ArrayPolygon(vert4)));
}
}