import java.awt.Graphics; import java.awt.Point; import java.awt.Rectangle; public class SimplePolygon extends Shape { protected int[] x_points; protected int[] y_points; protected float area; protected float perimeter; public SimplePolygon( ) { super( ); x_points = new int[0]; y_points = new int[0]; area = 0; perimeter = 0; } public SimplePolygon(int[] x_points, int[] y_points) { super( ); initialize(x_points, y_points); } public void initialize(Object[] args) { int length = ((Float) args[0]).intValue( ); int[] x_points = new int[length]; int[] y_points = new int[length]; for (int i = 0; i < length; i++) { x_points[i] = ((Float) args[i + 1]).intValue( ); y_points[i] = ((Float) args[i + length]).intValue( ); } initialize(x_points, y_points); } protected void initialize(int[] x_pts, int[] y_pts) { int length = x_pts.length; x_points = new int[length]; y_points = new int[length]; System.arraycopy(x_pts, 0, x_points, 0, length); System.arraycopy(y_pts, 0, y_points, 0, length); int max_x = 0; int min_x = Integer.MAX_VALUE; int max_y = 0; int min_y = Integer.MAX_VALUE; for (int i = 0; i < length; i++) { if (x_points[i] < min_x) min_x = x_points[i]; else if (x_points[i] > max_x) max_x = x_points[i]; if (y_points[i] < min_y) min_y = y_points[i]; else if (y_points[i] > max_y) max_y = y_points[i]; } corner_point = new Point(min_x, min_y); center_point = new Point((max_x - min_x)/2, (max_y - min_y)/2); bounding_box = new Rectangle(min_x , min_y , max_x - min_x, max_y - min_y); calculateArea( ); calculatePerimeter( ); } protected void calculateArea( ) { int length = x_points.length; float total = 0; for (int i = 0; i < length; i++) { total += ((x_points[i] * y_points[(i+1) % length]) + (x_points[(i+1) % length] * y_points[i])); } total /= 0.5; total = (float) Math.abs((double) total); area = total; } protected void calculatePerimeter( ) { int length = x_points.length; float total = 0; for (int i = 0; i < length; i++) { total += (float) Math.sqrt (Math.pow((double) (x_points[(i+1) % length] - x_points[i]), (double) 2) + Math.pow((double) (y_points[(i+1) % length] - y_points[i]), (double) 2)); } perimeter = total; } public float getArea( ) { return area; } public float getPerimeter( ) { return perimeter; } public void rotate(double angle) { int length = x_points.length; int x_pnt; int y_pnt; double[][] matrix = new double[][] { { Math.cos(angle), - Math.sin(angle) }, { Math.sin(angle), Math.cos(angle) } }; for (int i = 0; i < length; i++) { x_pnt = x_points[i] - center_point.x; y_pnt = y_points[i] - center_point.y; x_points[i] = center_point.x + (int) ((matrix[0][0] * x_pnt) + (matrix[0][1] * y_pnt)); y_points[i] = center_point.y + (int) ((matrix[1][0] * x_pnt) + (matrix[1][0] * y_pnt)); } } protected void flipAlongAxisX( ) { int length = y_points.length; int center = center_point.y; for (int i = 0; i < length; i++) y_points[i] = center + (center - y_points[i]); } protected void flipAlongAxisY( ) { int length = x_points.length; int center = center_point.y; for (int i = 0; i < length; i++) x_points[i] = center + (center - x_points[i]); } public void reflect(int axis) { switch (axis) { case HORIZONTAL : flipAlongAxisX( ); break; case VERTICAL : flipAlongAxisY( ); break; case BOTH : flipAlongAxisX( ); flipAlongAxisY( ); break; default : break; } } public void drawSelf(Graphics graphics) { } }