0

I'm trying to use a Euclidean formula. I need x2 and y2 to be the inner [1] and [2] indeces of all the lists nested in blue_points respectively, but it doesn't do that. I am trying to make the Euclidean distance compute points from blue_points with the point1 list, then determine if it is less than or greater than/equal to so the program can return it as True or False respectively.

import math

# [ID, X coordinate, Y coordinate]
blue_points = [[30, 536254.99137, 3659453.06343],
[33, 536721.584912, 3659162.97207],
[50, 535807.099324, 3659576.92825],
[112, 536827.131371, 3657913.01245],
[117, 536473.254082, 3659433.57702],
[120, 536196.9844, 3658713.72722],
[127, 536387.547701, 3658527.70015],
[133, 537397.838429, 3659554.48657],
[144, 537715.931243, 3658625.59997],
[164, 538367.648437, 3658867.34288],
[172, 537112.662366, 3657921.28957],
[173, 536418.315024, 3658715.47946],
[209, 538096.28422, 3658514.93514],
[211, 538077.87716, 3658138.39337],
[223, 536220.396985, 3659243.54161],
[242, 536102.087002, 3658703.61054],
[244, 536968.755886, 3659409.42857],
[246, 535996.903591, 3658705.08691],
[275, 538078.165429, 3659022.35547],
[303, 535999.885405, 3658521.91524]]

# [ID, X coordinate,Y coordinate]
point1 = [1, 1073706.744,3658967.925]

neighbor_points = [] # Empty list to store values.
for point2 in blue_points:
    if (math.sqrt(((point2[1] - point1[1]) ** 2) + ((point2[2] - point1[2]) ** 2))): # Euclidean distance formula.
        neighbor_points.append(point2) # Add points to empty list.
        print(point2[1:3]) # Print to check if indeces are correct.

x1 = point1[1]
y1 = point1[2]
x2 = blue_points[1]
y2 = blue_points[2]
                
distance = math.sqrt((neighbor_points[1] - point1[1]) ** 2) + ((neighbor_points[2] - point1[2]) ** 2)

def identify_neighbor(point1, point2):
    
    if (distance < 536000):
        print(distance)
        print("True.")
        return True
    else:
        print(distance)
        print("False")
        return False

neighbors = []
for point in blue_points:
    if (math.sqrt(((neighbor_points[1] - point1[1]) ** 2) + ((neighbor_points[2] - point1[2]) ** 2))):
        neighbors.append(point)
        print(point[0])
        identify_neighbor(point1, point2)
Tess
  • 1
  • 1
    Does this answer your question? [How to index into nested lists?](https://stackoverflow.com/questions/34636056/how-to-index-into-nested-lists) – bench Oct 08 '20 at 02:16

2 Answers2

0

Okey,you have a weird code. But only one problem.

To access a nested array (a matrix) using python array you do mat[i][j] for access de element i,j.

So for your code run, you only need to change your distance function

distance = math.sqrt((neighbor_points[1] - point1[1]) ** 2) + ((neighbor_points[2] - point1[2]) ** 2)

to

distance = math.sqrt((neighbor_points[0][1] - point1[1]) ** 2) + ((neighbor_points[0][2] - point1[2]) ** 2)

For further implementation i recommend you that create a function that does that

def euclidean_distance(p1, p2):
    return math.sqrt((p2[1] - p1[1]) ** 2) + ((p2[2] - p1[2]) ** 2)

and you can call it like this

distance = euclidean_distance(neighbor_points[0], point1)

For more information see this or this or this or the official documentation

bench
  • 136
  • 8
0

If I'm understanding the goal correctly, you want to find all the points in blue_points that are neighbors of point1. For that, you loop through the blue_points and find the distance for each point then collect the neighbor points in a list.

Try this code:

import math

# [ID, X coordinate, Y coordinate]
blue_points = [
[30, 536254.99137, 3659453.06343],
[33, 536721.584912, 3659162.97207],
[50, 535807.099324, 3659576.92825],
[112, 536827.131371, 3657913.01245],
[117, 536473.254082, 3659433.57702],
[120, 536196.9844, 3658713.72722],
[127, 536387.547701, 3658527.70015],
[133, 537397.838429, 3659554.48657],
[144, 537715.931243, 3658625.59997],
[164, 538367.648437, 3658867.34288],
[172, 537112.662366, 3657921.28957],
[173, 536418.315024, 3658715.47946],
[209, 538096.28422, 3658514.93514],
[211, 538077.87716, 3658138.39337],
[223, 536220.396985, 3659243.54161],
[242, 536102.087002, 3658703.61054],
[244, 536968.755886, 3659409.42857],
[246, 535996.903591, 3658705.08691],
[275, 538078.165429, 3659022.35547],
[303, 535999.885405, 3658521.91524]]

# [ID, X coordinate,Y coordinate]
point1 = [1, 1073706.744,3658967.925]

neighbor_points = []
outer_points = []  # not neighbor

for pt in blue_points:
   distance = math.sqrt(((pt[1] - point1[1]) ** 2) + ((pt[2] - point1[2]) ** 2))
   if (distance < 536000):
        neighbor_points.append(pt)
   else:
        outer_points.append(pt)

print('neighbor_points')
for pt in neighbor_points:
   print(pt)
print()
print('outer_points')
for pt in outer_points:
   print(pt)

Output

neighbor_points
[144, 537715.931243, 3658625.59997]
[164, 538367.648437, 3658867.34288]
[209, 538096.28422, 3658514.93514]
[211, 538077.87716, 3658138.39337]
[275, 538078.165429, 3659022.35547]

outer_points
[30, 536254.99137, 3659453.06343]
[33, 536721.584912, 3659162.97207]
[50, 535807.099324, 3659576.92825]
[112, 536827.131371, 3657913.01245]
[117, 536473.254082, 3659433.57702]
[120, 536196.9844, 3658713.72722]
[127, 536387.547701, 3658527.70015]
[133, 537397.838429, 3659554.48657]
[172, 537112.662366, 3657921.28957]
[173, 536418.315024, 3658715.47946]
[223, 536220.396985, 3659243.54161]
[242, 536102.087002, 3658703.61054]
[244, 536968.755886, 3659409.42857]
[246, 535996.903591, 3658705.08691]
[303, 535999.885405, 3658521.91524]
Mike67
  • 11,175
  • 2
  • 7
  • 15