2016-12-26 58 views
1

我使用如何比較在python兩個3D座標

#vector 
vec=[(.033,-.22,.98),(.5,-.9,.0029),(-.77,-.01,-.092),(.5,.2,.0029)] 

#grid 
x1 = np.linspace(-1,1,10) 
y1 = np.linspace(-1,1,10) 
z1 = np.linspace(-1,1,10) 

林這段代碼,這顯然是錯誤的,因爲它這需要在3D網格要檢查上谷三維坐​​標的向量座標只是比較第一x座標,而不是y的其餘,Z

ctr=0 
for v in vec: 
    for i in x1: 
     for j in y1: 
      for k in z1: 
       if ctr==0: 
        temp=(i,j,k) 
        ctr+=1 
        continue 
       else: 
        #print temp, "to" ,i,j 
        temp2=(i,j,k) 
        if temp<=v<=temp2: 
         print "low,high",temp, temp2 
        temp=(i,j,k) 
        ctr+=1 
===WRONG OUTPUT====== 
low,high (-0.052631578947368474, 1.0, 1.0) (0.052631578947368363, -1.0, -1.0) 
low,high (0.47368421052631571, 1.0, 1.0) (0.57894736842105265, -1.0, -1.0) 
low,high (-0.78947368421052633, 1.0, 1.0) (-0.68421052631578949, -1.0, -1.0) 
low,high (0.47368421052631571, 1.0, 1.0) (0.57894736842105265, -1.0, -1.0) 

伊夫粘貼二維網格這裏(3D將遵循相同的邏輯)。所以我們開始以連續順序從單元格(0,0)迭代網格。正如你所看到的較低&更高的座標點v enter image description here

+0

你能更清楚地確定你的上限值是什麼意思?起源距離? 2個座標之間的最大距離是多少? –

+0

@jeffcarey添加了描述圖片 – vinita

回答

1

試試這個

for v in vec: 
    #for i in grid: 
    ctr=0 
    i=grid[0] 
    while(np.float64(v[0])>=i[0]): 
     ctr+=1 
     #print "in while" 
     i=grid[ctr] 
    #print "1st ctr",ctr, 
    c=0 
    high=200-(ctr%200) # Takes care that you find the limit within that line itself 
    while(np.float64(v[1])>=i[1] and c<high): 
     #print v, (i) 
     ctr+=1 
     i=grid[ctr] 
     c+=1 
    #print "2nd ctr",ctr, 
    print ctr,v,grid[ctr]