我们如何计算正多边形内最大圆的直径?
How could we calculate the diameter of the largest circle inside a regular polygon?
我一直在接受以下编程练习的培训:Circles in Polygons。声明是:
You are the owner of a box making company.
Your company can produce any equal sided polygon box, but plenty of
your customers want to transport circular objects in these boxes.
Circles are a very common shape in the consumer industry. Tin cans,
glasses, tyres and CD's are a few examples of these.
As a result you decide to add this information on your boxes: The
largest (diameter) circular object that can fit into a given box.
我找到了以下公式:
取自:https://www.mathopenref.com/polygonincircle.html
所以要计算最大内切圆的直径,我们有:
sideLength / tan(180/numberOfSides)
我写了下面的代码:
public class Polygon {
int sides;
int sideLength;
public Polygon(int sides, int sideLength) {
this.sides = sides;
this.sideLength = sideLength;
}
public double circleDiameter /**/(){
double div = Math.toRadians(180/sides);
System.out.println("div: "+div);
double den = Math.tan(div);
System.out.println("den: "+den);
double diameter = sideLength / den;
System.out.println("diameter: "+diameter);
return diameter;
}
}
但是我想知道为什么它没有通过一项测试并通过了其中两项。以下是从练习中提取的测试:
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class PolygonTest {
@Test
public void test1(){
//Square with sides of 5 units
Polygon poly=new Polygon(4, 5);
assertEquals("5.000", String.format("%.3f", poly.circleDiameter()));
}
@Test
public void test2() {
//Octagon with sides of 9 units
Polygon poly=new Polygon(8, 9);
assertEquals("21.728", String.format("%.3f", poly.circleDiameter()));
}
@Test
public void test3() {
//Triangle with sides of 4 units
Polygon poly=new Polygon(3, 4);
assertEquals("2.309", String.format("%.3f", poly.circleDiameter()));
}
}
并且我们的代码未通过八边形测试。痕迹是:
div: 0.3839724354387525
den: 0.4040262258351568
diameter: 22.275781680746665
expected:<2[1.728]> but was:<2[2.276]>
为什么代码会给出这个结果?我认为可能存在舍入误差。然而它太大了,我认为这是错误的公式。
我也看过:
- Largest circle inside a non-convex polygon
- Is there a simple algorithm for calculating the maximum inscribed circle into a convex polygon?
double div = Math.toRadians(180/sides);
当 sides
为 8 时,结果应为 22.5,但由于 180 和 sides
都是整数,因此使用整数数学进行计算,得出 22。
将一个或两个操作数更改为双精度数以确保没有意外舍入:
double div = Math.toRadians(180.0/sides);
我一直在接受以下编程练习的培训:Circles in Polygons。声明是:
You are the owner of a box making company.
Your company can produce any equal sided polygon box, but plenty of your customers want to transport circular objects in these boxes. Circles are a very common shape in the consumer industry. Tin cans, glasses, tyres and CD's are a few examples of these.
As a result you decide to add this information on your boxes: The largest (diameter) circular object that can fit into a given box.
我找到了以下公式:
取自:https://www.mathopenref.com/polygonincircle.html
所以要计算最大内切圆的直径,我们有:
sideLength / tan(180/numberOfSides)
我写了下面的代码:
public class Polygon {
int sides;
int sideLength;
public Polygon(int sides, int sideLength) {
this.sides = sides;
this.sideLength = sideLength;
}
public double circleDiameter /**/(){
double div = Math.toRadians(180/sides);
System.out.println("div: "+div);
double den = Math.tan(div);
System.out.println("den: "+den);
double diameter = sideLength / den;
System.out.println("diameter: "+diameter);
return diameter;
}
}
但是我想知道为什么它没有通过一项测试并通过了其中两项。以下是从练习中提取的测试:
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class PolygonTest {
@Test
public void test1(){
//Square with sides of 5 units
Polygon poly=new Polygon(4, 5);
assertEquals("5.000", String.format("%.3f", poly.circleDiameter()));
}
@Test
public void test2() {
//Octagon with sides of 9 units
Polygon poly=new Polygon(8, 9);
assertEquals("21.728", String.format("%.3f", poly.circleDiameter()));
}
@Test
public void test3() {
//Triangle with sides of 4 units
Polygon poly=new Polygon(3, 4);
assertEquals("2.309", String.format("%.3f", poly.circleDiameter()));
}
}
并且我们的代码未通过八边形测试。痕迹是:
div: 0.3839724354387525
den: 0.4040262258351568
diameter: 22.275781680746665
expected:<2[1.728]> but was:<2[2.276]>
为什么代码会给出这个结果?我认为可能存在舍入误差。然而它太大了,我认为这是错误的公式。
我也看过:
- Largest circle inside a non-convex polygon
- Is there a simple algorithm for calculating the maximum inscribed circle into a convex polygon?
double div = Math.toRadians(180/sides);
当 sides
为 8 时,结果应为 22.5,但由于 180 和 sides
都是整数,因此使用整数数学进行计算,得出 22。
将一个或两个操作数更改为双精度数以确保没有意外舍入:
double div = Math.toRadians(180.0/sides);