- 积分
- 2220
- 明经币
- 个
- 注册时间
- 2021-11-14
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 landsat99 于 2023-2-7 09:15 编辑
// 分布于正交栅格网的散点集合(允许有内部空洞),生成正交点集合的外轮廓线。
说明:
1. 此算法分区施加右手法则,贴合正交外轮廓。
2. 对任意散点的集合,外轮廓线不唯一。 需附加约束条件,比如面积最大、 轮廓线周长最大、最小等。
3. Python演示算法及图形说明。
4. 此正交算法 为纯原创代码,未参考其它几何算法集。不严谨之处 多多交流指正。
- # RecVec_grid.py
- import matplotlib.pyplot as plt
- import GenList as Gen
- import MaxP as MP
- def Direc(UpDown, A, B):
- alpha = 360 + 10
- if UpDown == 1: # 象限号
- if B[1] == A[1] and B[0] > A[0]:
- alpha = 0 #
- elif B[1] == A[1] and B[0] < A[0]:
- alpha = 180
- elif B[0] == A[0] and B[1] > A[1]:
- alpha = 90
- elif B[0] == A[0] and B[1] < A[1]:
- alpha = 270
- else:
- pass
- if UpDown == 2: # 象限号
- if B[0] == A[0] and B[1] > A[1]:
- alpha = 0
- elif B[0] == A[0] and B[1] < A[1]:
- alpha = 180
- elif B[1] == A[1] and B[0] > A[0]:
- alpha = 270
- elif B[1] == A[1] and B[0] < A[0]:
- alpha = 90
- else:
- pass
- if UpDown == 3: # 象限号
- if B[1] == A[1] and B[0] < A[0]:
- alpha = 0
- elif B[1] == A[1] and B[0] > A[0]:
- alpha = 180
- elif B[0] == A[0] and B[1] < A[1]:
- alpha = 90
- elif B[0] == A[0] and B[1] > A[1]:
- alpha = 270
- else:
- pass
- if UpDown == 4: # 象限号
- if B[0] == A[0] and B[1] < A[1]:
- alpha = 0
- elif B[0] == A[0] and B[1] > A[1]:
- alpha = 180
- elif B[1] == A[1] and B[0] < A[0]:
- alpha = 270
- elif B[1] == A[1] and B[0] > A[0]:
- alpha = 90
- else:
- pass
- return alpha
- def Dist(A, B):
- return (B[1] - A[1])**2 + (B[0] - A[0])**2
- def NextPoi(UpDown, Poi, PList):
- Dire = 360 + 10
- Unit = 1
- for id, p in enumerate(PList):
- if Direc(UpDown, Poi, p) <= Dire and Dist(Poi, p) == Unit:
- Dire = Direc(UpDown, Poi, p)
- dis = Dist(Poi, p)
- Num = id
- return PList[Num]
- def plot2D(x, y, x_out, y_out):
- plt.scatter(x, y) # 画散点图
- plt.plot(x_out, y_out) # 画连线图
- plt.show()
- if __name__ == '__main__':
- Points = Gen.GenList()
- XMaxPoi, Num = MP.XMaxPoint(Points)
- YMaxPoi, Num = MP.YMaxPoint(Points)
- XMinPoi, Num = MP.XMinPoint(Points)
- YMinPoi, Num = MP.YMinPoint(Points)
- PPs = Points.copy()
- PPs.remove(XMaxPoi)
- Poi_A = XMaxPoi
- RList = [XMaxPoi]
- Poi_B = []
- while Poi_B != YMaxPoi:
- Poi_B = NextPoi(1, Poi_A, PPs)
- RList.append(Poi_B)
- PPs.remove(Poi_B)
- Poi_A = Poi_B
- Poi_A = YMaxPoi
- Poi_B = []
- while Poi_B != XMinPoi:
- Poi_B = NextPoi(2, Poi_A, PPs)
- RList.append(Poi_B)
- PPs.remove(Poi_B)
- Poi_A = Poi_B
- Poi_A = XMinPoi
- Poi_B = []
- while Poi_B != YMinPoi:
- Poi_B = NextPoi(3, Poi_A, PPs)
- RList.append(Poi_B)
- PPs.remove(Poi_B)
- Poi_A = Poi_B
- Poi_A = YMinPoi
- Poi_B = []
- PPs = [XMaxPoi] + PPs # 加回第一点
- while Poi_B != XMaxPoi:
- Poi_B = NextPoi(4, Poi_A, PPs)
- RList.append(Poi_B)
- PPs.remove(Poi_B)
- Poi_A = Poi_B
- print(RList)
- XList = [i[0] for i in Points]
- YList = [i[1] for i in Points]
- X_OutList = [i[0] for i in RList]
- Y_OutList = [i[1] for i in RList]
- plot2D(XList, YList, X_OutList, Y_OutList)
- # MaxP.py
- def XMaxPoint(PList):
- Xmax = PList[0][0]
- Ymax = PList[0][1]
- for id, poi in enumerate(PList):
- if poi[0] > Xmax:
- Xmax = poi[0]
- Ymax = poi[1]
- Num = id
- if poi[0] >= Xmax and poi[1] > Ymax:
- Xmax = poi[0]
- Num = id
- return PList[Num], Num
- def YMaxPoint(PList):
- Xmax = PList[0][0]
- Ymax = PList[0][1]
- for id, poi in enumerate(PList):
- if poi[1] > Ymax:
- Ymax = poi[1]
- Xmax = poi[0]
- Num = id
- if poi[1] >= Ymax and poi[0] < Xmax:
- Ymax = poi[1]
- Num = id
- return PList[Num], Num
- def XMinPoint(PList):
- Xmin = PList[0][0]
- Ymin = PList[0][1]
- for id, poi in enumerate(PList):
- if poi[0] < Xmin:
- Xmin = poi[0]
- Ymin = poi[1]
- Num = id
- if poi[0] <= Xmin and poi[1] < Ymin:
- Xmin = poi[0]
- Num = id
- return PList[Num], Num
- def YMinPoint(PList):
- Xmin = PList[0][0]
- Ymin = PList[0][1]
- for id, poi in enumerate(PList):
- if poi[1] < Ymin:
- Ymin = poi[1]
- Xmin = poi[0]
- Num = id
- if poi[1] <= Ymin and poi[0] > Xmin:
- Ymin = poi[1]
- Num = id
- return PList[Num], Num
- # GenList.py
- def GenList():
- Points = []
- for i in range(6, 9):
- for j in range(14, 17):
- Points.append([i, j])
- for i in range(4, 12):
- for j in range(12, 14):
- Points.append([i, j])
- for i in range(5, 10):
- for j in range(10, 12):
- Points.append([i, j])
- for i in range(2, 13):
- for j in range(6, 10):
- Points.append([i, j])
- for i in range(4, 13):
- for j in range(4, 6):
- Points.append([i, j])
- for i in range(3, 8):
- for j in range(0, 4):
- Points.append([i, j])
- for i in range(9, 12):
- for j in range(2, 5):
- Points.append([i, j])
- return Points
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
评分
-
查看全部评分
|