CSES Solution - Polygon Area
Last Updated :
26 Mar, 2024
Your task is to calculate the area of a given polygon.
The polygon consists of n vertices (x1,y1),(x2,y2),.....(xn,yn). The vertices (xi,yi) and (xi+1,yi+1) are adjacent for i=1,2,....,n-1, and the vertices (x1,y1) and (xn,yn) are also adjacent.
Example:
Input: vertices[] = {{1, 1}, {4, 2}, {3, 5}, {1, 4}}
Output: 16
Input: vertices[] = {{1, 3}, {5, 6}, {2, 5}, {1, 4}}
Output: 6
Approach:
The idea is to use Shoelace formula calculates the area of a polygon as half of the absolute difference between the sum of products of x-coordinates and y-coordinates of consecutive vertices.
Shoelace formula Formula: A = \frac{1}{2} \left| \sum_{i=1}^{n-1} (x_i y_{i+1} - x_{i+1} y_i) + x_n y_1 - x_1 y_n \right|
- The formula involves summing the products of the x-coordinates of adjacent vertices and the y-coordinates of the next adjacent vertices, and then subtracting the products of the y-coordinates of adjacent vertices and the x-coordinates of the next adjacent vertices.
- The result is multiplied by 0.5 to get the area of the polygon.
Steps-by-step approach:
- Calculate the area using the shoelace formula:
- Initialize a variable area to 0.
- Iterate over the vertices of the polygon.
- For each vertex i, calculate the product of the x-coordinate of vertex i and the y-coordinate of vertex i+1, and subtract the product of the y-coordinate of vertex i and the x-coordinate of vertex i+1.
- Add the result to the area variable.
- Take the absolute value of the area:
- The shoelace formula can result in a negative area if the polygon is oriented clockwise.
- To ensure that the area is always positive, take the absolute value of the area variable.
- Return the absolute value of the area variable.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
int main()
{
// Number of vertices in the polygon
ll n = 4;
// Array to store the vertices of the polygon
pair<ll, ll> vertices[n]
= { { 1, 3 }, { 5, 6 }, { 2, 5 }, { 1, 4 } };
// Variable to store the area of the polygon
ll area = 0;
// Calculating the area of the polygon using the
// shoelace formula
for (ll i = 0; i < n; i++) {
area += (vertices[i].first
* vertices[(i + 1) % n].second
- vertices[(i + 1) % n].first
* vertices[i].second);
}
// Printing the absolute value of the area
cout << abs(area);
return 0;
}
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
// Number of vertices in the polygon
int n = 4;
// Array to store the vertices of the polygon
int[][] vertices = { { 1, 3 }, { 5, 6 }, { 2, 5 }, { 1, 4 } };
// Variable to store the area of the polygon
int area = 0;
// Calculating the area of the polygon using the shoelace formula
for (int i = 0; i < n; i++) {
area += (vertices[i][0] * vertices[(i + 1) % n][1]
- vertices[(i + 1) % n][0] * vertices[i][1]);
}
// Printing the absolute value of the area
System.out.println(Math.abs(area));
}
}
Python
# Importing the math module for absolute function
import math
# Number of vertices in the polygon
n = 4
# List to store the vertices of the polygon
vertices = [(1, 3), (5, 6), (2, 5), (1, 4)]
# Variable to store the area of the polygon
area = 0
# Calculating the area of the polygon using the shoelace formula
for i in range(n):
area += (vertices[i][0] * vertices[(i + 1) % n][1] - vertices[(i + 1) % n][0] * vertices[i][1])
# Printing the absolute value of the area
print(math.fabs(area))
C#
using System;
public class MainClass {
public static void Main(string[] args) {
// Number of vertices in the polygon
int n = 4;
// Array to store the vertices of the polygon
Tuple<int, int>[] vertices = new Tuple<int, int>[] {
Tuple.Create(1, 3),
Tuple.Create(5, 6),
Tuple.Create(2, 5),
Tuple.Create(1, 4)
};
// Variable to store the area of the polygon
long area = 0;
// Calculating the area of the polygon using the shoelace formula
for (int i = 0; i < n; i++) {
area += (vertices[i].Item1 * vertices[(i + 1) % n].Item2)
- (vertices[(i + 1) % n].Item1 * vertices[i].Item2);
}
// Printing the absolute value of the area
Console.WriteLine(Math.Abs(area));
}
}
JavaScript
// Number of vertices in the polygon
let n = 4;
// Array to store the vertices of the polygon
let vertices = [ { x: 1, y: 3 }, { x: 5, y: 6 }, { x: 2, y: 5 }, { x: 1, y: 4 } ];
// Variable to store the area of the polygon
let area = 0;
// Calculating the area of the polygon using the
// shoelace formula
for (let i = 0; i < n; i++) {
area += (vertices[i].x * vertices[(i + 1) % n].y - vertices[(i + 1) % n].x * vertices[i].y);
}
// Printing the absolute value of the area
console.log(Math.abs(area));
Time complexity: O(N^3), where N is the number of points.
Auxiliary Space: O(1)
Similar Reads
CSES Solutions - Polygon Lattice Points Given a polygon, your task is to calculate the number of lattice points inside the polygon and on its boundary. A lattice point is a point whose coordinates are integers. The polygon consists of n vertices (x1,y1),(x2,y2),....,(xn,yn). The vertices (xi,yi) and (xi+1,yi+1) are adjacent for i=1,2,....
9 min read
Area of Polygons Area of the Polygon is the area enclosed by the boundary of the polygon. A polygon is a closed, two-dimensional shape with straight sides. Each side of a polygon is a line segment, and the points where the sides meet are called vertices.Area of PolygonsA polygon is a figure formed by joining 'n' str
14 min read
Area of Regular Polygon Area of a Polygon is the space covered inside the boundary of any polygon. Polygons are two- dimensional plane figures with at least three or more sides. It is to be noted that a polygon has a finite number of sides. The number of sides in a polygon determines its name. For example, a pentagon is a
7 min read
Program to calculate Area Of Octagon A regular octagon is a closed figure with sides of the same length and internal angles of the same size. It has eight lines of reflective symmetry and rotational symmetry of order 8. The internal angle at each vertex of a regular octagon is 135°. The central angle is 45°.Properties : Convex polygon,
3 min read
How to find the Area of a Regular Polygon? Geometry is a branch of mathematics that involves the calculation of different parameters of plane shapes and solid shapes. In this article, we have discussed formulas to calculate parameters like area, perimeter and volume of different shapes and have briefly described regular polygons and formulas
3 min read
Area of a Hexagon A hexagon is a 6-sided, 2-dimensional geometric figure. The total of the internal angles of any hexagon is 720°. A regular hexagon has 6 rotational symmetries and 6 reflection symmetries. All internal angles are 120 degrees. Examples : Input: 4 Output: 41.5692 Input: 6 Output: 93.5307 Number of vert
3 min read
Check if given polygon is a convex polygon or not Given a 2D array point[][] with each row of the form {X, Y}, representing the co-ordinates of a polygon in either clockwise or counterclockwise sequence, the task is to check if the polygon is a convex polygon or not. If found to be true, then print "Yes" . Otherwise, print "No".In a convex polygon,
9 min read
Practice Questions on Polygons In mathematics, practice questions are extremely important for understanding any concept or theory. Especially when it comes to study a branch like Geometry or Mensuration, the solving of practice questions not only helps in understanding these topics but also increases the accuracy of solving them
7 min read
Polygons | Formula, Types, and Examples A polygon is a two-dimensional shape made of straight-line segments that form a closed figure. In geometry, polygons are plane figures formed by line segments connected to create a closed chain. Polygons have straight sides (not curves) and can have any number of sides.Polygons can be categorized as
8 min read
Area of decagon inscribed within the circle Given here is a regular decagon, inscribed within a circle of radius r, the task is to find the area of the decagon.Examples: Input: r = 5Output: 160.144Input: r = 8Output: 409.969 Approach: We know, side of the decagon within the circle, a = râ(2-2cos36)(Refer here) So, area of the decagon, A = 5*a
4 min read