/* * ========================================================================== * PolygonAnalysis.java : This Java 2 program reads vertex coordinates for a * polygon from a datafile and computes a variety of properties..... * * Note. This input procedure assumes that the polygon is arranged into * two columns of input, each containing seven items: * * 1.0 1.0 * 1.0 5.0 * 6.0 5.0 * 7.0 3.0 * 4.0 3.0 * 3.0 2.0 * 3.0 1.0 * * The input procedure reads a complete line of input and then uses the * StringTokenizer to indentify "tokens" separated by blank spaces. * This strategy avoids the need for a fixed format of data input. * * Written By : Mark Austin December, 2003 * ========================================================================== */ import java.lang.Math; import java.util.*; import java.io.*; import java.text.*; class PolygonAnalysis { // Create two-dimensional array to store polygon coordinates. public final static int NoPoints = 7; static float[][] polygon = new float[ NoPoints ][2]; // Constructor method for PolygonAnalysis ...... public PolygonAnalysis() {} // Method polygonInput() : Read data coordinates from "polygon.txt" // and store in "polygon" array. static void polygonInput() throws IOException { float f1, f2; String sline; int i = 0; FileReader inputFile = new FileReader( "polygon.txt" ); BufferedReader in = new BufferedReader( inputFile ); try { while( i <= 6 ) { // Read a new row of polygon coordinates -- this is a string. sline = in.readLine(); StringTokenizer st = new StringTokenizer(sline); // Extract the floating-point no's from the string. int j = 0; while( st.hasMoreTokens() ) { polygon[i][j] = Float.parseFloat( st.nextToken() ); j = j + 1; } i = i + 1; } } catch (FileNotFoundException e){} catch (EOFException e){} in.close(); } // ============================================================ // Retrieve X and Y coordinates of a polygon point.... // ============================================================ static float getX( int PointNo ) { return polygon[ PointNo ][0]; } static float getY( int PointNo ) { return polygon[ PointNo ][1]; } // ============================================================ // Method polygonPrint() : print details of polygon to screen. // Input : polygon ... declared at top of the class. // Output : None. // ============================================================ static void polygonPrint() { for (int i = 0; i < polygon.length; i++) { System.out.print(polygon[i][0] + " "); for (int j = 1; j < polygon[i].length; j++) { System.out.print(polygon[i][j] + " "); } System.out.println(); } } // ============================================================ // Method polygonPerimeter() : compute perimeter of polygon // ============================================================ static float polygonPerimeter() { float fPerimeter = 0; for (int i = 1; i <= 6; i = i + 1) { fPerimeter = fPerimeter + (float) Math.sqrt( Math.pow( polygon[i][0]-polygon[i-1][0],2.0) + Math.pow( polygon[i][1]-polygon[i-1][1],2.0)); } fPerimeter = fPerimeter + (float) Math.sqrt( Math.pow( polygon[6][0]-polygon[0][0],2.0) + Math.pow( polygon[6][1]-polygon[0][1],2.0)); return fPerimeter; } // ============================================================ // Method polygonArea() : compute area of polygon // ============================================================ static float polygonArea() { float fArea = 0; for (int i = 1; i <= 6; i = i + 1) { fArea = fArea + ( polygon[i][0]-polygon[i-1][0] ) * ( polygon[i][1]+polygon[i-1][1] )/2; } fArea = fArea + ( polygon[0][0]-polygon[6][0] ) * ( polygon[0][1]+polygon[6][1] )/2; return fArea; } // Main method for polygon analysis ... public static void main( String args[] ) throws IOException { float fXmin, fXmax, fYmin, fYmax; float fR, fRmin, fRmax; float fPerimeter = 0; float fArea = 0; // Read polygon data from "polygon.txt" polygonInput(); // Print details of polygon data .... polygonPrint(); // Compute and print min/max "x" and "y" coordinates fXmin = fXmax = polygon[0][0]; fYmin = fYmax = polygon[0][1]; for (int i = 1; i <= 6; i = i + 1) { if ( polygon[i][0] > fXmax) fXmax = polygon[i][0]; if ( polygon[i][0] < fXmin) fXmin = polygon[i][0]; if ( polygon[i][1] > fYmax) fYmax = polygon[i][1]; if ( polygon[i][1] < fYmin) fYmin = polygon[i][1]; } System.out.println("Min X coord = " + fXmin ); System.out.println("Max X coord = " + fXmax ); System.out.println("Min Y coord = " + fYmin ); System.out.println("Max Y coord = " + fYmax ); // Compute and print min/max distance of coordinates from origin fRmin = fRmax = (float) Math.sqrt( polygon[0][0]*polygon[0][0] + polygon[0][1]*polygon[0][1] ); for (int i = 1; i <= 6; i = i + 1) { fR = (float) Math.sqrt( polygon[i][0]*polygon[i][0] + polygon[i][1]*polygon[i][1] ); if ( fR > fRmax) fRmax = fR; if ( fR < fRmin) fRmin = fR; } System.out.println("Min nodal distance = " + fRmin ); System.out.println("Max nodal distance = " + fRmax ); // Compute and print the polygon perimeter and area System.out.println("Polygon perimeter = " + polygonPerimeter()); System.out.println("Polygon area = " + polygonArea() ); } }