Java 计算圆的弦长

Java 计算圆的弦长

一个圆是一个没有角的、平面的、圆形图形。每个圆都有一个起始点,并且圆上的每个点与起始点的距离相等。起始点与圆上某个点之间的距离称为圆的半径。类似地,如果我们从圆的一边绘制一条线到另一边,并且起始点位于其中间,那么该线称为圆的直径。基本上,直径是半径长度的两倍。

圆的弦是指从圆的一个端点触及另一个端点的线段。或者简单地说,弦是指其端点位于圆上的线段。一条弦将圆分为两部分。

根据问题描述,我们需要根据给定的圆的半径和弦与中心的夹角来计算弦的长度。

计算弦长的公式如下:

Length = 2 * r * sin(a/2)

其中,’r’是圆的半径,’a’是弦在圆心的对应角度。

所以,让我们来探索一下。

为了给你展示一些实例−

实例1

Let say radius (r) of circle is 3.
And angle (a) subtended at centre by the chord is 60
Then by using the formula, length of the chord is 5.19.

实例2

Let say radius (r) of circle is 4.
And angle (a) subtended at centre by the chord is 50
Then by using the formula, length of the chord is 6.12.

实例3

Let say radius (r) of circle is 4.
And angle (a) subtended at centre by the chord is 40.
Then by using the formula, length of the chord is 5.1.

步骤

步骤-1 - 通过静态输入或用户输入获取圆的半径和弦所对应的圆心角度。

步骤-2 - 使用公式计算弦的长度。

步骤-3 - 打印结果。

多种方法

我们提供了不同的解决方案。

  • 使用静态输入值。

  • 使用用户输入值。

让我们逐个查看程序及其输出。

方法1:使用静态输入值

在此方法中,我们在程序中初始化半径和角度值。然后通过算法计算弦的长度。

示例

public class Main {
   //main method
   public static void main(String[] args) {
      //radius of circle
      double r = 4;
      //angle subtended by chord at center
      double a = 40;
      //find length of chord by using formula
      double length = 2 * r * Math.sin(a * (3.14 / 180));
      System.out.println("Length of Chord: "+length);
   }
}

输出

Length of Chord: 5.140131589369607

方法2:通过使用用户定义的具有静态输入值的方法

在这个方法中,我们在程序中接受用户输入的半径和角度值。然后通过将这些值作为参数传递给用户定义的方法,在方法内部使用算法来计算弦的长度。

示例

public class Main {
   //main method
   public static void main(String[] args) {
      //radius of circle
      double r = 4;
      //angle subtended by chord at center
      double a = 40;
      length_of_chord(r, a);
   }
   //user defined method to find the length of the chord
   static void length_of_chord(double r, double a) {
      double length = 2 * r * Math.sin(a * (3.14 / 180));
      System.out.println("Length of Chord: "+length);
   }
}

输出

Length of Chord: 5.140131589369607

在本文中,我们探讨了如何在给定圆的半径和弦与圆心所夹的角的情况下,使用不同的方法在Java中计算弦的长度。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程