我有一个坐标列表列表,每个坐标列表代表SVG路径中由两个点(例如[x1, y1, x2, y2]
)定义的路径段。在某些情况下,列表中还会有一些较小的细分与其他细分完全重叠。
这是一个简化的示例:
segments = [[1, 1, 2, 1], [2, 1, 2, 2], [2, 2, 3, 2], [3, 2, 3, 1], [3, 1, 1, 1]]
分别代表以下路径中的细分:
在这种情况下,第一个句段与最后一个句段完全重叠,因此segments[0]
应该被删除,因为它位于segments[4]
内部。一条路径可以超过8,000个段。 消除较小的重叠段的最有效方法是什么?
这些其他条件进一步定义了此问题:
x
或y
轴(即也可以是[1, 1, 2, 2]
之类的段)。[3, 1, 1, 1]
和[2, 1, 4, 1]
之间可以看到),则不会删除任何分段。答案 0 :(得分:0)
这是一个更简单的答案,它可以捕获所有段(正交或不正交),并且只需要一个通用的软件包NumPy
(和一些基本的几何知识):
import numpy as np
segments = [[1, 1, 2, 1], [2, 1, 2, 2], [2, 2, 3, 2], [3, 2, 3, 1], [3, 1, 1, 1]]
# Function determines if segment between coordinates 1 & 2 completely overlaps
# the segment between coordinates 3 & 4
def completelyOverlaps(x1, x2, x3, x4):
return (x1 <= x3 and x1 <= x4 and x2 >= x3 and x2 >= x4) or \
(x2 <= x3 and x2 <= x4 and x1 >= x3 and x1 >= x4)
overlapped = []
for i in range(len(segments)):
for j in range(i+1, len(segments)):
[x1, y1, x2, y2] = segments[i]
[x3, y3, x4, y4] = segments[j]
# Checks whether the cross product between two different pairs of points
# are both == 0, which means that the segments are both on the same line
if np.cross(np.array([x1-x2, y1-y2]), np.array([x3-x4, y3-y4])) == 0 and \
np.cross(np.array([x1-x2, y1-y2]), np.array([x3-x1, y3-y1])) == 0:
# If lines are vertical, consider the y-coordinates
if x1 == x2:
# If 1st segment fully overlaps 2nd, add latter to the list
if completelyOverlaps(y1, y2, y3, y4):
overlapped.append(segments[j])
# If 2nd segment fully overlaps 1st, add latter to the list
elif completelyOverlaps(y3, y4, y1, y2):
overlapped.append(segments[i])
# In all other cases, consider the x-coordinates
else:
if completelyOverlaps(x1, x2, x3, x4):
overlapped.append(segments[j])
elif completelyOverlaps(x3, x4, x1, x2):
overlapped.append(segments[i])
segments = [s for s in segments if s not in overlapped]
输出:
print(segments)
> [[1, 1, 2, 1], [2, 1, 2, 2], [2, 2, 3, 2], [3, 2, 3, 1]]
答案 1 :(得分:-1)
这种方法是通过使用库来实现的,并且功能齐全,可以解决整个问题,而较旧的方法是使用自定义算法,但没有考虑斜线段。
代码的Google colab链接,以防安装库时出现任何错误。 https://colab.research.google.com/drive/1tcQ5gps8dQz9kNY93rfAK97hSQPjCvOt
from shapely.geometry import LineString
lines = [[1, 1, 1, 2],[2, 1, 4, 1],[1, 1, 2, 1], [2, 1, 2, 2], [2, 2, 3, 2], [3, 2, 3, 1], [3, 1, 1, 1]]
overlaped_lines = []
for i in lines:
for j in lines:
if j == i:
continue
if LineString([(i[0],i[1]),(i[2],i[3])]).within(LineString([(j[0],j[1]),(j[2],j[3])])):
overlaped_lines.append(i)
break
for i in overlaped_lines:
lines.remove(i)
print(lines)
输出:
[[1, 1, 1, 2], [2, 1, 4, 1], [2, 1, 2, 2], [2, 2, 3, 2], [3, 2, 3, 1], [3, 1, 1, 1]]
仅适用于与x或y轴平行的线,而不适用于倾斜线。
def find_overlap(lines):
l = []
i = 0
for x1,y1,x2,y2 in lines:
j=0
for xx1,yy1,xx2,yy2 in lines:
if j == i:
j+=1
continue
#Check for lines along x-axis
if (y2-y1) == 0 and (yy2-yy1) == 0 and y1 == yy1 and y2 == yy2:
a,b,c,d = min(xx1,xx2), max(xx1,xx2), min(x1,x2),max(x1,x2)
if c >= a and d <= b:
l.append(lines[i])
break
#Check for lines along y-axis
if (x2-x1) == 0 and (xx2-xx1) == 0 and x1 == xx1 and x2 == xx2:
a,b,c,d = min(yy1,yy2), max(yy1,yy2), min(y1,y2),max(y1,y2)
if c >= a and d <= b:
l.append(lines[i])
break
j+=1
i+=1
return l
def remove_overlap(l,lines):
for i in l:
lines.remove(i)
return lines
lines = [[1, 1, 2, 1], [2, 1, 2, 2],[3, 4, 3, 1], [2, 2, 3, 2], [3, 2, 3, 1], [3, 1, 1, 1]]
l = find_overlap(lines)
lines_after_overlap_removal = remove_overlap(l,lines)
lines
Out[19]: [[1, 1, 2, 1], [2, 1, 2, 2],[3, 4, 3, 1], [2, 2, 3, 2], [3, 2, 3, 1], [3, 1, 1, 1]]
lines_after_overlap_removal
Out[20]: [[2, 1, 2, 2], [3, 4, 3, 1], [2, 2, 3, 2], [3, 1, 1, 1]]
1)首先,for循环解析给定列表,让我们考虑i = 0的示例, 行[0] = [1、1、2、1]
2)第二个for循环检查沿x和y轴的其他坐标
例如:
第一个for循环的[1,(y1)1,2,(y2)1]沿x轴(即平行于x轴)为y1 = y2。
现在第二个for循环带给我元素[2,(yy1)1,2,(yy2)2],这里y1 == yy1但y2!= yy2
接着继续,最后带来[3,1,1,1],其中y1 == yy1和y2 == yy2。
现在让我们检查其x坐标是否重叠,因为坐标可能是相反的顺序,我们必须将它们向前推,以便我们进行解释, 例如:坐标从3变到1,等于1到3。 通过从列表中查找最小值和最大值的简单操作即可完成。
然后检查转发带有xx1,xx2的x1,x2,如果它们重叠,则在列表l中记下它。即将其附加到列表l。 现在的最后一步是删除重叠的元素,这由最后一个函数remove_overlap完成。