Home  >  Article  >  Java  >  How to get arc length based on given angle in Java?

How to get arc length based on given angle in Java?

WBOY
WBOYforward
2023-09-01 16:29:061392browse

How to get arc length based on given angle in Java?

Arc length refers to the length between two points along an arc (two points along a segment of a circle or curve). Simply put, it is a portion of the circumference of a circle.

When two lines intersect each other, the common intersection point is called a vertex and the geometry between the two arms/lines is called an angle.

According to the problem statement, there is a circle, and there is an angle from which you need to find the arc length.

The formula for finding arc length using diameter -

Arc Length = (Θ / 360) * (d * π)

Among them, "d" refers to the diameter of the circle, and θ refers to the degree of the arc.

The formula for finding arc length using radius -

Arc Length = (Θ / 360) * (2 * π * r)

Among them, "r" refers to the radius of the circle, and θ refers to the degree of the arc.

In this article, we will learn how to find the arc length of a given angle using Java programming language.

Show you some examples

Example 1

Assume that the diameter (d) of the circle is 8 and the given angle is 60 degrees

Then use the arc length formula. We have

Arc Length = 4.18

Example 2

Assume that the diameter (d) of the circle is 7 and the given angle is 50 degrees

Then use the arc length formula. We have

Arc Length = 3.05

Example 3

Assume that the diameter (d) of the circle is 6.5 and the given angle is 45 degrees

Then use the arc length formula. We have

Arc Length = 2.55

grammar

In Java, we have a predefined constant in the Math class of the java.lang package, namely Math.PI, which gives us a pie chart value that is approximately equal to 3.14159265359. The following is its syntax

Math.PI

algorithm

Step-1 - Get the radius or diameter of the hemisphere through initialization or user input.

Step-2 - Calculate arc length using formula.

Step 3 - Print the results.

Multiple methods

We provide solutions in different ways.

  • When the radius and angle are given

  • When the diameter and angle are given

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

Method 1: When the radius and angle are given

In this method, the radius value and arc angle of the circle will be initialized in the program. Then use an algorithm to find the arc length.

Example

import java.io.*;
public class Main {
   public static void main(String args[]) {
      //initialized the radius value
      double r = 4;
      System.out.println("Given radius of circle: "+r);

      //initialized the angle value
      double angle = 40;
      System.out.println("Given angle : "+angle);
      double arcLength;

      //if angle is more than 360 then it is Invalid
      //As no angle is possible with that
      if (angle > 360) {
         System.out.println("Invalid angle");
      }

         //else find the arc length
      else {
         arcLength = (angle / 360) * (2 * Math.PI * r);
         //print result
         System.out.println("Arc length : "+arcLength);
      }
   }
}

Output

Given radius of circle: 4.0
Given angle : 40.0
Arc length : 2.792526803190927

Method 2: When diameter and angle are given

In this method, the radius value and arc angle of the circle will be initialized in the program. Then use an algorithm to find the arc length.

Example

import java.io.*;
public class Main {
   public static void main(String args[]) {
      //initialized the radius value
      double d = 6;
      System.out.println("Given diameter of circle: "+d);

      //initialized the angle value
      double angle = 40;
      System.out.println("Given angle : "+angle);
      double arcLength;

      //if angle is more than 360 then it is Invalid
      //As no angle is possible with that
      if (angle > 360) {
         System.out.println("Invalid angle");
      }

         //else find the arc length
      else {
         arcLength = (angle / 360) * (d * Math.PI);
         //print result
         System.out.println("Arc length : "+arcLength);
      }
   }
}

Output

Given diameter of circle: 6.0
Given angle : 40.0
Arc length : 2.0943951023931953

In this article, we explored how to find the arc length of a given angle in Java using different methods.

The above is the detailed content of How to get arc length based on given angle 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