Lets write a program to find the area of a polygon if number of sides and coordinates are given.
Program Structure
Here follows the program structure
- The first line reads the number of sides of the polygon.
- The second line reads the each coordinates reads in x1 y1 x2 y2 x3 y3.....xn yn format.
- the next line prints the output, which is the total area of a polygon.
Example
Inputs are
4
0 0 0 4 4 4 4 0
Output is
16.00
Solution
The mathematical formula for finding total area of a polygon is
| 1/2 [ (x1y2 + x2y3 + ... + xn-1yn + xny1) - (x2y1 + x3y2 + ... + xnyn-1 + x1yn) ] ||
Program
Here follows the java program for finding the total area of a polygon by applying above formula
No comments:
Post a Comment