3

我必须生成一个不同的点及其 xyz 坐标,然后计算距离。
可以说我想在所有方向上从 2.5 厘米的点 a 创建随机点坐标。这样我就可以计算到所有生成点(红色)的相互距离和角度,
我想删除冗余点以及所有不满足我的标准且位置相同的点。

![在此处输入图像描述][1]

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

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

4

2 回答 2

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()

结果如下: 绘图结果

于 2013-09-07T08:31:33.790 回答
1

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

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

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

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

于 2013-09-08T18:25:01.797 回答