Advertisement
Guest User

challenge 169 hard

a guest
Jul 4th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class C169H
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         Scanner in = new Scanner(System.in);
  8.     double area;
  9.     double total = 0;
  10.        
  11.     System.out.print("Please enter the number of vertices of the polygon: ");
  12.     int numVertices = in.nextInt();
  13.        
  14.     double[][] vertices = new double[numVertices + 1][2];
  15.        
  16.         System.out.println("Please enter the coordinates of each vertex, separated by spaces: ");
  17.        
  18.     for(int x = 0; x < numVertices; x++)
  19.     {
  20.             vertices[x][0] = in.nextDouble();
  21.             vertices[x][1] = in.nextDouble();
  22.     }
  23.        
  24.     vertices[numVertices][0] = vertices[0][0];
  25.     vertices[numVertices][1] = vertices[0][1];
  26.        
  27.     for(int x = 0; x < vertices.length - 1; x++)
  28.     {
  29.             total += ((vertices[x][0] * vertices[x + 1][1]) - (vertices[x + 1][0] * vertices[x][1]));
  30.     }
  31.        
  32.     area = Math.abs((double)(total / 2.0));
  33.        
  34.     System.out.println("The area of the polygon is " + area + " square units.");
  35.     }  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement