# Area of a polygon with given n ordered vertices in C++

In this program, we have to find the area of a polygon. The coordinates of the vertices of this polygon are given. Before we move further lets brushup old concepts for a better understanding of the concept that follows.

The area is the quantitative representation of the extent of any two-dimensional figure.

Polygon is a closed figure with a given number of sides.

Coordinates of vertices are the value of points in the 2-d plane. For example (0,0).

Now, let's see the mathematical formula for finding the area.

## Formula

Area = ½ [(x1y2 + x2y3 + …… + x(n-1)yn + xny1) - (x2y1 + x3y2 + ……. + xny(n-1) + x1yn ) ]

Using this formula the area can be calculated,

## Example

Live Demo

#include <iostream>
#include <math.h>
using namespace std;
double areaOfPolygon(double x[], double y[], int n){
double area = 0.0;
int j = n - 1;
for (int i = 0; i < n; i++){
area += (x[j] + x[i]) * (y[j] - y[i]);
j = i;
}
return abs(area / 2.0);
}
int main(){
double X[] = {0, 1, 4, 8};
double Y[] = {0, 2, 5, 9};
int n = sizeof(X)/sizeof(X[0]);
cout<<"The area is "<<areaOfPolygon(X, Y, n);
}

## Output

The area is 3.5

Updated on: 16-Oct-2019

748 Views