Home  >  Article  >  Java  >  How to tell if two convex regular polygons have the same center in Java?

How to tell if two convex regular polygons have the same center in Java?

WBOY
WBOYforward
2023-08-20 16:37:041194browse

How to tell if two convex regular polygons have the same center in Java?

A polygon is a two-dimensional closed shape with at least 3 sides. Depending on the number of sides, the relationship of the sides to angles, and other characteristics, polygons can be classified under various names such as triangles, squares, and quadrilaterals.

The definition of a convex polygon explains that it is a polygon with all angles less than 180 degrees. This also means that the vertices, the points where two edges meet, point towards the center of the shape.

In this article, we will find out whether two convex regular polygons have the same center.

We will take two sides of a convex polygon with side length "a", and two sides of a convex polygon with side length "b", where "b>a". We then need to check if an "a" sided polygon is embedded into a "b" sided polygon, whether they have the same center.

We will use the formula "b % a == 0" to find this, which means that the side of the "a" side can completely cover the "b" side, which means that both polygons have the same center.

let's start!

Show you some examples

The Chinese translation of

Instance-1

is:

Instance-1

  • Suppose the value of a is 15 and the value of b is 30.

  • After putting the values ​​into the formula "b % a == 0" the result will be −

    • Polygons have the same center

The Chinese translation of

Instance-2

is:

Instance-2

  • Suppose the value of a is 7 and the value of b is 22.

  • After putting the values ​​into the formula "b % a == 0" the result will be −

    • Polygons do not have the same center

algorithm

  • Step-1 − Declare and initialize the variables.

  • Second step - Check the central condition, which is "b % a == 0".

  • Step-3 − Print the results.

Multiple methods

We provide solutions in different ways.

  • By using static input

  • By using user-defined methods

Let’s look at the program and its output one by one.

Method 1: By using static input

In this method, the values ​​of "a" and "b" will be assigned. Then according to the algorithm, we will find out whether two convex regular polygons have the same center

The Chinese translation of

Example

is:

Example

public class Main {
   //main method
   public static void main(String[] args){
      //declaring variables
      int a = 7;
      int b = 22;
      //checking for condition of center
      if (b % a == 0){
         //print if polygon have same center
         System.out.print("Polygon have same center");
      } else {
         //print if polygon do not have same center
         System.out.print("Polygon do not have same center");
      }
   }
} 

Output

Polygon do not have same center

Method 2: Use user-defined methods

In this method, the given values ​​are assigned to "a" and "b". Then a user-defined method is called by passing the given value and the algorithm determines whether the two convex regular polygons have the same center.

The Chinese translation of

Example

is:

Example

public class Main {
   //main method
   public static void main(String[] args){
      
      //declaring variables
      int a = 15;
      int b = 30;
      
      //calling user defined method
      func(a, b);
   }

   //user defined method
   static void func(int a, int b){
   
      //checking for condition of center
      if (b % a == 0){
         
         //print if polygon have same center
         System.out.print("Polygon have same center");
      } else {
         //print if polygon do not have same center
         System.out.print("Polygon do not have same center");
      }
   }
} 

Output

Polygon have same center

In this article, we explored different ways to check whether two convex regular polygons have the same center using Java programming language.

The above is the detailed content of How to tell if two convex regular polygons have the same center in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete