这是做这项工作的功能。它是一个生成器,生成找到的所有多边形(如果有多个连接的多边形)。在
首先,区域必须重写为元组:regions = [[(sl.start, sl.stop) for sl in sllist] for sllist in regions]
排序是通过调用生成器(在循环中或其他方式)来完成的。该函数生成一个多边形作为(x,y)点的列表。获得单独的xx和yy列表很容易:
^{pr2}$
函数本身被详细注释。在def order_points(x, regions):
"""Given two lists of the same length, one of x values and one of regions,
determine how to order the points so that they describe a polygon that
contains the interior of all regions.
This function is a generator that yields disjoint polygons.
Parameters
==========
x: list
x values at which the regions are defined
regions: list of lists of tuples
Each list contains information for one x value. The sublist contains
so-called regions that are described by tuples corresponding to the
bottom and the top of the region. The y values between the bottom and
the top of each region are in the interior of a polygon.
Each list of tuples should be sorted by increasing y value.
Yields
======
polygon: list of tuples
Ordered list of (x, y) coordinated of the points on the polygon.
"""
# Make sure the x values are in ascending order.
xvals, yregions = zip(*sorted(zip(x, regions)))
# Direction is -1 when going toward low x, 1 when going toward high x.
direction = 1
# Indicate whether the inside of the polygon is below, 0, or above, 1, the
# current point.
inside = 1
# List all possible combinations of x index, region index.
tovisit = [(pos, rpos) for pos in range(len(xvals))
for rpos in range(len(yregions[pos]))]
pos, rpos = tovisit.pop(0)
ycur = yregions[pos][rpos][0]
# Keep track of already visited points.
visited = set()
# Keep track of current polygon.
polygon = []
while True:
# Keep track of ordered points.
xcur = xvals[pos]
polygon.append((xcur, ycur))
visited.add((xcur, ycur))
# Find the minimum vertical distance between the current position and
# the following points: points at the next (as specified by direction)
# x value, and points at the current x value
# For points at the next x, if the polygon is currently above, inside =
# 1, the points considered are at the bottom of a region, i.e., at
# index 0 of the tuple.
next_pos = pos + direction
if next_pos < 0 or next_pos >= len(xvals):
next_pos = pos
distance = -1
for ri, region in enumerate(yregions[pos]):
if (xcur, region[inside]) not in visited:
d = abs(ycur - region[inside])
if d < distance or distance == -1:
distance = d
move = ('vertical', (pos, ri, inside))
# For points at the same x, if the polygon is currently above, the
# points considered are at the top of a region, i.e., at index 1 of the
# tuple.
for ri, region in enumerate(yregions[next_pos]):
polypos = (not inside)
if (xvals[next_pos], region[polypos]) not in visited:
d = abs(ycur - region[polypos])
if d < distance or distance == -1:
distance = d
move = ('next', (next_pos, ri, polypos))
# If no suitable next point is found, the polygon is complete. Yield
# the polygon and try to find a separate polygon.
if distance < 0:
yield polygon
polygon = []
direction = 10 # dummy value to detect if no next point is found
while tovisit:
pos, slpos = tovisit.pop(0)
ycur = yregions[pos][slpos][0]
if (xvals[pos], ycur) not in visited:
direction = 1
inside = 1
break
if direction == 10:
break
continue
# Make the move.
if move[0] == 'vertical':
# Vertical move changes the direction (unless it is the very first
# move) and toggles inside/outside.
if len(polygon) > 1:
direction *= -1
inside = int(not inside)
pos, rpos, end = move[1]
ycur = yregions[pos][rpos][end]