ARTICLE · INTELLIGENCE

战地情报 · 详情页

来自尧图项目组的一线实战观察与深度解析

Java 已知三个点,求多边形的最小面积(Minimum area of a Polygon with three points given)

Java 已知三个点,求多边形的最小面积(Minimum area of a Polygon with three points given)

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

已知正多边形(n > 3)的三个顶点,求由这三个顶点构成的正多边形(四边相等)的最小面积。

例如:

输入:0.00 0.00
1.00 1.00
0.00 1.00
输出:1.00

通过取点 (1.00, 0.00) 形成

边长为 1.0 的正方形,因此面积 = 1.00。

在继续之前,需要注意一点:边数必须至少为 4(注意 n > 3 的条件)。
这里,我们需要找到正多边形的最小面积,因此,为了计算最小面积,我们需要计算所需的 n 值。由于边长未知,我们首先计算由这些点构成的三角形的外接圆半径。外接圆半径的计算公式为
R = abc / 4A ,其中 a、b、c 是所构成三角形的三边长,A 是三角形的面积。三角形的面积可以使用海伦公式计算。

计算出三角形的外接圆半径后,我们使用公式 A =​​ n × (sin(360/n) × r² /2)
计算正多边形的面积。这里,r 代表 n 边形(n 边正多边形)的外接圆半径。
但是,首先我们需要计算 n 的值。为了计算 n,我们首先需要利用余弦公式 计算三角形的三个角:
cosA = (b² + c² - a² ) / 2bc,
cosB = (a² + c² - b² ) / 2ac,
cosC = (a² + b² - c² ) / 2ab。

然后,n 的值由公式 n = π / GCD(A, B, C)给出 ,其中 A、B 和 C 是三角形的三个角。计算出 n 后,将其代入多边形面积的计算公式。以下是该方法的实现:

// Java program to find minimum
// area of polygon of number of
// sides more than three with
// given three points.
class GFG{

// Assigning pi value to variable
static double pi = 3.14159265359;

public static double fmod(double a,
double b)
{
int result = (int) Math.floor(a / b);
return a - result * b;
}

// calculating gcd value of
// two double values .
public static double gcd(double x,
double y)
{
return Math.abs(y) < 1e-4 ? x :
gcd(y, fmod(x, y));
}

// Calculating minimum area of polygon through this function .
public static double min_area_of_polygon(double Ax, double Ay,
double Bx, double By,
double Cx, double Cy)
{
double a, b, c, Radius, Angle_A, Angle_B, Angle_C,
semiperimeter, n, area;

// Calculating the length of the sides
// of the triangle formed from given
/// points a, b, c represents the
// length of different sides of triangle .
a = Math.sqrt((Bx - Cx) * (Bx - Cx) +
(By - Cy) * (By - Cy));
b = Math.sqrt((Ax - Cx) * (Ax - Cx) +
(Ay - Cy) * (Ay - Cy));
c = Math.sqrt((Ax - Bx) * (Ax - Bx) +
(Ay - By) * (Ay - By));

// Here we have calculated the
// semiperimeter of a triangle .
semiperimeter = (a + b + c) / 2;

// Now from the semiperimeter area
// of triangle is derived
// through the heron's formula .
double area_triangle = Math.sqrt(semiperimeter *
(semiperimeter - a) *
(semiperimeter - b) *
(semiperimeter - c));

// Thus circumradius of the triangle
// is derived from the sides and
// area of the triangle calculated .
Radius = (a * b * c) / (4 * area_triangle);

// Now each angle of the triangle
// is derived from the sides
// of the triangle .
Angle_A = Math.acos((b * b + c * c - a * a) /
(2 * b * c));
Angle_B = Math.acos((a * a + c * c - b * b) /
(2 * a * c));
Angle_C = Math.acos((b * b + a * a - c * c) /
(2 * b * a));

// Now n is calculated such that
// area is minimum for the regular n-gon .
n = pi / gcd(gcd(Angle_A, Angle_B), Angle_C);

// calculating area of regular n-gon
// through the circumradius of the triangle .
area = (n * Radius * Radius *
Math.sin((2 * pi) / n)) / 2;

return area;
}

// Driver code
public static void main(String[] args)
{
// Three points are given as input .
double Ax, Ay, Bx, By, Cx, Cy;
Ax = 0.00;
Ay = 0.00;
Bx = 1.00;
By = 1.00;
Cx = 0.00;
Cy = 1.00;
System.out.println(String.format("%.2f",
min_area_of_polygon(Ax, Ay,
Bx, By,
Cx, Cy)));
}
}

// This code is contributed by divyeshrabadiya07

输出:

1.00

时间复杂度: O(log(min(A,B,C)))

辅助空间: O(1),因为没有占用额外的空间。

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

RELATED READING

延伸阅读

更多一线实战笔记与深度复盘,助您持续精进