Given ordered coordinates of a polygon with n vertices. Find area of the polygon. Here ordered mean that the coordinates are given either in clockwise manner or anticlockwise from first vertex to last.

**Examples :**

Input : X[] = {0, 4, 4, 0}, Y[] = {0, 0, 4, 4}; Output : 16 Input : X[] = {0, 4, 2}, Y[] = {0, 0, 4} Output : 8

We can compute area of a polygon using Shoelace formula.

Area = | 1/2 [ (x_{1}y_{2}+ x_{2}y_{3}+ ... + x_{n-1}y_{n}+ x_{n}y_{1}) - (x_{2}y_{1}+ x_{3}y_{2}+ ... + x_{n}y_{n-1}+ x_{1}y_{n}) ] |

Below is implementation of above formula.

## CPP

`// C++ program to evaluate area of a polygon using ` `// shoelace formula ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// (X[i], Y[i]) are coordinates of i'th point. ` `double` `polygonArea(` `double` `X[], ` `double` `Y[], ` `int` `n) ` `{ ` ` ` `// Initialze 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 = i; ` `// j is previous vertex to i ` ` ` `} ` ` ` ` ` `// Return absolute value ` ` ` `return` `abs` `(area / 2.0); ` `} ` ` ` `// Driver program to test above function ` `int` `main() ` `{ ` ` ` `double` `X[] = {0, 2, 4}; ` ` ` `double` `Y[] = {1, 3, 7}; ` ` ` ` ` `int` `n = ` `sizeof` `(X)/` `sizeof` `(X[0]); ` ` ` ` ` `cout << polygonArea(X, Y, n); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to evaluate area ` `// of a polygon using shoelace formula ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// (X[i], Y[i]) are coordinates of i'th point. ` ` ` `public` `static` `double` `polygonArea(` `double` `X[], ` `double` `Y[], ` ` ` `int` `n) ` ` ` `{ ` ` ` `// Initialze 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` `Math.abs(area / ` `2.0` `); ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `double` `X[] = {` `0` `, ` `2` `, ` `4` `}; ` ` ` `double` `Y[] = {` `1` `, ` `3` `, ` `7` `}; ` ` ` ` ` `int` `n = ` `3` `; ` ` ` `System.out.println(polygonArea(X, Y, n)); ` ` ` `} ` ` ` `} ` `// This code is contributed by Sunnnysingh ` |

*chevron_right*

*filter_none*

## Python3

`# python3 program to evaluate ` `# area of a polygon using ` `# shoelace formula ` ` ` `# (X[i], Y[i]) are coordinates of i'th point. ` `def` `polygonArea(X, Y, n): ` ` ` ` ` `# Initialze area ` ` ` `area ` `=` `0.0` ` ` ` ` `# Calculate value of shoelace formula ` ` ` `j ` `=` `n ` `-` `1` ` ` `for` `i ` `in` `range` `(` `0` `,n): ` ` ` `area ` `+` `=` `(X[j] ` `+` `X[i]) ` `*` `(Y[j] ` `-` `Y[i]) ` ` ` `j ` `=` `i ` `# j is previous vertex to i ` ` ` ` ` ` ` `# Return absolute value ` ` ` `return` `int` `(` `abs` `(area ` `/` `2.0` `)) ` ` ` `# Driver program to test above function ` `X ` `=` `[` `0` `, ` `2` `, ` `4` `] ` `Y ` `=` `[` `1` `, ` `3` `, ` `7` `] ` `n ` `=` `len` `(X) ` `print` `(polygonArea(X, Y, n)) ` ` ` `# This code is contributed by ` `# Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to evaluate area ` `// of a polygon using shoelace formula ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// (X[i], Y[i]) are coordinates of i'th point. ` ` ` `public` `static` `double` `polygonArea(` `double` `[] X, ` ` ` `double` `[] Y, ` `int` `n) ` ` ` `{ ` ` ` ` ` `// Initialze 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` `Math.Abs(area / 2.0); ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `double` `[] X = { 0, 2, 4 }; ` ` ` `double` `[] Y = { 1, 3, 7 }; ` ` ` ` ` `int` `n = 3; ` ` ` `Console.WriteLine(polygonArea(X, Y, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to evaluate area of ` `// a polygon using shoelace formula ` ` ` `// (X[i], Y[i]) are ` `// coordinates of i'th point. ` `function` `polygonArea(` `$X` `, ` `$Y` `, ` `$n` `) ` `{ ` ` ` `// Initialze area ` ` ` `$area` `= 0.0; ` ` ` ` ` `// Calculate value of ` ` ` `// shoelace formula ` ` ` `$j` `= ` `$n` `- 1; ` ` ` `for` `(` `$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); ` `} ` ` ` `// Driver Code ` `$X` `= ` `array` `(0, 2, 4); ` `$Y` `= ` `array` `(1, 3, 7); ` ` ` `$n` `= sizeof(` `$X` `); ` ` ` `echo` `polygonArea(` `$X` `, ` `$Y` `, ` `$n` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

2

**Why is it called Shoelace Formula?**

The formula is called so because of the way we evaluate it.

**Example :**

Let the input vertices be (0, 1), (2, 3), and (4, 7). Evaluation procedure matches with process of tying shoelaces. We write vertices as below 0 1 2 3 4 7 0 1 [written twice] we evaluate positive terms as below 0 \ 1 2 \ 3 4 \ 7 0 1 i.e., 0*3 + 2*7 + 4*1 = 18 we evaluate negative terms as below 0 1 2 / 3 4 / 7 0 / 1 i.e., 0*7 + 4*3 + 2*1 = 14 Area = 1/2 (18 - 14) = 2 See this for a clearer image.

**How does this work?**

We can always divide a polygon into triangles. The area formula is derived by taking each edge AB, and calculating the (signed) area of triangle ABO with a vertex at the origin O, by taking the cross-product (which gives the area of a parallelogram) and dividing by 2. As one wraps around the polygon, these triangles with positive and negative area will overlap, and the areas between the origin and the polygon will be cancelled out and sum to 0, while only the area inside the reference triangle remains. [Source : Wiki]

**Related articles : **

Minimum Cost Polygon Triangulation

Find Simple Closed Path for a given set of points

This article is contributed by Utkarsh Trivedi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the remaining vertices of a square from two given vertices
- Check if given polygon is a convex polygon or not
- Number of triangles formed by joining vertices of n-sided polygon with one side common
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Number of cycles formed by joining vertices of n sided polygon at the center
- Number of cycles in a Polygon with lines from Centroid to Vertices
- Polygon with maximum sides that can be inscribed in an N-sided regular polygon
- Find the area of quadrilateral when diagonal and the perpendiculars to it from opposite vertices are given
- Area of a n-sided regular polygon with given side length
- Area of a n-sided regular polygon with given Radius
- Minimum area of a Polygon with three points given
- Program to find Area of Triangle inscribed in N-sided Regular Polygon
- Area of largest Circle inscribe in N-sided Regular polygon
- Find area of the larger circle when radius of the smaller circle and difference in the area is given
- Calculate Volume, Curved Surface Area and Total Surface Area Of Cylinder
- Check if the given point lies inside given N points of a Convex Polygon
- Find the cordinates of the fourth vertex of a rectangle with given 3 vertices
- Orientation of 3 ordered points
- Number of ordered points pair satisfying line equation
- How to check if a given point lies inside or outside a polygon?