简体   繁体   English

具有(x,y)坐标列表的多边形区域

[英]Area of polygon with list of (x,y) coordinates

It might seem a bit odd that I am asking for python code to calculate the area of a polygon with a list of (x,y) coordinates given that there have been solutions offered in stackoverflow in the past. 考虑到过去在stackoverflow中提供了解决方案,我要求使用python代码来计算具有(x,y)坐标列表的多边形的面积似乎有些奇怪。 However, I have found that all the solutions provided are sensitive to the order of the list of (x,y) coordinates given. 但是,我发现提供的所有解决方案都对给定的(x,y)坐标列表的顺序敏感。 For example, with the code below to find an area of a polygon: 例如,使用下面的代码查找多边形的区域:

def area(p):
    return 0.5 * abs(sum(x0*y1 - x1*y0
                             for ((x0, y0), (x1, y1)) in segments(p)))

def segments(p):
    return zip(p, p[1:] + [p[0]])


coordinates1 = [(0.5,0.5), (1.5,0.5), (0.5,1.5), (1.5,1.5)]
coordinates2 = [(0.5,0.5), (1.5,0.5), (1.5,1.5), (0.5,1.5)]

print "coordinates1", area(coordinates1)
print "coordinates2", area(coordinates2)

This returns 这返回

coordinates1 0.0
coordinates2 1.0  #This is the correct area

For the same set of coordinates but with a different order. 对于同一组坐标,但顺序不同。 How would I correct this in order to get the area of the non-intersecting full polygon with a list of random (x,y) coordinates that I want to make into a non-intersecting polygon? 为了获得不相交的完整多边形的面积以及我要制作成不相交多边形的随机(x,y)坐标列表,我该如何纠正呢?

EDIT: I realise now that there can be multiple non-intersecting polygons from a set of coodinates. 编辑:我现在意识到,从一组coodinates中可以有多个不相交的多边形。 Basically I am using scipy.spatial.Voronoi to create Voronoi cells and I wish to calculate the area of the cells once I've fed the coordinates to the scipy Voronoi function - unfortunately the function doesn't always output the coordinates in the order that will allow me to calculate the correct area. 基本上,我使用scipy.spatial.Voronoi创建Voronoi单元格,我希望在将坐标输入scipy Voronoi函数后计算出单元格的面积-不幸的是,该函数并不总是按以下顺序输出坐标将允许我计算正确的面积。

Several non-intersecting polygons can be created from a random list of coordinates (depending on its order), and each polygon will have a different area, so it is essential that you specify the order of the coordinates to build the polygon (see attached picture for an example). 可以从一个随机的坐标列表(取决于其顺序)中创建几个不相交的多边形,并且每个多边形将具有不同的面积,因此必须指定坐标的顺序来构建多边形(请参见所附图片)例如)。 这两个多边形的坐标相同但面积不同

The Voronoi cells are convex, so that the polygon is unambiguously defined. Voronoi单元是凸的,因此可以明确定义多边形。

You can compute the convex hull of the points, but as there are no reflex vertices to be removed, the procedure is simpler. 您可以计算这些点的凸包,但是由于没有要删除的反射顶点,因此此过程更加简单。

1) sort the points by increasing abscissa; 1)通过增加横坐标对点进行排序; in case of ties, sort on ordinates (this is a lexicographical ordering); 如果是平局,则按纵坐标排序(这是字典顺序);

2) consider the straight line from the first point to the last and split the point sequence in a left and a right subsequence (with respect to the line); 2)考虑从第一个点到最后一个点的直线,并将点序列分成左右两个子序列(相对于该线);

3) the requested polygon is the concatenation of the left subsequence and the right one, reversed. 3)所请求的多边形是左子序列和右子序列的串联,颠倒了。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM