当前的方法是在区域边界之间定期生成平行线,然后将它们切成段与障碍物或地图边界相交的段。这是一个可以说明这一点的图像。
import numpy as np
from shapely.geometry import Point, Polygon, LineString, MultiLineString, GeometryCollection
def generate_intersections(areaCoords, obstacleCoords, spacing = 1.0):
# Create Shapely polygons
area = Polygon(areaCoords)
obstacle = Polygon(obstacleCoords)
map = area.difference(obstacle)
# Obtain bounding box
minX, minY, maxX, maxY = area.bounds
# Generate parallel lines within bounding box
lines = []
y = minY
while y <= maxY:
lines.append(LineString( [(minX,y),(maxX,y)] ))
y += spacing
# Cut lines where they intersect with Boundaries/Obstacle,
intersections = []
for line in lines:
parallel = []
intersection = line.intersection(map)
if intersection.is_empty:
continue
# Line intersects with boundaries only
if isinstance(intersection, LineString):
coords = list(intersection.xy)
parallel.append(coords)
# Line intersects with obstacle and boundaries
elif isinstance(intersection, (MultiLineString, GeometryCollection)):
for g in list(intersection.geoms):
parallel.append(list(g.xy))
intersections.append(parallel)
return intersections
if __name__ == "__main__":
# Polygon Coordinates
areaCoords = [(2,0),(14,0),(14,12),(2,12)]
obstacleCoords = [(4,5),(9,5),(9,9),(4,9)]
# Get intersecting points
intersections = generate_intersections(areaCoords,obstacleCoords,2.0)
宣布割草区域(蓝线)和障碍物(红线)是多边形的,所有边缘严格垂直或水平,如您所示:
可以连接一对点的三种方式(即图之间将具有边缘)。
因此,算法看起来像这样:
- LOOP P1 over all points
- LOOP P2 over all points > P1
- If P1,P2 satisfy any one of the 3 ways described above
- Add edge between P1 and P2.