如果要伪随机定义点,可以使用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
尝试使用matplotlib对其进行测试:
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()
结果如下:
![绘图结果](https://i.stack.imgur.com/c7Hzn.png)