我有一个多边形,其中一些顶点与线连接(起点和终点是多边形的顶点)。对于每条线(连接顶点)是4条规则:
实施例: 在图像中,红线是坏线, 黑线是多边形边,绿线是好线。
h
很好i
不好,因为它与多边形j
不好,因为它与行h
和i
k
不好,因为它是多边形的边缘g
不好,因为它不包含在多边形我有一个包含多边形顶点的数组,以及一个包含直线的数组,如下所示:
polygon = [
{x: ..., y: ...},
...
]
lines = [
{
p1: {x: ..., y: ...},
p2: {x: ..., y: ...}
},
...
]
lines
数组仅包含有效行。
如何获取由线切割的多边形。
我想要这样的事情:
function getSlicedPolygons(polygon, lines){
// No check for valid lines, they are already valid
// Do the algorithm...
}
我从第一个顶点开始然后一直到达连接的顶点。从该顶点开始,我转到在该行的另一端连接的顶点。现在我去下一个直到另一个连接的顶点,依此类推,直到我到达我开始的顶点。现在我有了第一个多边形。我找不到其他人......
代码(实现,而不是真正的代码):
function getSlicedPolygons(polygon, line){
var results = []
var ofl = 0; // Overflow counter, to prevent infinite looping
while(lines.length > 0){
// Iterate through polygon
var i = 0;
var r = []; // Array of new indices
var iterations = 0; // An overflow counter again
while(i < polygon.length){
r.push[i]
// findNextConnectionIndex(...) searches for a line
// that connects with this vertex and returns the index of
// the other connected vertex
var n = findNextConnectionIndex(i, polygon, lines) || i+1
i=n;
// Don't loop infinite
iterations++;
if(iterations > 10) break;
}
var result = [];
for(var z = 0; z<r.length; z++){
result.push(polygon[r[z]])
}
results.push(result)
// Now I should do something to get another polygon next
// time ...
// Don't loop infinite
ofl++;
if(ofl >= 10) break;
}
return results;
}
它在数组中返回相同的多边形10次...
答案 0 :(得分:2)
将多边形及其相交线视为带有循环的无向图,并对其应用循环检测算法。由于我们知道连接线,事情变得更加简单,我们实际上可以在O(V)
中解决问题。
这将是一个足以解释基本原则的模型。我们可以将多边形转换为由行列表切割的矩形。由于没有线可能相交,因此这也适用于生成的矩形。现在,可以从图形的一角开始并沿着两条边移动,直到两条路径上都达到了3度的顶点。因此,我们找到了切割原始多边形的第一个多边形。从上一步骤中到达的两个点继续,直到再次到达度数3的顶点。当两条路径相遇并且您已列出所有可能的多边形时,请终止此步骤。
运行此流程的单个步骤的图表:
从图形/多边形中的任意点开始,沿任意方向沿多边形遍历顶点,直到达到3度的顶点。存储相应的切片线并沿多边形前进,直到达到3度的顶点。如果它是相同的切片线,你找到了一个“角”顶点,否则存储新的切片线并重复。
python中的工作实现:
def slice_polygon(poly, part):
# find the "corner point"
last_slice = None
last_pt = None
for pt in poly:
s = [x for x in part if pt in x]
if s:
if last_slice in s:
break
last_slice = s[0]
last_pt = pt
# find slicing starting from border-point
a = poly.index(last_pt)
b = (a + 1) % len(poly) # current positions on the polygon
sliced_poly = [] # current polygon
slicing = [] # list of all polygons that are created by the slicing
while a != b:
if not [x for x in part if poly[a] in x]:
# point doesn't lie on slicing-line => add to current polygon
sliced_poly.insert(0, poly[a]) # prepend point
a = (a - 1 + len(poly)) % len(poly) # advance a by one
elif not [x for x in part if poly[b] in x]:
# point doesn't lie on slicing-line => add to current polygon
sliced_poly.append(poly[b]) # append point
b = (b + 1 + len(poly)) % len(poly) # advance by one
else:
# append points of slicing line
sliced_poly.insert(0, poly[a])
sliced_poly.append(poly[b])
# store created polygon and start over
slicing.append(sliced_poly)
sliced_poly = []
# remove partitioning-line at which the algorithm stopped
part.remove([x for x in part if poly[a] in x and poly[b] in x][0])
# add last point to the current polygon, as it's not yet added to it
sliced_poly.append(poly[a])
# add last polygon to result-set
slicing.append(sliced_poly)
return slicing
# test
polygon = [(150, 110), (270, 40), (425, 90), (560, 150), (465, 290), (250, 290), (90, 220)]
partition = [((270, 40), (250, 290)), ((425, 90), (250, 290))]
print(slice_polygon(polygon, partition))
输出:
[[(425,90),(560,150),(465,290),(250,290)],[(270,40),(425,90),(250,290)], [(90,220),(150,110),(270,40),(250,290)]]
由于总共有两个“角点”(至少),如果我们遍历多边形一次,我们保证至少找到一个。