dimanche 7 mars 2021

Generating random points in a box

I want to generate random points in a box (a=0.2m, b=0.2m, c=1m). This points should have random distance between each other but minimum distance between two points is should be 0.03m, for this I used random.choice. When I run my code it generates random points but distance management is so wrong. Also my float converting approximation is terrible because I don't want to change random values which I generate before but I couldn't find any other solution. I'm open to suggestions.

Images

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, 21,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)
    



Aucun commentaire:

Enregistrer un commentaire