
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement Slicker Algorithm to Find Area of a Polygon
In this article, we implement a C++ program to find the area of a polygon using a slicker algorithm that avoids Triangulation to find the area of a polygon.
What is Slicker Algorithm
The Silcker algorithm is a method for calculating the area of a polygon by performing a series of calculations based on the polygon's vertices. It is an efficient alternative to triangulation, especially for polygons with many sides.
How Silcker Algorithm Works?
Since slicker is a method used to compute the area of a polygon without triangulation. Instead of breaking the polygon into triangles, it directly computes the area using the Shoelace formula
(or Gauss's area formula). This Formula simplifies the calculation and avoids unnecessary complexity.
Following is the Shoelace formula:
(1/2) |(x1y2 + x2y3 + ... + xn-1yn + xny1) - (y1x2 + y2x3 + ... + yn-1xn + ynx1)|
Example
Following is a C++ example to implement the slicker algorithm to avoid the triangulation to find the area of a polygon:
#include <iostream> using namespace std; const int MAX = 200; class P { public: double a, b; }; class Polygon { public: P p[MAX]; int n; Polygon() { for (int i = 0; i < MAX; i++) { P p[i]; } } }; // Function to calculate area using Slicker Algorithm double Area(Polygon p) { double t = 0; for (int i = 0; i < p.n; i++) { int j = (i + 1) % p.n; t += (p.p[i].b * p.p[j].b) - (p.p[j].a * p.p[i].b); } return abs(t) / 2; } int main() { Polygon p; p.n = 4; p.p[0] = {0, 0}; p.p[1] = {4, 0}; p.p[2] = {4, 3}; p.p[3] = {0, 3}; double a = Area(p); cout << "The Area of Polygon with " << p.n << " points using Slicker Algorithm is : " << a << endl; return 0; }
Following is the output of the code ?
The Area of Polygon with 4 points using Slicker Algorithm is : 4.5