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



A polygon is a closed two-dimensional shape formed by connecting three or more straight lines end-to-end. These lines form sides, and their connection points are called vertices. When the vertices of the polygon are given in a specific order either clockwise or counter-clockwise, we can calculate the area of the polygon using a mathematical formula known as the Shoelace Formula or Surveyor's Formula.

You are given the coordinates of a polygon with n vertices. The vertices are provided in an ordered manner, meaning they are listed either in clockwise or anticlockwise order starting from the first vertex to the last. Your task is to calculate the area of the polygon using a C++ program.


Let's see the following example scenarios to understand the problem better:

Scenario 1

Input: x[] = {0, 4, 4, 0}, y[] = {0, 0, 4, 4};
Output: Area = 16
Explanation:
The vertices formed by the given x and y coordinates are ((0,0), (4,0), (4,4), (0,4)).
Using the Shoelace formula. 
We get the area of the square is 16.

Scenario 2

Input: x[] = {1, 5, 3}, y[] = {2, 2, 6}
Output: Area = 8
Explanation:
The triangle with vertices at ((1,2), (5,2), and (3,6)) 
forms a triangle with base along the x-axis and height 
along the y-axis. Using the Shoelace formula. 
We get the area of the triangle is 8.

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

To find the area of a polygon with n ordered vertices, we use the Shoelace Formula.

The Shoelace Formula is a mathematical method used to calculate the area of a simple polygon when the coordinates of its vertices are known and listed in order either clockwise or anticlockwise.

The formula is as follows:

Area = (1/2) x | (x1y2 + x2y3 + ... + xn?1yn + xny1) - (x2y1 + x3y2 + ... + xnyn?1 + x1yn) |

Where x and y are x and y coordinates of the plane, respectively. So, the x1 is the x coordinate of vertices v1, and y1 is the y coordinate of vertices v1, and so on?

C++ Program to Compute Area of a Polygon

The following is the C++ program to calculate the area of a polygon with given n ordered vertices:

#include <iostream>
using namespace std;

double polygonArea(double x[], double y[], int n) {
   // Initialize area
   double area = 0.0;

   // Calculate value of shoelace formula
   int j = n - 1;
   for (int i = 0; i < n; i++) {
      area += (x[j] + x[i]) * (y[j] - y[i]);
      // j is previous vertex to i
      j = i;
   }
   // Return absolute value
   return abs(area / 2.0);
}
int main() {
   double x[] = {1, 5, 3};
   double y[] = {2, 2, 6};
   int n = sizeof(x) / sizeof(x[0]);
   cout<<"Area = "<<polygonArea(x, y, n);
}

Following is the output:

Area = 8

Conclusion

Here, we learned how simple it was to calculate the area of a polygon given its ordered coordinates using the Shoelace Formula, irrespective of whether it is a triangle, square, or any other polygon. The C++ program demonstrates how easily this formula applies to a real scenario to calculate the area of the polygon.

Updated on: 2025-07-29T14:39:53+05:30

964 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements