python - 如何获取距引用点特定距离范围内的一系列随机点并生成xyz坐标

标签 python

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

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

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

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

最佳答案

如果你想伪随机定义点,你可以使用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

关于python - 如何获取距引用点特定距离范围内的一系列随机点并生成xyz坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18670974/

相关文章:

python - 从文本中分割数字

使用 tkinter 的 Python 程序在 Windows 10 中立即关闭

python - 如何将字典中的值添加到另一本字典中?

python - EOFError : marshal data too short

python - Pandas:如何在一系列列中找到第一个有效列

python - 广度优先搜索 : how to get currency exchange?

python - 在Python中编辑字典

python - 在文本框中输入时的功能?

python - 什么是卡住的 Python 模块?

python - 加入两个列表,其中列表 "A"的最后一个元素和列表 "B"的第一个元素连接在一起