Slicker Algorithm in C++

This is a C++ Program to find the area of ploygon using slicker algorithm. The algorithm assumes the usual mathematical convention that positive y points upwards. In computer systems where positive y is downwards (most of them) the easiest thing to do is list the vertices counter-clockwise using the “positive y down” coordinates. The two effects then cancel out to produce a positive area.

Here is source code of the C++ Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const int MAXPOLY = 200;
  6. double EPSILON = 0.000001;
  7.  
  8. class Point
  9. {
  10.     private:
  11.     public:
  12.         double x, y;
  13. };
  14.  
  15. class Polygon
  16. {
  17.     private:
  18.     public:
  19.         Point p[MAXPOLY];
  20.         int n;
  21.  
  22.         Polygon()
  23.         {
  24.             for (int i = 0; i < MAXPOLY; i++)
  25.                 Point p[i];// = new Point();
  26.         }
  27. };
  28.  
  29. double area(Polygon p)
  30. {
  31.     double total = 0;
  32.     for (int i = 0; i < p.n; i++)
  33.     {
  34.         int j = (i + 1) % p.n;
  35.         total += (p.p[i].x * p.p[j].y) - (p.p[j].x * p.p[i].y);
  36.     }
  37.     return total / 2;
  38. }
  39.  
  40. int main(int argc, char **argv)
  41. {
  42.     Polygon p;
  43.  
  44.     cout << "Enter the number of points in Polygon: ";
  45.     cin >> p.n;
  46.     cout << "Enter the coordinates of each point: <x> <y>";
  47.     for (int i = 0; i < p.n; i++)
  48.     {
  49.         cin >> p.p[i].x;
  50.         cin >> p.p[i].y;
  51.     }
  52.  
  53.     double a = area(p);
  54.     if (a > 0)
  55.         cout << "The Area of Polygon with " << (p.n)
  56.                 << " points using Slicker Algorithm is : " << a;
  57.     else
  58.         cout << "The Area of Polygon with " << p.n
  59.                 << " points using Slicker Algorithm is : " << (a * -1);
  60. }

Output:

$ g++ PloygonAreaSlickerAlgo.cpp
$ a.out
 
Enter the number of points in Polygon: 4
Enter the coordinates of each point: <x> <y>
1 1
1 6
6 6
6 1
The Area of Polygon with 4 points using Slicker Algorithm is : 25
 
Enter the number of points in Polygon: 
5
Enter the coordinates of each point: <x> <y>
1 2
4 5
9 8
3 2
1 5
The Area of Polygon with 5points using Slicker Algorithm is : 6.0
------------------
(program exited with code: 0)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement

Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.