如何在距参考点的特定距离范围内获得一系列随机点并生成xyz坐标

时间:2013-09-07 07:35:05

标签: python

我必须生成各种点及其xyz坐标,然后计算距离      让我们说我希望从所有方向上的2.5厘米处创建随机点坐标。这样我就可以计算出所有生成点(红色)的相互距离和角度 我想删除冗余点和所有那些不符合我的标准并且也具有相同位置的点。

![在此处输入图片说明] [1]

例如,我知道两个点的坐标 a ( - 10,12,2)和 b ( - 9,11,5)。 a b 之间的距离为5厘米 问题是:如何生成红点的坐标。我知道如何计算距离和角度。到目前为止,我已经尝试了以下计算:

我无法随机定义点数。 我找到了一些不起作用的解决方案。 任何帮助,将不胜感激。

2 个答案:

答案 0 :(得分:4)

如果要随机定义伪点,可以使用random.randrange

from random import randrange as rd
from math import sqrt
ptBlu=[11,12,13] #example of blu point
ptRedx=ptBlu[0]+rd(-10,10,1) #x coordinate of a red point(rd is the function stand for random.randrange)
ptRedy=ptBlu[1]+rd(-10,10,1) #y coordinate of a red point
ptRedz=ptBlu[2]+rd(-10,10,1) #z coordinate of a red point
ptRed=[ptRedx,ptRedy,ptRedz] #list with the x,y,z coordinates

如果要创建一系列距离大于特定点的点,从而避免创建具有相同坐标的点。你必须做更精细的事情。这是2D平面作为绘图的示例。对于3D示例,只需添加第三个坐标并调整公式。

redptlist=[] #inizialize a void lists for red point coordinates
xredptlist=[] 
yredptlist=[]
pointcounter=0 #initizlize counter for the while loop
mindist=2.5#set the minimum euclidean distance beyond you want to create the points
maxdist=12#set the maximum euclidean distance redpoint can have from blu point
maxc=int(sqrt((maxdist**2)/2)) #from the euclidean distance formula you can get the max      coordinate
while True: #create a potentailly infinite loop! pay attention!
     if pointcounter<20: #set the number of point you want to add (in this case 20)
         x_RedPtshift=rd(-maxc,maxc,1) #x shift of a red point 
         y_RedPtshift=rd(-maxc,maxc,1) #y shift of a red point
         if sqrt(x_RedPtshift**2+y_RedPtshift**2)>mindist: #if the point create go beyond the minimum distance
             ptRedx=ptBlu[0]+ x_RedPtshift#x coordinate of a red point
             ptRedy=ptBlu[1]+ y_RedPtshift #y coordinate of a red point
             ptRed=[ptRedx,ptRedy] #list with the x,y,z coordinates
             if ptRed not in redptlist: #avoid to create red point with the same coordinates
                 redptlist.append(ptRed) # add to a list with this notation [x1,y1],[x2,y2]
                 xredptlist.append(ptRedx) # add to a list with this notation [x1,x2,x3...] for plotting
                 yredptlist.append(ptRedy) # add to a list with this notation [y1,y2,y3...] for plotting
                 pointcounter+=1 #add one to the counter of how many points you have in your list 
     else: #when pointcounter reach the number of points you want the while cicle ends
         break

尝试使用进行测试:

import matplotlib.pyplot as plt
plt.plot(ptBlu[0],ptBlu[1],'bo')#plot blu point
plt.plot(xredptlist, yredptlist, 'ro')#plot red points
minCircle=plt.Circle((ptBlu[0],ptBlu[1],1),mindist,color='b',fill=False)#draw circle with min distance
maxCircle=plt.Circle((ptBlu[0],ptBlu[1],1),maxdist,color='r',fill=False)
fig = plt.gcf()
fig.gca().add_artist(minCircle)
fig.gca().add_artist(maxCircle)
plt.axis([-3, 25, -1, 25])
plt.axes().set_aspect('equal', 'box')
plt.grid(True)
plt.show()

结果如下: plotting result

答案 1 :(得分:1)

通常,您可以通过两种方式填充积分:

1)使用random为解决方案外边界内的点创建坐标。如果给定的随机点超出最大值或在内部限制范围内。

2)你可以使用极坐标来做到这一点:在内边界和外边界之间产生一个随机距离和一个偏航旋转。在3D中,你必须使用两个旋转,一个用于偏航,另一个用于俯仰。这避免了拒绝点的需要。

您可以通过生成原点(0,0,0)周围的圆(或球体)中的所有点而不是原位来简化​​两者的代码。他们将整个点移动到正确的蓝色圆圈位置,将其位置添加到每个点的位置。

相关问题