Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class C169H
- {
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
- double area;
- double total = 0;
- System.out.print("Please enter the number of vertices of the polygon: ");
- int numVertices = in.nextInt();
- double[][] vertices = new double[numVertices + 1][2];
- System.out.println("Please enter the coordinates of each vertex, separated by spaces: ");
- for(int x = 0; x < numVertices; x++)
- {
- vertices[x][0] = in.nextDouble();
- vertices[x][1] = in.nextDouble();
- }
- vertices[numVertices][0] = vertices[0][0];
- vertices[numVertices][1] = vertices[0][1];
- for(int x = 0; x < vertices.length - 1; x++)
- {
- total += ((vertices[x][0] * vertices[x + 1][1]) - (vertices[x + 1][0] * vertices[x][1]));
- }
- area = Math.abs((double)(total / 2.0));
- System.out.println("The area of the polygon is " + area + " square units.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement