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.
Input: Enter number of sides of the Polygon: 4 Enter the coordinates as : <x> <y> 0 0 1 0 1 1 0 1 Output: 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
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.