ARTICLE · INTELLIGENCE

战地情报 · 详情页

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

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

Python 已知三个点,求多边形的最小面积(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 后,将其代入多边形面积的计算公式。以下是该方法的实现:

# Python3 program to find minimum area of
# polygon of number of sides more than three
# with given three points.

# from math lib import every function
from math import *

# assigning pi value to variable
pi = 3.14159265359

# calculating gcd value of two double values .
def gcd(x, y) :

if abs(y) < 1e-4 :
return x
else :
return gcd(y, fmod(x, y))


# Calculating minimum area of polygon
# through this function .
def min_area_of_polygon(Ax, Ay, Bx,
By, Cx, Cy) :

# 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 = sqrt((Bx - Cx) * (Bx - Cx) +
(By - Cy) * (By - Cy))
b = sqrt((Ax - Cx) * (Ax - Cx) +
(Ay - Cy) * (Ay - Cy))
c = 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
area_triangle = 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 = acos((b * b + c * c - a * a) / (2 * b * c))
Angle_B = acos((a * a + c * c - b * b) / (2 * a * c))
Angle_C = 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 *
sin((2 * pi) / n)) / 2

return area

# Driver Code
if __name__ == "__main__" :

# three points are given as input .
Ax = 0.00
Ay = 0.00
Bx = 1.00
By = 1.00
Cx = 0.00
Cy = 1.00

print(round(min_area_of_polygon(Ax, Ay, Bx,
By, Cx, Cy), 1))

# This code is contributed by Ryuga

输出:

1.00

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

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

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

RELATED READING

延伸阅读

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