In this lecture, we will cover one of the frequently asked questions in coding tests, which is the “Calculating the Area of a Polygon” problem. We will conduct in-depth learning on how to implement an algorithm to calculate the area of a polygon using JavaScript.
Problem Description
Write a function to calculate the area of a polygon given the coordinates of its vertices. The vertices of the polygon are sorted either in a clockwise or counterclockwise direction, and the vertex coordinates are represented as integers in a two-dimensional coordinate system.
Input
- The number of vertices of the polygon
n
(3 ≤n
≤ 1000) - The coordinates of
n
vertices(x1, y1), (x2, y2), ..., (xn, yn)
Output
The area of the polygon should be printed rounded to two decimal places.
Solution Process
There are several methods to calculate the area of a polygon. Here, we will use the most common “Shoelace Formula (or Polygon Area Formula)”. This formula allows us to easily calculate the area of a polygon.
Shoelace Formula
For the given vertices (x1, y1), (x2, y2), ..., (xn, yn)
, the area A
is calculated as follows:
A = (1/2) * | Σ (xi * yi+1 - yi * xi+1) |
Here, i+1
is set to return to 1 when i
reaches n
using modular arithmetic. This formula considers the contributions of all edges of the polygon in calculating the area.
JavaScript Code Implementation
Let’s implement the above formula in code. Below is the code written in JavaScript.
function calculatePolygonArea(vertices) {
let n = vertices.length;
let area = 0;
for (let i = 0; i < n; i++) {
let x1 = vertices[i][0];
let y1 = vertices[i][1];
let x2 = vertices[(i + 1) % n][0];
let y2 = vertices[(i + 1) % n][1];
area += (x1 * y2) - (y1 * x2);
}
return Math.abs(area / 2).toFixed(2);
}
// Example
let vertices = [[0, 0], [4, 0], [4, 3], [0, 4]];
console.log(calculatePolygonArea(vertices)); // 12.00
Code Explanation
- The
calculatePolygonArea
function takes an array of vertex coordinatesvertices
as input. - It calculates the number of polygon vertices
n
. - It initializes the area
area
to 0, then calculates the area for all vertices. - It adds the contribution to the area using the current vertex
(xi, yi)
and the next vertex(xi+1, yi+1)
. - It connects the last vertex with the first vertex through modulus operation to complete the area calculation.
- It returns the calculated area rounded to two decimal places.
Test Cases
If you have checked the code, let’s add the following test cases.
let testVertices1 = [[0, 0], [0, 2], [2, 2], [2, 0]]; // Rectangle
let testVertices2 = [[0, 0], [4, 0], [4, 3], [0, 4]]; // Irregular Polygon
console.log(calculatePolygonArea(testVertices1)); // 4.00
console.log(calculatePolygonArea(testVertices2)); // 12.00
Conclusion
In this lecture, we explored the theory behind calculating the area of a polygon along with an example of its implementation in JavaScript. I believe that understanding the formula to calculate the area of a polygon and implementing it in actual code will help in coding tests.
The problem of calculating the area of a polygon is frequently asked in real coding tests, so be sure to firmly grasp the basic theories and problem-solving processes. In the next lecture, we will cover another algorithm problem, so I hope for your continued interest.