0

我一直在研究电子结构映射。我使用 python 编写代码,其中我有一个类似多边形的形状(不完全是多边形),并且我有一组 x 和 y 坐标。首先,我必须检查这些点是在多边形内部还是外部。我几乎可以做到。现在的问题是我想计算多边形与多边形外的点之间的距离。任何人都可以帮助我吗?

这是我的代码:

注意:c14list.dat 是我从中导入点集的文件,格式为(<Element name> <x-axis><y-axis>)

laves 是一种多边形


import numpy as np

def maverick(x,y,cir):

   if (x,y) in cir: return "In Convex Hull"


   for i in range(len(cir)):
      p1 = None
      p2 = None
      if i==0:
         p1 = cir[0]
         p2 = cir[1]
      else:
         p1 = cir[i-1]
         p2 = cir[i]
      if p1[1] == p2[1] and p1[1] == y and x > min(p1[0], p2[0]) and x < max(p1[0], p2[0]):
         return "In Convex Hull"

   n = len(cir)
   inside = False

   p1x,p1y = cir[0]
   for i in range(n+1):
      p2x,p2y = cir[i % n]
      if y > min(p1y,p2y):
         if y <= max(p1y,p2y):
            if x <= max(p1x,p2x):
               if p1y != p2y:
                  xints = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
               if p1x == p2x or x <= xints:
                  inside = not inside
      p1x,p1y = p2x,p2y

   if inside: return "In Convex Hull"
   else: return "out of Convex Hull"

laves = [(4.667,0.22701578), (5.667,0.33127494),
(6.283,0.32687551), (11.333,0.26443006),(9.667,0.11122194),(9.333,0.1067382),(5.667,0.09125353),(5,0.1141427),(4.667,0.22701578)]

f = open('c14list.dat','r')
label_arr=[]
x_arr=[]
y_arr=[]

for i in range(69):
 a=f.readline()
 a=str(a).split('\t')
 lent = len(a)
 label_arr.append(a[0])
 x_arr.append(float(a[1]))
 y_arr.append(float(a[lent-1][0:10]))


for i in range(69):
 label=label_arr[i]
 x=x_arr[i]
 y=y_arr[i]
 print label,'\t',x,'\t',y,'\t',maverick(x,y,laves) 
4

0 回答 0