给定顶点的多边形面积的Python演算
我试图计算一个特定多边形的面积 让我解释清楚。我正在尝试学习如何在Python中处理多边形。我知道有像Shapely这样的可用软件包,但我希望自己使用更常见的软件包(如numpy、scipy或math)编写代码 我目前正在尝试这个练习:给定一些顶点坐标和一个半径度量,计算得到的多边形的面积。该区域由:(圆心位于给定点且半径给定的圆的面积)-(由给定顶点顺时针形成的多边形面积)构成 让我们看一些例子:给定顶点的多边形面积的Python演算,python,math,geometry,polygon,area,Python,Math,Geometry,Polygon,Area,我试图计算一个特定多边形的面积 让我解释清楚。我正在尝试学习如何在Python中处理多边形。我知道有像Shapely这样的可用软件包,但我希望自己使用更常见的软件包(如numpy、scipy或math)编写代码 我目前正在尝试这个练习:给定一些顶点坐标和一个半径度量,计算得到的多边形的面积。该区域由:(圆心位于给定点且半径给定的圆的面积)-(由给定顶点顺时针形成的多边形面积)构成 让我们看一些例子: 我有一个圆,给定的顶点形成一个不与圆相交的多边形。想要的区域就是圆的区域:区域=区域圆(简单任务
from math import pi
# parser for input
def parser():
while 1:
data = list(input().split(' '))
for number in data:
if len(number) > 0:
yield (number)
input_parser = parser()
def get_word():
global input_parser
return next(input_parser)
def get_number():
data = get_word()
try:
return int(data)
except ValueError:
return float(data)
# input data:
X,Y,L = map(int,input().split()) # circle data: X_coord_center, Y_coord_center, radius
N = get_number() # number of vertices of the polygon
vertexes = []
for i in range(N): #vertices of the polygon, given clockwise
xy = (get_number(), get_number())
vertexes.append(xy)
def circleArea(radius):
return pi * radius ** 2
# from question https://stackoverflow.com/questions/24467972/calculate-area-of-polygon-given-x-y-coordinates
def polyArea(coords):
t=0
for count in range(len(coords)-1):
y = coords[count+1][1] + coords[count][1]
x = coords[count+1][0] - coords[count][0]
z = y * x
t += z
return abs(t/2.0)
# area polygon
print("area polygon: ", polyArea(vertexes))
# area circle
print("area circle: ", circleArea(L))
问题是,我只能在这两者之间做点1)(计算一个圆的半径,没什么大不了的)。
事实上,给定这些输入示例,我只能在多边形不与圆相交时计算正确的面积
example where the circle does not intersect the polygon (answerArea = 3.14159)
3 3 1
4
3 5
6 7
8 5
7 2
example where the circle intersects the polygon (answerArea = 36.71737) - it's the image's data
5 5 4
4
4 7
7 9
9 7
8 4
我应该如何修改代码(不使用Shapely)以计算其他两点之一?我没有老师,我是自学成才,网上的提示对我帮助不大
感谢“建议给谁”的帮助,并愉快地为大家编码:)您可能会对计算多边形面积感兴趣(这也适用于凹多边形,但不适用于自交多边形)。谢谢,@JohanC,我尝试将其实现为
polyArea
函数。你觉得可以吗?或者你能建议它的另一种实现方式吗?