[从头学数学] 第273节 [计算几何] 多边形求交集
来源:互联网 发布:win10重置网络设置 编辑:程序博客网 时间:2024/05/21 10:18
剧情提要:
阿伟看到了一本比较有趣的书,是关于《计算几何》的,2008年由北清派出版。很好奇
它里面讲了些什么,就来看看啦。
正剧开始:
星历2016年09月23日 11:58:19, 银河系厄尔斯星球中华帝国江南行省。
如果直接按线段来显示,肯定会得到满意的图像,当然,阿伟是选择画蛇添足了一下。想让它们生成路径。
阿伟看到了一本比较有趣的书,是关于《计算几何》的,2008年由北清派出版。很好奇
它里面讲了些什么,就来看看啦。
正剧开始:
星历2016年09月23日 11:58:19, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起研究[计算几何]]。
两个多边形求交集,比如下图:
最初很容易想到通过顶点在另一个多边形里的位置来判断。
<span style="font-size:18px;">#class Polygon(): #格式:path = [[-6, 9], [8, -7], [8, 3], [-6, 9]] def __init__(self, path): if (path[-1] != path[0]): path.append(path[0]); self.path = path; #顶点数量 self.vertNum = len(self.path)-1; self.vertex = []; self.edge = []; self.vertexCalc(); self.edgeCalc(); def vertexCalc(self): for i in range(self.vertNum): self.vertex.append(Point(self.path[i])); def getVertex(self): return self.vertex; def edgeCalc(self): for i in range(self.vertNum): self.edge.append(SegLine(self.path[i], self.path[i+1])); def getEdge(self): return self.edge; #传入的是[x, y]格式的对象 def pointInPolygon(self, point): #测试点左右两边截多边形的各边,得到的交点数都为奇数,说明测试点在多边形内 #反之在外面 oddNodes = False; #点在多边形的边上 if (Point(point) in self.vertex): return 0; x, y = point[0], point[1]; j = self.vertNum - 1; for i in range(self.vertNum): p1 = self.vertex[i].value(); p2 = self.vertex[j].value(); px1, py1 = p1[0], p1[1]; px2, py2 = p2[0], p2[1]; if (((py1 < y and py2 >= y) or (py2 < y and py1 >= y)) and \ (px1 <= x or px2 <= x)): if ((px1+(y-py1)/(py2-py1)*(px2-px1)) < x): oddNodes = not oddNodes; j = i; if (oddNodes == False): #点不在多边形内 return -1; else: #点在多边形内 return 1; #两个多边形的交集区域 def intersection(self, other): vlist_1 = self.vertex; vlist_2 = other.vertex; commonVert = set(); for i in range(len(vlist_1)): #点在多边形外部是-1, 在边上是0, 在内部是1 if other.pointInPolygon(vlist_1[i].value()) != -1: commonVert.add(vlist_1[i]); for i in range(len(vlist_2)): if self.pointInPolygon(vlist_2[i].value()) != -1: commonVert.add(vlist_2[i]); commonVert = list(commonVert); for i in range(len(commonVert)): commonVert[i] = commonVert[i].value(); #返回顶点序列 return commonVert; #把给定的坐标点阵列数组[x, y],...按照距离它们的中心点的角度进行排列 #是为了把无序排列的闭合曲线上的点进行有序排列,后续可再经过连线形成 #可填充的闭合曲线 def angularSort(self, array): len0 = len(array); len1 = len(array[0]); if (len0 <= 0 or len1 != 2): return array; xTotal = yTotal = xCenter = yCenter = 0; for i in range(len0): xTotal += array[i][0]; yTotal += array[i][1]; xCenter = xTotal/len0; yCenter = yTotal/len0; x = y = xdiff = ydiff = 0; arrayB = []; for i in range(len0): x = array[i][0]; y = array[i][1]; xdiff = x - xCenter; ydiff = y - yCenter; if (abs(xdiff) < 1e-4): if (ydiff > 0): arrayB.append([x, y, math.pi/2]); else: arrayB.append([x, y, math.pi/2*3]); elif (xdiff >= 0 and ydiff > 0): #第一象限 arrayB.append([x, y, math.atan(abs(ydiff/xdiff))]); elif (xdiff < 0 and ydiff >= 0): #第二象限 arrayB.append([x, y, math.pi-math.atan(abs(ydiff/xdiff))]); elif (xdiff <= 0 and ydiff < 0): #第三象限 arrayB.append([x, y, math.pi+math.atan(abs(ydiff/xdiff))]); else: #第四象限 arrayB.append([x, y, math.pi*2-math.atan(abs(ydiff/xdiff))]); arrayB = sorted(arrayB, key = lambda a:(a[2], (a[0]-xCenter)**2+(a[1]-yCenter)**2)); retArray = []; for i in range(len(arrayB)): retArray.append(arrayB[i][:-1]); return retArray;>>> 20[][[4, -1], [3.56, -0.67], [6, -3]][[4, -1], [3.6, -0.6], [3.56, -0.67]][][][][[4, -1], [3.6, -0.6]][[4, -1], [3.6, -0.6]][[4, -1], [3.6, -0.6]][[4, -1], [1.5, 1.5], [3.6, -0.6]][[4, -1], [1.5, 1.5], [3.6, -0.6]][[4, -1], [1.5, 1.5], [3.6, -0.6]][[4, -1]][[4, -1], [3.08, -1.38], [3.33, -1.67], [3.56, -0.67]][[4, -1], [3.56, -0.67], [1.33, 1]][[3.56, -0.67], [1.5, 1.5], [3.6, -0.6]][[4, -1], [1.33, 1], [3.56, -0.67]][[1.33, 1], [3.56, -0.67]][][[3.56, -0.67]]20[[], [[4, -1], [3.56, -0.67], [6, -3]], [[4, -1], [3.6, -0.6], [3.56, -0.67]], [], [], [], [[4, -1], [3.6, -0.6]], [[4, -1], [3.6, -0.6]], [[4, -1], [3.6, -0.6]], [[4, -1], [1.5, 1.5], [3.6, -0.6]], [[4, -1], [1.5, 1.5], [3.6, -0.6]], [[4, -1], [1.5, 1.5], [3.6, -0.6]], [[4, -1]], [[4, -1], [3.08, -1.38], [3.33, -1.67], [3.56, -0.67]], [[4, -1], [3.56, -0.67], [1.33, 1]], [[3.56, -0.67], [1.5, 1.5], [3.6, -0.6]], [[4, -1], [1.33, 1], [3.56, -0.67]], [[1.33, 1], [3.56, -0.67]], [], [[3.56, -0.67]]]---[[], [[4, -1], [3.56, -0.67], [6, -3]], [[3.6, -0.6], [3.56, -0.67], [4, -1]], [], [], [], [[4, -1], [3.6, -0.6]], [[4, -1], [3.6, -0.6]], [[4, -1], [3.6, -0.6]], [[1.5, 1.5], [3.6, -0.6], [4, -1]], [[1.5, 1.5], [3.6, -0.6], [4, -1]], [[1.5, 1.5], [3.6, -0.6], [4, -1]], [[4, -1]], [[4, -1], [3.56, -0.67], [3.08, -1.38], [3.33, -1.67]], [[1.33, 1], [4, -1], [3.56, -0.67]], [[1.5, 1.5], [3.56, -0.67], [3.6, -0.6]], [[1.33, 1], [4, -1], [3.56, -0.67]], [[1.33, 1], [3.56, -0.67]], [], [[3.56, -0.67]]]>>> def tmp15(): #两组路径 path1 = a6=[[[2, -3], [3.33, -1.67], [5.09, -3.68], [2, -6], [1.2, -6.6], [-0.86, -5.57], [-0.4, -4.2], [1, 0], [2, -3]], [[2, -3], [3.33, -1.67], [5.09, -3.68], [5.56, -3.33], [6, -3], [4, -1], [3.56, -0.67], [3.08, -1.38], [2, -3]], [[2, -3], [3.33, -1.67], [5.09, -3.68], [5.56, -3.33], [4, -1], [3.6, -0.6], [3.56, -0.67], [3.08, -1.38], [2, -3]], [[2, -3], [3.33, -1.67], [3.08, -1.38], [1.24, 0.72], [1.33, 1], [0.44, 1.67], [0.46, 1.62], [1, 0], [2, -3]], [[2, -3], [3.33, -1.67], [3.08, -1.38], [3.56, -0.67], [1.33, 1], [1.24, 0.72], [0.46, 1.62], [1, 0], [2, -3]], [[2, -3], [3.33, -1.67], [3.08, -1.38], [3.56, -0.67], [1.33, 1], [0.44, 1.67], [0.46, 1.62], [1, 0], [2, -3]], [[2, -3], [3.33, -1.67], [4, -1], [3.6, -0.6], [3.56, -0.67], [3.08, -1.38], [1.24, 0.72], [1, 0], [2, -3]], [[2, -3], [3.33, -1.67], [4, -1], [3.6, -0.6], [3.56, -0.67], [1.33, 1], [1.24, 0.72], [1, 0], [2, -3]], [[2, -3], [3.33, -1.67], [4, -1], [3.6, -0.6], [3.56, -0.67], [1.33, 1], [1.24, 0.72], [3.08, -1.38], [2, -3]], [[2, -3], [3.33, -1.67], [4, -1], [3.6, -0.6], [1.5, 1.5], [1.33, 1], [1.24, 0.72], [1, 0], [2, -3]], [[2, -3], [3.33, -1.67], [4, -1], [3.6, -0.6], [1.5, 1.5], [1.33, 1], [1.24, 0.72], [3.08, -1.38], [2, -3]], [[2, -3], [3.33, -1.67], [4, -1], [3.6, -0.6], [1.5, 1.5], [1.33, 1], [3.56, -0.67], [3.08, -1.38], [2, -3]], [[2, -3], [3.33, -1.67], [4, -1], [3.56, -0.67], [3.08, -1.38], [1.24, 0.72], [0.46, 1.62], [1, 0], [2, -3]], [[2, -3], [3.33, -1.67], [4, -1], [3.56, -0.67], [1.33, 1], [1.24, 0.72], [0.46, 1.62], [1, 0], [2, -3]], [[2, -3], [3.33, -1.67], [4, -1], [3.56, -0.67], [1.33, 1], [0.44, 1.67], [0.46, 1.62], [1, 0], [2, -3]], [[2, -3], [1, 0], [1.24, 0.72], [1.33, 1], [1.5, 1.5], [3.6, -0.6], [3.56, -0.67], [3.08, -1.38], [2, -3]], [[2, -3], [1, 0], [1.24, 0.72], [1.33, 1], [3.56, -0.67], [4, -1], [3.33, -1.67], [3.08, -1.38], [2, -3]], [[2, -3], [1, 0], [1.24, 0.72], [0.46, 1.62], [0.44, 1.67], [1.33, 1], [3.56, -0.67], [3.08, -1.38], [2, -3]], [[2, -3], [1, 0], [0.46, 1.62], [0.36, 1.73], [0.44, 1.67], [1.33, 1], [1.24, 0.72], [3.08, -1.38], [2, -3]], [[2, -3], [1, 0], [0.46, 1.62], [0.36, 1.73], [0.44, 1.67], [1.33, 1], [3.56, -0.67], [3.08, -1.38], [2, -3]]] path2 = a8=[[[4, -1], [6, -3], [6, 3], [4.29, 0.43], [3.67, -0.5], [1.78, 2.33], [1.5, 1.5], [3.6, -0.6], [4, -1]], [[4, -1], [6, -3], [6, 3], [2, 3], [1.78, 2.33], [1.5, 1.5], [1.33, 1], [3.56, -0.67], [4, -1]], [[4, -1], [6, -3], [6, 3], [2, 3], [1.78, 2.33], [1.5, 1.5], [3.6, -0.6], [3.56, -0.67], [4, -1]], [[4, -1], [6, -3], [6, 3], [2, 3], [1.78, 2.33], [3.67, -0.5], [3.6, -0.6], [3.56, -0.67], [4, -1]], [[4, -1], [6, -3], [6, 3], [2, 3], [1.33, 3], [1.78, 2.33], [1.5, 1.5], [3.6, -0.6], [4, -1]], [[4, -1], [6, -3], [6, 3], [2, 3], [1.33, 3], [1.78, 2.33], [3.67, -0.5], [3.6, -0.6], [4, -1]], [[4, -1], [6, -3], [6, 3], [2, 3], [1.33, 3], [0, 3], [1.5, 1.5], [3.6, -0.6], [4, -1]], [[4, -1], [3.6, -0.6], [3.67, -0.5], [1.78, 2.33], [2, 3], [6, 3], [4.29, 0.43], [4.5, 0], [4, -1]], [[4, -1], [3.6, -0.6], [3.67, -0.5], [1.78, 2.33], [1.33, 3], [2, 3], [6, 3], [4.5, 0], [4, -1]], [[4, -1], [3.6, -0.6], [1.5, 1.5], [1.78, 2.33], [2, 3], [6, 3], [4.29, 0.43], [4.5, 0], [4, -1]], [[4, -1], [3.6, -0.6], [1.5, 1.5], [1.78, 2.33], [3.67, -0.5], [4.29, 0.43], [6, 3], [4.5, 0], [4, -1]], [[4, -1], [3.6, -0.6], [1.5, 1.5], [1.78, 2.33], [1.33, 3], [2, 3], [6, 3], [4.5, 0], [4, -1]], [[4, -1], [3.6, -0.6], [1.5, 1.5], [0, 3], [1.33, 3], [2, 3], [6, 3], [4.5, 0], [4, -1]], [[4, -1], [4.5, 0], [6, -3], [5.56, -3.33], [5.09, -3.68], [3.33, -1.67], [3.08, -1.38], [3.56, -0.67], [4, -1]], [[4, -1], [4.5, 0], [4.29, 0.43], [3.67, -0.5], [1.78, 2.33], [1.5, 1.5], [1.33, 1], [3.56, -0.67], [4, -1]], [[4, -1], [4.5, 0], [4.29, 0.43], [3.67, -0.5], [1.78, 2.33], [1.5, 1.5], [3.6, -0.6], [3.56, -0.67], [4, -1]], [[4, -1], [4.5, 0], [4.29, 0.43], [3.67, -0.5], [3.6, -0.6], [1.5, 1.5], [1.33, 1], [3.56, -0.67], [4, -1]], [[4, -1], [4.5, 0], [6, 3], [2, 3], [1.78, 2.33], [1.5, 1.5], [1.33, 1], [3.56, -0.67], [4, -1]], [[4, -1], [4.5, 0], [6, 3], [2, 3], [1.78, 2.33], [1.5, 1.5], [3.6, -0.6], [3.56, -0.67], [4, -1]], [[4, -1], [4.5, 0], [6, 3], [2, 3], [1.78, 2.33], [3.67, -0.5], [3.6, -0.6], [3.56, -0.67], [4, -1]]] len_ = min(len(path1), len(path2)); print(len_); interPath = []; for i in range(len_): poly1 = Polygon(path1[i]); poly2 = Polygon(path2[i]); interPath.append(poly1.intersection(poly2)); print(len(interPath)); print(interPath); #调用geo的几何变形类 transform = geo.Transform(); result = []; for i in range(len(interPath)): if (len(interPath[i]) > 3): result.append(transform.angularSort(interPath[i])); else: result.append(interPath[i]); print('---'); print(result);#</span>
<span style="font-size:18px;">//$path1 = a7=[[[6, -3], [8, -7], [5.35, -3.97], [5.09, -3.68], [5.56, -3.33], [4, -1], [4.5, 0], [6, 3], [6, -3]], [[6, -3], [8, -7], [5.35, -3.97], [5.09, -3.68], [3.33, -1.67], [3.08, -1.38], [3.56, -0.67], [4, -1], [6, -3]], [[6, -3], [8, -7], [5.35, -3.97], [5.09, -3.68], [3.33, -1.67], [4, -1], [4.5, 0], [6, 3], [6, -3]], [[6, -3], [4.5, 0], [4.29, 0.43], [3.67, -0.5], [1.78, 2.33], [1.5, 1.5], [3.6, -0.6], [4, -1], [6, -3]], [[6, -3], [4.5, 0], [4.29, 0.43], [3.67, -0.5], [1.78, 2.33], [1.33, 3], [2, 3], [6, 3], [6, -3]], [[6, -3], [4.5, 0], [4.29, 0.43], [6, 3], [2, 3], [1.78, 2.33], [3.67, -0.5], [4, -1], [6, -3]], [[6, -3], [4.5, 0], [4, -1], [3.67, -0.5], [1.78, 2.33], [1.33, 3], [2, 3], [6, 3], [6, -3]], [[6, -3], [4.5, 0], [4, -1], [3.6, -0.6], [3.67, -0.5], [1.78, 2.33], [2, 3], [6, 3], [6, -3]], [[6, -3], [4.5, 0], [4, -1], [3.6, -0.6], [1.5, 1.5], [1.78, 2.33], [2, 3], [6, 3], [6, -3]], [[6, -3], [4.5, 0], [4, -1], [3.56, -0.67], [3.6, -0.6], [3.67, -0.5], [4.29, 0.43], [6, 3], [6, -3]], [[6, -3], [4, -1], [5.56, -3.33], [5.67, -3.5], [8, -7], [8, 3], [6, 3.86], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.67, -0.5], [1.78, 2.33], [2, 3], [3.33, 5], [6, 3.86], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.67, -0.5], [3.6, -0.6], [1.5, 1.5], [1.78, 2.33], [2, 3], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.6, -0.6], [3.67, -0.5], [1.78, 2.33], [1.33, 3], [2, 3], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.6, -0.6], [1.5, 1.5], [1.78, 2.33], [3.67, -0.5], [4.29, 0.43], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.6, -0.6], [1.5, 1.5], [1.78, 2.33], [1.33, 3], [2, 3], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.6, -0.6], [1.5, 1.5], [0, 3], [1.33, 3], [2, 3], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.56, -0.67], [3.6, -0.6], [3.67, -0.5], [1.78, 2.33], [2, 3], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.56, -0.67], [3.6, -0.6], [1.5, 1.5], [1.78, 2.33], [2, 3], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.56, -0.67], [1.33, 1], [1.5, 1.5], [1.78, 2.33], [2, 3], [6, 3], [6, -3]]]$path2 = [[[6, 9], [2.4, 5.4], [0.8, 3.8], [1.33, 3], [2, 3], [6, 3], [6, 3.86], [3.33, 5], [6, 9]], [[6, 9], [2.4, 5.4], [0.8, 3.8], [1.33, 3], [2, 3], [3.33, 5], [6, 3.86], [8, 3], [6, 9]], [[6, 9], [2.4, 5.4], [0.8, 3.8], [0, 3], [1.5, 1.5], [1.78, 2.33], [2, 3], [3.33, 5], [6, 9]], [[6, 9], [2.4, 5.4], [0.8, 3.8], [0, 3], [1.33, 3], [1.78, 2.33], [2, 3], [3.33, 5], [6, 9]], [[6, 9], [2.4, 5.4], [0.8, 3.8], [0, 3], [1.33, 3], [2, 3], [6, 3], [6, 3.86], [6, 9]], [[6, 9], [2.4, 5.4], [0.8, 3.8], [0, 3], [1.33, 3], [2, 3], [3.33, 5], [6, 3.86], [6, 9]], [[6, 9], [2.4, 5.4], [3.33, 5], [2, 3], [6, 3], [6, -3], [8, -7], [8, 3], [6, 9]], [[6, 9], [2.4, 5.4], [3.33, 5], [6, 3.86], [6, 3], [6, -3], [8, -7], [8, 3], [6, 9]], [[6, 9], [2.4, 5.4], [-6, 9], [-0.37, 2.56], [0, 3], [1.33, 3], [2, 3], [3.33, 5], [6, 9]], [[6, 9], [3.33, 5], [2, 3], [1.78, 2.33], [3.67, -0.5], [4.29, 0.43], [6, 3], [6, 3.86], [6, 9]], [[6, 9], [3.33, 5], [2, 3], [6, 3], [6, -3], [5.67, -3.5], [8, -7], [8, 3], [6, 9]], [[6, 9], [3.33, 5], [2, 3], [6, 3], [6, -3], [8, -7], [8, 3], [6, 3.86], [6, 9]], [[6, 9], [3.33, 5], [2, 3], [6, 3], [4.5, 0], [6, -3], [8, -7], [8, 3], [6, 9]], [[6, 9], [3.33, 5], [6, 3.86], [6, 3], [6, -3], [5.67, -3.5], [8, -7], [8, 3], [6, 9]], [[6, 9], [3.33, 5], [6, 3.86], [6, 3], [4.5, 0], [6, -3], [8, -7], [8, 3], [6, 9]], [[6, 9], [6, 3.86], [6, 3], [4.29, 0.43], [4.5, 0], [6, -3], [8, -7], [8, 3], [6, 9]], [[6, 9], [6, 3.86], [6, 3], [6, -3], [5.56, -3.33], [5.67, -3.5], [8, -7], [8, 3], [6, 9]], [[6, 9], [6, 3.86], [6, 3], [6, -3], [5.67, -3.5], [5.35, -3.97], [8, -7], [8, 3], [6, 9]], [[6, 9], [6, 3.86], [6, 3], [4.5, 0], [6, -3], [5.67, -3.5], [8, -7], [8, 3], [6, 9]], [[6, 9], [6, 3.86], [6, 3], [4.5, 0], [4, -1], [6, -3], [8, -7], [8, 3], [6, 9]]]//交集$inter = [[[6, 3]], [], [], [[1.78, 2.33]], [[6, 3], [2, 3], [1.33, 3]], [[2, 3]], [[6, -3], [6, 3], [2, 3]], [[6, -3], [6, 3]], [[2, 3]], [[3.67, -0.5], [4.29, 0.43], [6, 3]], [[8, 3], [6, 3.86], [6, 3], [5.67, -3.5], [6, -3], [8, -7]], [[6, 3], [6, 3.86], [3.33, 5], [2, 3], [6, -3]], [[6, 3], [2, 3], [4.5, 0], [6, -3]], [[6, -3], [6, 3]], [[6, -3], [6, 3], [4.5, 0]], [[6, 3], [4.29, 0.43], [4.5, 0], [6, -3]], [[6, -3], [6, 3]], [[6, -3], [6, 3]], [[6, -3], [6, 3], [4.5, 0]], [[6, 3], [4.5, 0], [4, -1], [6, -3]]]if (1) {var r = 20; config.setSector(1,1,1,1); config.graphPaper2D(0, 0, r); config.axis2D(0, 0, 250, 1.2); //坐标轴设定 var scaleX = 2*r, scaleY = 2*r; var spaceX = 2, spaceY = 2; var xS = -10, xE = 10; var yS = -10, yE = 10; config.axisSpacing(xS, xE, spaceX, scaleX, 'X'); config.axisSpacing(yS, yE, spaceY, scaleY, 'Y'); var transform = new Transform(); //顶点var a = [];for (var i = 0; i < $vertex.length; i++) {a.push($vertex[i][0]);}//显示变换 if (a.length > 0) { a = transform.scale(transform.translate(a, 0, 0), scaleX/spaceX, scaleY/spaceY);}var lable = [];for (var i = 0; i < 100; i++) {lable.push(i.toFixed(0));}/*//边集var b = [];for (var i = 0; i < $seg.length; i++) {b.push([a[$seg[i][0]], a[$seg[i][1]]]);}var edges = b.length;for (var i = 0; i < edges; i++) {shape.multiLineDraw([].concat(b[i]), 'red');}*/var colorArray = ['red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'purple', ];var seg = [];var nSeg = $divideSeg.length;plot.setLineWidth(2);for (var i = 0; i < nSeg; i++) {seg = transform.scale(transform.translate($divideSeg[i], 0, 0), scaleX/spaceX, scaleY/spaceY);shape.multiLineDraw([].concat(seg), 'pink');}var nPath = $inter.length;i = xGlobal;//路径var path1 = transform.scale(transform.translate($path1[i%nPath], 0, 0), scaleX/spaceX, scaleY/spaceY);plot.setLineWidth(8);shape.multiLineDraw([].concat(path1), colorArray[4]);var path2 = transform.scale(transform.translate($path2[i%nPath], 0, 0), scaleX/spaceX, scaleY/spaceY);plot.setLineWidth(6);shape.multiLineDraw([].concat(path2), colorArray[3]);//主要顶点shape.pointDraw([].concat(a), 'blue', 1, 1, lable);var interPoints = $inter[i%nPath].length;if (interPoints > 0) {var interpath = transform.scale(transform.translate($inter[i%nPath], 0, 0), scaleX/spaceX, scaleY/spaceY);if (interPoints >= 2) {plot.setLineWidth(4);shape.strokeDraw([].concat(interpath), colorArray[0]);}else {shape.pointDraw([].concat(interpath), 'red');}}}//</span>
但更多的会像这样:
记录顶点然后不论采取何种排序方式,都可能会排出很奇怪的边来,这些边并不是原多边形的边。
所以还是直接记录所有的相交边,内部边好了。
<span style="font-size:18px;">#class Polygon(): #格式:path = [[-6, 9], [8, -7], [8, 3], [-6, 9]] def __init__(self, path): if (path[-1] != path[0]): path.append(path[0]); self.path = path; #顶点数量 self.vertNum = len(self.path)-1; self.vertex = []; self.edge = []; self.vertexCalc(); self.edgeCalc(); def vertexCalc(self): for i in range(self.vertNum): self.vertex.append(Point(self.path[i])); def getVertex(self): return self.vertex; def edgeCalc(self): for i in range(self.vertNum): self.edge.append(SegLine(self.path[i], self.path[i+1])); def getEdge(self): return self.edge; #传入的是[x, y]格式的对象 def pointInPolygon(self, point): #测试点左右两边截多边形的各边,得到的交点数都为奇数,说明测试点在多边形内 #反之在外面 oddNodes = False; #点在多边形的边上 if (Point(point) in self.vertex): return 0; x, y = point[0], point[1]; j = self.vertNum - 1; for i in range(self.vertNum): p1 = self.vertex[i].value(); p2 = self.vertex[j].value(); px1, py1 = p1[0], p1[1]; px2, py2 = p2[0], p2[1]; if (((py1 < y and py2 >= y) or (py2 < y and py1 >= y)) and \ (px1 <= x or px2 <= x)): if ((px1+(y-py1)/(py2-py1)*(px2-px1)) < x): oddNodes = not oddNodes; j = i; if (oddNodes == False): #点不在多边形内 return -1; else: #点在多边形内 return 1; #线段是否在多边形内的判断,传入[[x1, y1], [x2, y2]] def segInPolygon(self, seg): p1, p2 = seg[0], seg[1]; #组成线段的两个端点都不在多边形外部,认为它是在多边形内部(或边上) if self.pointInPolygon(p1) >= 0 and self.pointInPolygon(p2) >= 0: return True; return False; #两个多边形的交集区域 #返回内部点的集合并不能准确生成相交多边形区域,但有时也会用到 def intersection(self, other): vlist_1 = self.vertex; vlist_2 = other.vertex; commonVert = set(); for i in range(len(vlist_1)): #点在多边形外部是-1, 在边上是0, 在内部是1 if other.pointInPolygon(vlist_1[i].value()) != -1: commonVert.add(vlist_1[i]); for i in range(len(vlist_2)): if self.pointInPolygon(vlist_2[i].value()) != -1: commonVert.add(vlist_2[i]); commonVert = list(commonVert); for i in range(len(commonVert)): commonVert[i] = commonVert[i].value(); #返回顶点序列 return commonVert; def intersection_seg(self, other): elist_1 = self.edge; elist_2 = other.edge; interEdge = set(); for i in range(len(elist_1)): if other.segInPolygon(elist_1[i].value()) == True: interEdge.add(elist_1[i]); for i in range(len(elist_2)): if self.segInPolygon(elist_2[i].value()) == True: interEdge.add(elist_2[i]); interEdge = list(interEdge); return interEdge;#</span>
<span style="font-size:18px;">#>>> 2020[][][][][SegLine([1.33, 3], [2, 3]), SegLine([2, 3], [6, 3])][][SegLine([6, -3], [6, 3]), SegLine([2, 3], [6, 3])][SegLine([6, -3], [6, 3])][][SegLine([4.29, 0.43], [6, 3]), SegLine([3.67, -0.5], [4.29, 0.43])][SegLine([8, -7], [8, 3]), SegLine([8, -7], [5.67, -3.5]), SegLine([6, -3], [6, 3]), SegLine([5.67, -3.5], [6, -3]), SegLine([8, 3], [6, 3.86]), SegLine([6, 3], [6, 3.86])][SegLine([2, 3], [6, 3]), SegLine([6, 3.86], [3.33, 5]), SegLine([2, 3], [3.33, 5]), SegLine([6, 3], [6, 3.86]), SegLine([6, -3], [6, 3])][SegLine([4.5, 0], [6, 3]), SegLine([6, -3], [6, 3]), SegLine([6, -3], [4.5, 0]), SegLine([2, 3], [6, 3])][SegLine([6, -3], [6, 3])][SegLine([4.5, 0], [6, 3]), SegLine([6, -3], [4.5, 0]), SegLine([6, -3], [6, 3])][SegLine([4.5, 0], [4.29, 0.43]), SegLine([4.29, 0.43], [6, 3]), SegLine([6, -3], [4.5, 0]), SegLine([6, -3], [6, 3])][SegLine([6, -3], [6, 3])][SegLine([6, -3], [6, 3])][SegLine([4.5, 0], [6, 3]), SegLine([6, -3], [4.5, 0]), SegLine([6, -3], [6, 3])][SegLine([4.5, 0], [6, 3]), SegLine([6, -3], [4, -1]), SegLine([4, -1], [4.5, 0]), SegLine([6, -3], [6, 3])]>>> def tmp15(): #两组路径 path1 = a7=[[[6, -3], [8, -7], [5.35, -3.97], [5.09, -3.68], [5.56, -3.33], [4, -1], [4.5, 0], [6, 3], [6, -3]], [[6, -3], [8, -7], [5.35, -3.97], [5.09, -3.68], [3.33, -1.67], [3.08, -1.38], [3.56, -0.67], [4, -1], [6, -3]], [[6, -3], [8, -7], [5.35, -3.97], [5.09, -3.68], [3.33, -1.67], [4, -1], [4.5, 0], [6, 3], [6, -3]], [[6, -3], [4.5, 0], [4.29, 0.43], [3.67, -0.5], [1.78, 2.33], [1.5, 1.5], [3.6, -0.6], [4, -1], [6, -3]], [[6, -3], [4.5, 0], [4.29, 0.43], [3.67, -0.5], [1.78, 2.33], [1.33, 3], [2, 3], [6, 3], [6, -3]], [[6, -3], [4.5, 0], [4.29, 0.43], [6, 3], [2, 3], [1.78, 2.33], [3.67, -0.5], [4, -1], [6, -3]], [[6, -3], [4.5, 0], [4, -1], [3.67, -0.5], [1.78, 2.33], [1.33, 3], [2, 3], [6, 3], [6, -3]], [[6, -3], [4.5, 0], [4, -1], [3.6, -0.6], [3.67, -0.5], [1.78, 2.33], [2, 3], [6, 3], [6, -3]], [[6, -3], [4.5, 0], [4, -1], [3.6, -0.6], [1.5, 1.5], [1.78, 2.33], [2, 3], [6, 3], [6, -3]], [[6, -3], [4.5, 0], [4, -1], [3.56, -0.67], [3.6, -0.6], [3.67, -0.5], [4.29, 0.43], [6, 3], [6, -3]], [[6, -3], [4, -1], [5.56, -3.33], [5.67, -3.5], [8, -7], [8, 3], [6, 3.86], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.67, -0.5], [1.78, 2.33], [2, 3], [3.33, 5], [6, 3.86], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.67, -0.5], [3.6, -0.6], [1.5, 1.5], [1.78, 2.33], [2, 3], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.6, -0.6], [3.67, -0.5], [1.78, 2.33], [1.33, 3], [2, 3], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.6, -0.6], [1.5, 1.5], [1.78, 2.33], [3.67, -0.5], [4.29, 0.43], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.6, -0.6], [1.5, 1.5], [1.78, 2.33], [1.33, 3], [2, 3], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.6, -0.6], [1.5, 1.5], [0, 3], [1.33, 3], [2, 3], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.56, -0.67], [3.6, -0.6], [3.67, -0.5], [1.78, 2.33], [2, 3], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.56, -0.67], [3.6, -0.6], [1.5, 1.5], [1.78, 2.33], [2, 3], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.56, -0.67], [1.33, 1], [1.5, 1.5], [1.78, 2.33], [2, 3], [6, 3], [6, -3]]] path2 = a17=[[[6, 9], [2.4, 5.4], [0.8, 3.8], [1.33, 3], [2, 3], [6, 3], [6, 3.86], [3.33, 5], [6, 9]], [[6, 9], [2.4, 5.4], [0.8, 3.8], [1.33, 3], [2, 3], [3.33, 5], [6, 3.86], [8, 3], [6, 9]], [[6, 9], [2.4, 5.4], [0.8, 3.8], [0, 3], [1.5, 1.5], [1.78, 2.33], [2, 3], [3.33, 5], [6, 9]], [[6, 9], [2.4, 5.4], [0.8, 3.8], [0, 3], [1.33, 3], [1.78, 2.33], [2, 3], [3.33, 5], [6, 9]], [[6, 9], [2.4, 5.4], [0.8, 3.8], [0, 3], [1.33, 3], [2, 3], [6, 3], [6, 3.86], [6, 9]], [[6, 9], [2.4, 5.4], [0.8, 3.8], [0, 3], [1.33, 3], [2, 3], [3.33, 5], [6, 3.86], [6, 9]], [[6, 9], [2.4, 5.4], [3.33, 5], [2, 3], [6, 3], [6, -3], [8, -7], [8, 3], [6, 9]], [[6, 9], [2.4, 5.4], [3.33, 5], [6, 3.86], [6, 3], [6, -3], [8, -7], [8, 3], [6, 9]], [[6, 9], [2.4, 5.4], [-6, 9], [-0.37, 2.56], [0, 3], [1.33, 3], [2, 3], [3.33, 5], [6, 9]], [[6, 9], [3.33, 5], [2, 3], [1.78, 2.33], [3.67, -0.5], [4.29, 0.43], [6, 3], [6, 3.86], [6, 9]], [[6, 9], [3.33, 5], [2, 3], [6, 3], [6, -3], [5.67, -3.5], [8, -7], [8, 3], [6, 9]], [[6, 9], [3.33, 5], [2, 3], [6, 3], [6, -3], [8, -7], [8, 3], [6, 3.86], [6, 9]], [[6, 9], [3.33, 5], [2, 3], [6, 3], [4.5, 0], [6, -3], [8, -7], [8, 3], [6, 9]], [[6, 9], [3.33, 5], [6, 3.86], [6, 3], [6, -3], [5.67, -3.5], [8, -7], [8, 3], [6, 9]], [[6, 9], [3.33, 5], [6, 3.86], [6, 3], [4.5, 0], [6, -3], [8, -7], [8, 3], [6, 9]], [[6, 9], [6, 3.86], [6, 3], [4.29, 0.43], [4.5, 0], [6, -3], [8, -7], [8, 3], [6, 9]], [[6, 9], [6, 3.86], [6, 3], [6, -3], [5.56, -3.33], [5.67, -3.5], [8, -7], [8, 3], [6, 9]], [[6, 9], [6, 3.86], [6, 3], [6, -3], [5.67, -3.5], [5.35, -3.97], [8, -7], [8, 3], [6, 9]], [[6, 9], [6, 3.86], [6, 3], [4.5, 0], [6, -3], [5.67, -3.5], [8, -7], [8, 3], [6, 9]], [[6, 9], [6, 3.86], [6, 3], [4.5, 0], [4, -1], [6, -3], [8, -7], [8, 3], [6, 9]]] len_ = min(len(path1), len(path2)); print(len_); interPath = []; for i in range(len_): poly1 = Polygon(path1[i]); poly2 = Polygon(path2[i]); interPath.append(poly1.intersection_seg(poly2)); print(len(interPath)); for i in range(len(interPath)): print(interPath[i]);#</span>
如果直接按线段来显示,肯定会得到满意的图像,当然,阿伟是选择画蛇添足了一下。想让它们生成路径。
<span style="font-size:18px;">#共处理路径(条): 20路径: []路径: []路径: []路径: []路径: [SegLine([1.33, 3], [2, 3]), SegLine([2, 3], [6, 3])]顶点数: 3连通: [[1.33, 3], [2, 3], [6, 3]]路径: []路径: [SegLine([2, 3], [6, 3]), SegLine([6, -3], [6, 3])]顶点数: 3连通: [[6, -3], [6, 3], [2, 3]]路径: [SegLine([6, -3], [6, 3])]路径: []路径: [SegLine([4.29, 0.43], [6, 3]), SegLine([3.67, -0.5], [4.29, 0.43])]顶点数: 3连通: [[3.67, -0.5], [4.29, 0.43], [6, 3]]路径: [SegLine([6, 3], [6, 3.86]), SegLine([5.67, -3.5], [6, -3]), SegLine([8, -7], [5.67, -3.5]), SegLine([8, 3], [6, 3.86]), SegLine([8, -7], [8, 3]), SegLine([6, -3], [6, 3])]顶点数: 6连通: [[8, -7], [5.67, -3.5], [6, -3], [6, 3], [6, 3.86], [8, 3], [8, -7]]路径: [SegLine([2, 3], [6, 3]), SegLine([6, 3.86], [3.33, 5]), SegLine([6, -3], [6, 3]), SegLine([6, 3], [6, 3.86]), SegLine([2, 3], [3.33, 5])]顶点数: 5连通: [[6, -3], [6, 3], [2, 3], [3.33, 5], [6, 3.86], [6, 3]]路径: [SegLine([6, -3], [4.5, 0]), SegLine([4.5, 0], [6, 3]), SegLine([2, 3], [6, 3]), SegLine([6, -3], [6, 3])]顶点数: 4连通: [[6, -3], [4.5, 0], [6, 3], [2, 3]]路径: [SegLine([6, -3], [6, 3])]路径: [SegLine([4.5, 0], [6, 3]), SegLine([6, -3], [4.5, 0]), SegLine([6, -3], [6, 3])]顶点数: 3连通: [[6, -3], [6, 3], [4.5, 0], [6, -3]]路径: [SegLine([4.29, 0.43], [6, 3]), SegLine([6, -3], [4.5, 0]), SegLine([4.5, 0], [4.29, 0.43]), SegLine([6, -3], [6, 3])]顶点数: 4连通: [[6, -3], [4.5, 0], [4.29, 0.43], [6, 3], [6, -3]]路径: [SegLine([6, -3], [6, 3])]路径: [SegLine([6, -3], [6, 3])]路径: [SegLine([4.5, 0], [6, 3]), SegLine([6, -3], [4.5, 0]), SegLine([6, -3], [6, 3])]顶点数: 3连通: [[6, -3], [6, 3], [4.5, 0], [6, -3]]路径: [SegLine([6, -3], [4, -1]), SegLine([4, -1], [4.5, 0]), SegLine([4.5, 0], [6, 3]), SegLine([6, -3], [6, 3])]顶点数: 4连通: [[6, -3], [4, -1], [4.5, 0], [6, 3], [6, -3]]***result:***[[], [], [], [], [[1.33, 3], [2, 3], [6, 3]], [], [[6, -3], [6, 3], [2, 3]], [[[6, -3], [6, 3]]], [], [[3.67, -0.5], [4.29, 0.43], [6, 3]], [[8, -7], [5.67, -3.5], [6, -3], [6, 3], [6, 3.86], [8, 3], [8, -7]], [[6, -3], [6, 3], [2, 3], [3.33, 5], [6, 3.86], [6, 3]], [[6, -3], [4.5, 0], [6, 3], [2, 3]], [[[6, -3], [6, 3]]], [[6, -3], [6, 3], [4.5, 0], [6, -3]], [[6, -3], [4.5, 0], [4.29, 0.43], [6, 3], [6, -3]], [[[6, -3], [6, 3]]], [[[6, -3], [6, 3]]], [[6, -3], [6, 3], [4.5, 0], [6, -3]], [[6, -3], [4, -1], [4.5, 0], [6, 3], [6, -3]]]#</span>
<span style="font-size:18px;">#def tmp15(): #两组路径 path1 = a7=[[[6, -3], [8, -7], [5.35, -3.97], [5.09, -3.68], [5.56, -3.33], [4, -1], [4.5, 0], [6, 3], [6, -3]], [[6, -3], [8, -7], [5.35, -3.97], [5.09, -3.68], [3.33, -1.67], [3.08, -1.38], [3.56, -0.67], [4, -1], [6, -3]], [[6, -3], [8, -7], [5.35, -3.97], [5.09, -3.68], [3.33, -1.67], [4, -1], [4.5, 0], [6, 3], [6, -3]], [[6, -3], [4.5, 0], [4.29, 0.43], [3.67, -0.5], [1.78, 2.33], [1.5, 1.5], [3.6, -0.6], [4, -1], [6, -3]], [[6, -3], [4.5, 0], [4.29, 0.43], [3.67, -0.5], [1.78, 2.33], [1.33, 3], [2, 3], [6, 3], [6, -3]], [[6, -3], [4.5, 0], [4.29, 0.43], [6, 3], [2, 3], [1.78, 2.33], [3.67, -0.5], [4, -1], [6, -3]], [[6, -3], [4.5, 0], [4, -1], [3.67, -0.5], [1.78, 2.33], [1.33, 3], [2, 3], [6, 3], [6, -3]], [[6, -3], [4.5, 0], [4, -1], [3.6, -0.6], [3.67, -0.5], [1.78, 2.33], [2, 3], [6, 3], [6, -3]], [[6, -3], [4.5, 0], [4, -1], [3.6, -0.6], [1.5, 1.5], [1.78, 2.33], [2, 3], [6, 3], [6, -3]], [[6, -3], [4.5, 0], [4, -1], [3.56, -0.67], [3.6, -0.6], [3.67, -0.5], [4.29, 0.43], [6, 3], [6, -3]], [[6, -3], [4, -1], [5.56, -3.33], [5.67, -3.5], [8, -7], [8, 3], [6, 3.86], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.67, -0.5], [1.78, 2.33], [2, 3], [3.33, 5], [6, 3.86], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.67, -0.5], [3.6, -0.6], [1.5, 1.5], [1.78, 2.33], [2, 3], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.6, -0.6], [3.67, -0.5], [1.78, 2.33], [1.33, 3], [2, 3], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.6, -0.6], [1.5, 1.5], [1.78, 2.33], [3.67, -0.5], [4.29, 0.43], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.6, -0.6], [1.5, 1.5], [1.78, 2.33], [1.33, 3], [2, 3], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.6, -0.6], [1.5, 1.5], [0, 3], [1.33, 3], [2, 3], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.56, -0.67], [3.6, -0.6], [3.67, -0.5], [1.78, 2.33], [2, 3], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.56, -0.67], [3.6, -0.6], [1.5, 1.5], [1.78, 2.33], [2, 3], [6, 3], [6, -3]], [[6, -3], [4, -1], [3.56, -0.67], [1.33, 1], [1.5, 1.5], [1.78, 2.33], [2, 3], [6, 3], [6, -3]]] path2 = a17=[[[6, 9], [2.4, 5.4], [0.8, 3.8], [1.33, 3], [2, 3], [6, 3], [6, 3.86], [3.33, 5], [6, 9]], [[6, 9], [2.4, 5.4], [0.8, 3.8], [1.33, 3], [2, 3], [3.33, 5], [6, 3.86], [8, 3], [6, 9]], [[6, 9], [2.4, 5.4], [0.8, 3.8], [0, 3], [1.5, 1.5], [1.78, 2.33], [2, 3], [3.33, 5], [6, 9]], [[6, 9], [2.4, 5.4], [0.8, 3.8], [0, 3], [1.33, 3], [1.78, 2.33], [2, 3], [3.33, 5], [6, 9]], [[6, 9], [2.4, 5.4], [0.8, 3.8], [0, 3], [1.33, 3], [2, 3], [6, 3], [6, 3.86], [6, 9]], [[6, 9], [2.4, 5.4], [0.8, 3.8], [0, 3], [1.33, 3], [2, 3], [3.33, 5], [6, 3.86], [6, 9]], [[6, 9], [2.4, 5.4], [3.33, 5], [2, 3], [6, 3], [6, -3], [8, -7], [8, 3], [6, 9]], [[6, 9], [2.4, 5.4], [3.33, 5], [6, 3.86], [6, 3], [6, -3], [8, -7], [8, 3], [6, 9]], [[6, 9], [2.4, 5.4], [-6, 9], [-0.37, 2.56], [0, 3], [1.33, 3], [2, 3], [3.33, 5], [6, 9]], [[6, 9], [3.33, 5], [2, 3], [1.78, 2.33], [3.67, -0.5], [4.29, 0.43], [6, 3], [6, 3.86], [6, 9]], [[6, 9], [3.33, 5], [2, 3], [6, 3], [6, -3], [5.67, -3.5], [8, -7], [8, 3], [6, 9]], [[6, 9], [3.33, 5], [2, 3], [6, 3], [6, -3], [8, -7], [8, 3], [6, 3.86], [6, 9]], [[6, 9], [3.33, 5], [2, 3], [6, 3], [4.5, 0], [6, -3], [8, -7], [8, 3], [6, 9]], [[6, 9], [3.33, 5], [6, 3.86], [6, 3], [6, -3], [5.67, -3.5], [8, -7], [8, 3], [6, 9]], [[6, 9], [3.33, 5], [6, 3.86], [6, 3], [4.5, 0], [6, -3], [8, -7], [8, 3], [6, 9]], [[6, 9], [6, 3.86], [6, 3], [4.29, 0.43], [4.5, 0], [6, -3], [8, -7], [8, 3], [6, 9]], [[6, 9], [6, 3.86], [6, 3], [6, -3], [5.56, -3.33], [5.67, -3.5], [8, -7], [8, 3], [6, 9]], [[6, 9], [6, 3.86], [6, 3], [6, -3], [5.67, -3.5], [5.35, -3.97], [8, -7], [8, 3], [6, 9]], [[6, 9], [6, 3.86], [6, 3], [4.5, 0], [6, -3], [5.67, -3.5], [8, -7], [8, 3], [6, 9]], [[6, 9], [6, 3.86], [6, 3], [4.5, 0], [4, -1], [6, -3], [8, -7], [8, 3], [6, 9]]] len_ = min(len(path1), len(path2)); print('共处理路径(条):', len_); interPath = []; result = []; for i in range(len_): poly1 = Polygon(path1[i]); poly2 = Polygon(path2[i]); interPath.append(poly1.intersection_seg(poly2)); for i in range(len(interPath)): print('路径:',interPath[i]); len_path = len(interPath[i]); if (len_path == 0): result.append(interPath[i]); elif (len_path < 2): #路径中包含的线段数小于2 print('No:', i); val = []; for j in range(len_path): val_ = interPath[i][j].value(); val.append(val_[0]); val.append(val_[1]); result.append(val); print(val); else: result.append(connectSeg(interPath[i])); print('***result:***'); print(result); #处理每个顶点最多连接两条线段的#线段集的路径连接问题。def connectSeg(seg): #seg = [SegLine([8, -7], [8, 3]), SegLine([8, -7], [5.67, -3.5]), SegLine([6, -3], [6, 3]), SegLine([5.67, -3.5], [6, -3]), SegLine([8, 3], [6, 3.86]), SegLine([6, 3], [6, 3.86])]; #去除重复线段 seg = list(set(seg)); len_seg = len(seg); d = dict(); path = []; #构造顶点集 vertex = set(); for i in range(len_seg): seg_ = seg[i].value(); vertex.add(Point(seg_[0])); vertex.add(Point(seg_[1])); print('顶点数:', len(vertex)); #这里的排序相当重要,一定要从所有顶点中最小的那个开始找 vertex = sorted(list(vertex)); len_vert = len(vertex); #构造边集字典 for i in range(len_vert): d[vertex[i]] = []; for i in range(len_seg): seg_ = seg[i].value(); d[Point(seg_[0])].append(Point(seg_[1])); d[Point(seg_[1])].append(Point(seg_[0])); #选择起始点 start = curVert = vertex[0]; path.append(curVert); while len_vert > 0: #从字典中移除相应线段的记录 if (len(d[curVert]) == 0): break; choice = d[curVert].pop(0); d[choice].remove(curVert); #处理下一个点 curVert = choice; if (curVert in vertex): vertex.remove(curVert); path.append(curVert); len_vert = len(vertex); else: path.append(curVert); break; for i in range(len(path)): path[i] = path[i].value(); print('连通:', path); return path;#</span>
最后这张图的点7和点12本来应该连接成区域的,这里是暴露出了路径连通算法的缺陷,这个在上节提过了。
但如果严格认为两个多边形共用的边不是交集,这张图的结果就会正确,当然,上一张图就没有交集了。
还是根据需要的应用场景去选择好了。
本节到此结束,欲知后事如何,请看下回分解。
0 0
- [从头学数学] 第273节 [计算几何] 多边形求交集
- [从头学数学] 第269节 [计算几何] 点在多边形内
- [从头学数学] 第275节 [计算几何] 多边形顶点的五种类型
- [从头学数学] 第277节 [计算几何] 任意多边形的内角
- [从头学数学] 第278节 [计算几何] 多边形周长和面积
- [从头学数学] 第283节 [计算几何] 多边形的单调块划分
- [从头学数学] 第284节 [计算几何] 生成随机多边形
- [从头学数学] 第286节 [计算几何] 多边形的布尔运算(上)
- [从头学数学] 第287节 [计算几何] 多边形的布尔运算(中)
- [从头学数学] 第288节 [计算几何] 多边形的布尔运算(下)
- [从头学数学] 第265节 [计算几何] 多线段求交点(扫描线法)
- [从头学数学] 第266节 [计算几何] 多线段求交点
- [从头学数学] 第240节 计算几何 整装待发
- [从头学数学] 第263节 [计算几何] 线段生成
- [从头学数学] 第267节 [计算几何] 路径规划
- [从头学数学] 第268节 [计算几何] 环形路径
- [从头学数学] 第271节 [计算几何] 凸包
- [从头学数学] 第274节 [计算几何] 顶点树
- 英语学习
- Java 之浅复制和深复制
- Heptavintimal Encoding of Ternary Values
- 数据类型和运算符
- c编程的经验分享1—一个标点符号引发的血案
- [从头学数学] 第273节 [计算几何] 多边形求交集
- ASP.NET MVC 后台 URL 对象转换接收
- TerSCII: Ternary Standard Code for Information Interchange
- 示波器分析UART串口数据
- MySQL助理配置
- TabLayout的用法
- 位操作基础篇之位操作全面总结
- Ternary Number Systems
- 前端人学java--2