Find the closest point in geometry from other point to reform or sort the unordered coordinates in geometry in Python

Viewed 34

I am working on the task in which I have to find the nearest (closest) point from other point. The reason behind this problem is I want to reform (sort) the coordinate number in my geometry such a way that, every consecuttive nodes placed next to each other.

For that, I started to check the first point which is nearest to my origin (0, 0). I have list of coordinates as below,

X = [2, 4, 4, 2, 3, 2, 4, 3, 1, 3, 4, 3, 1, 2, 0, 3, 4, 2, 0]
Y = [3, 4, 2, 1, 3, 2, 1, 0, 0, 2, 3, 4, 1, 4, 0, 1, 0, 0, 1]

len(X2_coordinate)
19

and Origin is,

origin_x = 0
origin_y = 0

How can find the nearest point from origin??

I have written below code to find the distance between origin to all other points (mentioned in X and Y list) But I got 18 number of values (Distance values) which should be 19. Because I am finding a distance from origin to all other points stored in a list of X and Y.

dist_from_ori = []
i = 0
for i in range(len(X)): 
    
    # Finding a distance between two coordinates (nodes) in first dataframe
    # General formula for this: square root of [(x2 - x1)^2 + (y2 - y1)^2] where x1 and y1 will be constant and second coordinate will vary from x2 to x15
    new_list = np.sqrt((X[i] - origin_x)**2 + (Y[i] - origin_y)**2)
    dist_from_ori.append(new_list)
        
dist_from_ori = [round(num,2) for num in dist_from_ori]       # rounding a obtained values in 'nodal_dist_df1' by 2 decimals 

The result I got is,

dist_from_ori  = [3.61, 5.66, 4.47, 2.24, 4.24, 2.83, 4.12, 3.0, 1.0, 3.61,  5.0, 5.0, 1.41, 4.47, 0.0, 3.16, 4.0, 2.0]

Kindly guide me what should I do?

0 Answers
Related