在框中生成随机点

问题描述

我想在一个盒子中生成随机点(a=0.2m,b=0.2m,c=1m)。这些点之间应该有随机距离,但两点之间的最小距离应该是 0.03m,为此我使用了 random.choice。当我运行我的代码时,它会生成随机点,但距离管理是如此错误。此外,我的浮点转换近似值很糟糕,因为我不想更改之前生成的随机值,但找不到任何其他解决方案。我愿意接受建议。

图片

graph1 graph2

import random
import matplotlib.pyplot as plt

# BOX a = 0.2m b=0.2m h=1m
    
save = 0 #for saving 3 different plot.
for k in range(3):
    pointsX = [] #information of x coordinates of points
    pointsY = [] #information of y coordinates of points
    pointsZ = [] #information of z coordinates of points
    for i in range(100): #number of the points
        a = random.uniform(0.0,0.00001) #for the numbers generated below are float. 
        
        x = random.choice(range(3,21,3)) #random coordinates for x
        x1 = x/100 + a
        pointsX.append(x1)
        
        y = random.choice(range(3,3)) #random coordinates for y
        y1 = y/100 + a
        pointsY.append(y1)
        
        z = random.choice(range(3,98,3)) #random coordinates for z
        z1 = z/100 + a
        pointsZ.append(z1)
        
    new_pointsX = list(set(pointsX)) # deleting if there is a duplicates
    new_pointsY = list(set(pointsY))
    new_pointsZ = list(set(pointsZ))
    
    # i wonder max and min values it is or not between borders.
    print("X-Min",min(new_pointsX)) 
    print("X-Max",max(new_pointsX))
    print("Y-Min",min(new_pointsY))
    print("Y-Max",max(new_pointsY))
    print("Z-Min",min(new_pointsZ))
    print("Z-Max",max(new_pointsZ))
    if max(new_pointsX) >= 0.2 or max(new_pointsY) >= 0.2:
        print("MAX VALUE GREATER THAN 0.2") 
    if max(new_pointsZ) >= 0.97:
        print("MAX VALUE GREATER THAN 0.97")
    
    #3D graph  
    fig = plt.figure(figsize=(18,9))
    ax = plt.axes(projection='3d')
    ax.set_xlim([0,0.2])
    ax.set_ylim([0,0.2])
    ax.set_zlim([0,1])
    ax.set_title('title',fontsize=18)
    ax.set_xlabel('X',fontsize=14)
    ax.set_ylabel('Y',fontsize=14)
    ax.set_zlabel('Z',fontsize=14)
    ax.scatter3D(new_pointsX,new_pointsY,new_pointsZ);
    
    save += 1
    plt.savefig("graph" + str(save) + ".png",dpi=900)
    

解决方法

@user3431635 的评论中所述,您可以在将新点附加到列表之前检查每个点与所有先前的点。我会这样做:

import random
import numpy as np
import matplotlib.pyplot as plt

plt.close("all")

a = 0.2         # x bound
b = 0.2         # y bound
c = 1.0         # z bound
N = 1000        # number of points

def distance(p,points,min_distance):
    """
    Determines if any points in the list are less than the minimum specified 
    distance apart.

    Parameters
    ----------
    p : tuple
        `(x,y,z)` point.
    points : ndarray
        Array of points to check against. `x,z` points are columnwise.
    min_distance : float
        Minimum allowable distance between any two points.

    Returns
    -------
    bool
        True if point `p` is at least `min_distance` from all points in `points`.

    """
    distances = np.sqrt(np.sum((p+points)**2,axis=1))
    distances = np.where(distances < min_distance)
    return distances[0].size < 1

points = np.array([])       # x,z columnwise
while points.shape[0] < 1000:
    x = random.choice(np.linspace(0,a,100000))
    y = random.choice(np.linspace(0,b,100000))
    z = random.choice(np.linspace(0,c,100000))
    p = (x,z)
    if len(points) == 0:                # add first point blindly
        points = np.array([p])
    elif distance(p,0.03):     # ensure the minimum distance is met
        points = np.vstack((points,p))
        
fig = plt.figure(figsize=(18,9))
ax = plt.axes(projection='3d')
ax.set_xlim([0,a])
ax.set_ylim([0,b])
ax.set_zlim([0,c])
ax.set_title('title',fontsize=18)
ax.set_xlabel('X',fontsize=14)
ax.set_ylabel('Y',fontsize=14)
ax.set_zlabel('Z',fontsize=14)
ax.scatter(points[:,0],points[:,1],2])

请注意,这可能不是您要寻找的随机性。我已经编写了它来获取 x、y 和 z 值的范围并将其拆分为 100000 个增量;然后从这些值中选择一个新的 x、y 或 z 点。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...