java 各种形状的面积和周长计算

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4479624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:34:22  来源:igfitidea点击:

area and perimeter calculation of various shapes

javamath

提问by Jason

Area of a circle, rectangle, triangle, trapezoid, parallelogram, ellipse, sector.
Perimeter of a rectangle, square

Is there a java library which provides mathematical functions to calculate the above?

是否有提供数学函数来计算上述内容的java库?

回答by moinudin

public double areaOfRectangle(double width, double length) {
  return width*height;
}
public double areaOfCircle(double radius) {
  return Math.PI * radius * radius;
}
public double areaOfTriangle(double a, double b, double c) {
  double s = (a+b+c)/2;
  return Math.sqrt(s * (s-a) * (s-b) * (s-c));
}

etc.

等等。

How hard can it be to code up yourself? Do you reallyneed a library to do it for you?

自己编写代码有多难?你真的需要一个图书馆来为你做这件事吗?

You could also port this C codewhich implements area and perimeter calculations for manyshapes.

您还可以移植这个 C 代码,它实现了许多形状的面积和周长计算。

回答by duffymo

I would not recommend that you use a library for such a thing. Just look up the formulas for each one and write the single line of code that each requires.

我不建议你使用库来做这样的事情。只需查找每个公式的公式并编写每个公式所需的单行代码即可。

Sounds like somebody's classic first object-oriented assignment:

听起来像某人经典的第一个面向对象的赋值:

package geometry;

public interface Shape
{
    double perimeter();
    double area();
}

class Rectangle implements Shape
{
    private double width;
    private double height;

    Rectangle(double w, double h)
    {
        this.width = w;
        this.height = h;
    }

    public double perimeter()
    { 
        return 2.0*(this.width + this.height);
    }

    public double area()
    { 
        return this.width*this.height;
    }
}

// You get the idea - same for Triangle, Circle, Square with formula changes.

回答by Alexandre C.

The only non trivial formula you asked for is the perimeter of an ellipse.

您要求的唯一重要公式是椭圆的周长。

You'll need either complete elliptical integrals(google for that), or numerical integration, or approximate formulas(basically, the "Infinite Series 2" is the one you should use)

你需要完整的椭圆积分(谷歌),或者数值积分,或者近似公式(基本上,“无限系列 2”是你应该使用的)

回答by Larry H

For the general case you could get an close estimate with a Monte Carlo method. You need a good random number generator. Take a rectangle large enough to contain the Shape and get a large number of random points in the rectangle. For each, use contains(double x, double y) to see if the point is in the Shape or not. The ratio of points in the Shape to all points in the rectangle, times the area of the rectangle is an estimate of the Shape area.

对于一般情况,您可以使用 Monte Carlo 方法进行近似估计。你需要一个好的随机数生成器。取一个足够大的矩形来包含 Shape 并在矩形中获得大量随机点。对于每个,使用 contains(double x, double y) 来查看点是否在 Shape 中。Shape 中的点与矩形中所有点的比率乘以矩形的面积是对 Shape 面积的估计。