Home > Java > javaTutorial > body text

Using a more efficient algorithm to calculate the area of ​​a polygon in Java

PHPz
Release: 2023-08-27 21:57:06
forward
1432 people have browsed it

Using a more efficient algorithm to calculate the area of ​​a polygon in Java

The word "polygon" comes from the Greek "Poly" meaning "many" and "gon" meaning "angle". A polygon is a two-dimensional closed plane shape connected by three or more straight lines. For example triangle, quadrilateral, hexagon, etc.

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

A more flexible algorithm for finding the area of ​​a polygon

Slicker Algorithm

There are two facts you must know. First, according to mathematical convention, points in the y direction upward are always positive. Secondly, according to the computer system, the y-direction points downward and is always positive. This algorithm provides an efficient solution by listing the vertices counterclockwise using positive y-down coordinates. It will cancel out both of these facts, creating a positive zone.

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

algorithm

  • Step 1 - Create a class "Slicker" and its two inner classes "Coordinates" and "Poly".

  • Step 2 - Declare and initialize the constant "MAXIMUM" to limit the number of sides of the polygon.

  • Step 3 - Create an array of objects of class "Coordinates" inside the inner class "Poly". Then, create a constructor of the "Poly" class to store the coordinates in that object array.

  • Step 4 - Further define the method "calcAr" and the parameter "cr". In this method, we will create a for loop that will run to the number of sides of the polygon and calculate the area.

  • Step 5 - Now in the main method, create an object "cr" of class "Poly". We will then get the number of sides and coordinates of the polygon from the user.

  • Step 6 - Finally, we will call the method "calcAr" and check if the area is positive or negative using an if-else block. If positive, the "if" block statement is executed, otherwise the else block is executed.

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));
      }
   }
} 
Copy after login

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
Copy after login

in conclusion

No planar shape can be considered a polygon, such as a circle, although it is a closed planar shape without 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 slicker algorithm.

The above is the detailed content of Using a more efficient algorithm to calculate the area of ​​a polygon in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!