如何比较python中的两个3d坐标

时间:2016-12-26 06:58:57

标签: python multidimensional-array coordinate-systems

我有一个3d坐标向量,需要在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)

我使用的代码显然是错误的,因为它只是比较第一个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) 

我在这里粘贴了一个2d网格(3d将遵循相同的逻辑)。因此,我们开始按顺序从单元格(0,0)迭代网格。你可以看到较低的&amp;点v的高坐标 enter image description here

1 个答案:

答案 0 :(得分: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]