Java Program to Find Area of a Polygon using Slicker Algorithm

This is a Java Program to find the area of a polygon using slicker method. 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 the source code of the Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to find the area of polygon using Slicker algorithm
  2. import java.util.*;
  3.  
  4. class Area_polygon_Slicker
  5. {
  6.     static final int MAXPOLY = 200;
  7.     static final double EPSILON = 0.000001;
  8.  
  9.     static class Point
  10.     {
  11.         double x, y;
  12.     }
  13.  
  14.     static class Polygon
  15.     {
  16.         Point p[] = new Point[MAXPOLY];
  17.         int n;
  18.  
  19.         Polygon()
  20.         {
  21.             for (int i = 0; i < MAXPOLY; i++)
  22.                 p[i] = new Point();
  23.         }
  24.     }
  25.  
  26.     static double area(Polygon p)
  27.     {
  28.         double total = 0;
  29.         for (int i = 0; i < p.n; i++)
  30.         {
  31.             int j = (i + 1) % p.n;
  32.             total += (p.p[i].x * p.p[j].y) - (p.p[j].x * p.p[i].y);
  33.         }
  34.         return total / 2;
  35.     }
  36.  
  37.     static public void main(String[] args)
  38.     {
  39.         Polygon p = new Polygon();
  40.         Scanner sc = new Scanner(System.in);
  41.         System.out.println("Enter the number of points in Polygon: ");
  42.         p.n = sc.nextInt();
  43.         System.out.println("Enter the coordinates of each point: <x> <y>");
  44.         for (int i = 0; i < p.n; i++)
  45.         {
  46.             p.p[i].x = sc.nextDouble();
  47.             p.p[i].y = sc.nextDouble();
  48.         }
  49.  
  50.         double area = area(p);
  51.         if (area > 0)
  52.             System.out.print("The Area of Polygon with " + p.n
  53.                     + " points using Slicker Algorithm is : " + area);
  54.         else
  55.             System.out.print("The Area of Polygon with " + p.n
  56.                     + " points using Slicker Algorithm is : " + (area * -1));
  57.         sc.close();
  58.     }
  59. }

Output:

$ javac Area_polygon_Slicker.java
$ java Area_polygon_Slicker
 
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.0
 
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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java 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.