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 the 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.Â
Example:
Input: Enter number of sides of the Polygon: 4
      Enter the coordinates as : <x> <y>
                           0   0
                           1   0
                           1   1
                           0   1Output: The Area of Polygon with 4 points using Slicker Algorithm is: 1   Â
Approach
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
Java
// Implement Slicker Algorithm that avoids// Triangulation to Find Area of a Polygonimport 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;Â
        Polygon()        {            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(System.in);Â
        // Taking inputs from the userÂ
        System.out.print(            "Enter number of sides of the Polygon: ");Â
        p.n = sc.nextInt();Â
        System.out.println(            "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)            System.out.print(                "The Area of Polygon with " + p.n                + " points using Slicker Algorithm is : "                + area);        else            System.out.print(                "The Area of Polygon with " + p.n                + " points using Slicker Algorithm is : "                + (area * -1));        sc.close();    }} |
Output:
Time Complexity: O(N), Where N is the number of sides of the polygon.
Auxiliary Space: O(1) because constant array p of constant size 200 has been used

