Slicker Algorithm to Find the Area of a Polygon in Java


The term ‘Polygon’ originated from the Greek words ‘Poly’ which means ‘many’ and ‘gon’ which means ‘angle’. A polygon is a two dimensional closed plane shape created by connecting three or more than three straight lines. For example, triangle, quadrilateral, hexagon and so forth.

Although there are many ways to find the area of a polygon in this article, we will use the slicker algorithm for this purpose.

Slicker Algorithm to Find Area of a Polygon

Slicker Algorithm

Two facts you must know, First, according to mathematical convention the y-direction points upwards are always positive. Second, according to computer system, the y-diirection points downwards and is always positive. This algorithm provides an efficient solution by listing the vertices counter clockwise using positive y-down coordinates. It will cancel out those two facts resulting in a positive area.

Now let’s discuss a java program that implements slicker algorithm.

Algorithm

  • Step 1 − Create a class ‘Slicker’ and two of its inner class ‘Coordinates’ and ‘Poly’.

  • Step 2 − Declare and initialize a constant ‘MAXIMUM’ to restrict the number of sides of polygon.

  • Step 3 − Inside the inner class ‘Poly’ create an object array of class ‘Coordinates’. Then, create a constructor of class ‘Poly’ to store the coordinates in that object array.

  • Step 4 − Moving further define a method ‘calcAr’ along with parameter ‘cr’. Inside this method, we will create a for loop that will run till the number of sides of polygon and calculate the area.

  • Step 5 − Now inside the main method, create an object ‘cr’ of class ‘Poly’. Then, we will take number of sides and coordinates of the polygon by user.

  • Step 6 − At last, we will call the method ‘calcAr’ and using if-else block we check whether the area is positive or negative. If it is positive then ‘if’ block statement will be executed otherwise else block.

Example

import java.util.*;
public class Slicker {
   // to signify maximum number of sides of polygon
   static final int MAXIMUM = 50; 
   static class Coordinates {
      double c1, c2; 
      // declaring coordinates
   }
   static class Poly {
      // Array object of class Coordinates
      Coordinates cr[] = new Coordinates[MAXIMUM];
      int sides;
      Poly() 
      // constructor
      {
         // to accept input of coordinates
         for (int i = 0; i < MAXIMUM; i++)
         cr[i] = new Coordinates();
      }
   }
   // method to calculate area
   static double caclAr(Poly cr) {
      double res = 0;
      for (int i = 0; i < cr.sides; i++) {
         int j = (i + 1) % cr.sides;
         res += (cr.cr[i].c1 * cr.cr[j].c2)
          - (cr.cr[j].c1 * cr.cr[i].c2);
      }
      return res / 2;
   }
   static public void main(String[] args)
   {
      Poly cr = new Poly(); 
      // object of class 'Poly'
      // Object of scanner class for User inputs
      Scanner in = new Scanner(System.in);
      System.out.print("Enter total number of sides: ");
      cr.sides = in.nextInt();
      // to take coordinates from user
      System.out.println("Enter c1 and c2 coordinates: ");
      for (int i = 0; i < cr.sides; i++) {
         cr.cr[i].c1 = in.nextDouble();
         cr.cr[i].c2 = in.nextDouble();
      }
      // calling user defined method
      double caclAr = caclAr(cr);
      if (caclAr > 0) {
         System.out.print("The area of given Polygon: " + caclAr);
      } else {
         System.out.print("The area of given Polygon: " + (caclAr * -1));
      }
   }
} 

Output

Enter total number of sides: 4
Enter c1 and c2 coordinates: 
2 3
3 5
5 8
8 2
The area of given Polygon: 17.0

Conclusion

Any plane shape could not be considered a polygon for instance take a circle although it is a closed plane shape it doesn’t have any sides. So we can’t call it a polygon. In this article, we have created a java program to calculate the area of a polygon using the slicker algorithm.

Updated on: 12-May-2023

184 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements