sarisinhache
sarisinhache

Reputation: 67

How to calculate the distance between two points using Euclidean distance?

Answer is here.

I have the following points in a cartesian plane

points = [(4, 5), (-0, 2), (4, 7), (1, -3), (3, -2), (4, 5), (3, 2), (5, 7), (-5, 7), (2, 2), (-4, 5), (0, -2), (-4, 7), (-1, 3), (-3, 2), (-4, -5), (-3, 2), (5, 7), (5, 7), (2, 2), (9, 9), (-8, -9)]

And I need to find the closest point to the center using the Euclidean distance.

So far a I have done this:

import math
euclidean_distance=[]
distance = math.sqrt(sum([((x+y)**2) for (x,y) in points]))

for (x,y) in points:
    int(distance)
    euclidean_distance.append(distance)

min_distance=min(euclidean_distance)

print(distance)
print(euclidean_distance)
print(min_distance)

and the results are:

38.71692136521188
[38.71692136521188, 38.71692136521188, 38.71692136521188, 38.71692136521188, 38.71692136521188, 38.71692136521188, 38.71692136521188, 38.71692136521188, 38.71692136521188, 38.71692136521188, 38.71692136521188, 38.71692136521188, 38.71692136521188, 38.71692136521188, 38.71692136521188, 38.71692136521188, 38.71692136521188, 38.71692136521188, 38.71692136521188, 38.71692136521188, 38.71692136521188, 38.71692136521188]
38.71692136521188

Why is not calculating the Euclidean distance in each point of the list?

Upvotes: 2

Views: 2056

Answers (1)

sarisinhache
sarisinhache

Reputation: 67

Finally I did this and worked for me:

import math

distance_points=[]

def euclidean_distance (points):
    return math.sqrt((x**2+y**2))

for (x,y) in (points): 
    distance=euclidean_distance(points)
    distance_points.append(distance)


print(distance_points)

Upvotes: 1

Related Questions