Skip to content
Related Articles

Related Articles

Slicker Algorithm to Find the Area of a Polygon in Java
  • Difficulty Level : Basic
  • Last Updated : 05 Nov, 2020

Slicker Algorithm is a way to determine the area of the n-sided polygon. This algorithm takes the y-direction pointing upwards as positive according to the mathematical conventions but according to computer systems where positive y-direction is downwards, the most efficient way is to list the vertices counter-clockwise using the positive y down coordinates which cancels out the two effects and in turn returns the positive area. 


      Enter number of sides of the Polygon: 4
      Enter the coordinates as : <x> <y>
      0 0
      1 0
      1 1
      0 1
      The Area of Polygon with 4 points using Slicker Algorithm is: 1      


Take no of sides and coordinates of n-sided polygon as an input from the user

Define function Area() which calculates the area with p as an argument as follows:
  for i = 0 to (p.n-1)
      j = (i + 1) % p.n;
     calculate area += (p.p[i].x * p.p[j].y) - (p.p[j].x * p.p[i].y);
     print total area as area/2

Note: Input points must take in order, Program will not work properly with points taken in random order.

Below is the implementation of the above program


// Implement Slicker Algorithm that avoids
// Triangulation to Find Area of a Polygon
import java.util.*;
class Main {
    // defining the maximum no of sides for the Polygon
    static final int MAXSIDES = 200;
    static class Corner {
        double x, y;
    static class Polygon {
        Corner p[] = new Corner[MAXSIDES];
        int n;
            for (int i = 0; i < MAXSIDES; i++)
                p[i] = new Corner();
    // calculating area with Slicker Algorithm
    static double area(Polygon p)
        double total = 0;
        for (int i = 0; i < p.n; i++) {
            int j = (i + 1) % p.n;
            total += (p.p[i].x * p.p[j].y)
                     - (p.p[j].x * p.p[i].y);
        return total / 2;
    static public void main(String[] args)
        Polygon p = new Polygon();
        Scanner sc = new Scanner(;
        // Taking inputs from the user
            "Enter number of sides of the Polygon: ");
        p.n = sc.nextInt();
            "Enter the coordinates as : <x> <y>");
        // Taking the coordinates of each Corner
        for (int i = 0; i < p.n; i++) {
            p.p[i].x = sc.nextDouble();
            p.p[i].y = sc.nextDouble();
        double area = area(p);
        if (area > 0)
                "The Area of Polygon with " + p.n
                + " points using Slicker Algorithm is : "
                + area);
                "The Area of Polygon with " + p.n
                + " points using Slicker Algorithm is : "
                + (area * -1));


Time Complexity: O(N), Where N is a number of sides of the polygon.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :