In Java:
(Area of a convex polygon)
A polygon is convex if it contains any line segment that connects
two points of the polygon. Write a program that prompts the user to
enter the number of points in a convex polygon, then enter the
points clockwise, and display the area of the polygon.
Sample Run
Enter the number of points: 7
Enter the coordinates of the points:
-12 0 -8.5 10 0 11.4 5.5 7.8 6 -5.5 0 -7 -3.5 -5.5
The total area is 244.575
Class Name: Exercise11_15
import java.util.Scanner; public class Exercise11_15 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter the number of points: "); int numPoints = in.nextInt(); double[] x = new double[numPoints]; double[] y = new double[numPoints]; System.out.println("Enter the coordinates of the points:"); for (int i = 0; i < numPoints; i++) { x[i] = in.nextDouble(); y[i] = in.nextDouble(); } double area = 0; for (int i = 0; i < numPoints; i++) { area += x[i] * y[(i + 1) % numPoints] - y[i] * x[(i + 1) % numPoints]; } area = Math.abs(area / 2); System.out.printf("The total area is %.3f\n", area); } }
Get Answers For Free
Most questions answered within 1 hours.