How do I find the distance between two points? [closed]
Asked Answered
S

3

92

Let's say I have x1, y1 and also x2, y2.

How can I find the distance between them? It's a simple math function, but is there a snippet of this online?

Sherwoodsherwynd answered 8/3, 2011 at 4:33 Comment(8)
It didn't work. So I asked here. I figured out why. It's coz I did ^ instead of **Sherwoodsherwynd
@Greg: His track record says no. @TIMEX: Searching didn't work? Seriously: google.com/search?q=python+distance+pointsRhodian
-1 for "is there a snippet of this online?" Seriously, @TIMEX, if searching the web for a code snippet is too hard, now is the time for a change of career.Cascara
I'm surprised this question is closed. It was in my search results for 'python pythagoras' and was how I discovered the existence of math.hypot.Rhumb
Okay, I just want to add a note that this is on the first page of google. It always frustrates me to see "just google it" as the first answer. Quite obviously there was some need that this question filled.Parasite
@GlennMaynard I came here by searching "python distance point", what do I do now?Hobie
Be wise, generalise "dist = numpy.linalg.norm(a-b)" where a and b are your two vectors.Ranson
A closed question yet so popular !!!!Insatiate
A
150
dist = sqrt( (x2 - x1)**2 + (y2 - y1)**2 )

As others have pointed out, you can also use the equivalent built-in math.hypot():

dist = math.hypot(x2 - x1, y2 - y1)
Agbogla answered 8/3, 2011 at 4:35 Comment(6)
This is, by the way, the distance formulaWarfarin
did you mean en.wikipedia.org/wiki/Euclidean_distance ?Agbogla
This isn't how to do the "power" in python? Isn't it **?Sherwoodsherwynd
@TIMEX: Yes it is. The change is now reflected on @MitchWheat's postZeal
I'm inclined to downvote this and all answers that ignore math.hypot because it seems wrong to reimplement code, however trivial, that is in the standard library. But instead, a question: is there any good reason to write the above code?Rhumb
@RobFisher - explicitly writing this expression may actually be faster than calling math.hypot since it replaces a function call with inline bytecodes.Andee
A
72

Let's not forget math.hypot:

dist = math.hypot(x2-x1, y2-y1)

Here's hypot as part of a snippet to compute the length of a path defined by a list of (x, y) tuples:

from math import hypot

pts = [
    (10,10),
    (10,11),
    (20,11),
    (20,10),
    (10,10),
    ]

# Py2 syntax - no longer allowed in Py3
# ptdiff = lambda (p1,p2): (p1[0]-p2[0], p1[1]-p2[1])
ptdiff = lambda p1, p2: (p1[0]-p2[0], p1[1]-p2[1])

diffs = (ptdiff(p1, p2) for p1, p2 in zip (pts, pts[1:]))
path = sum(hypot(*d) for d in  diffs)
print(path)
Andee answered 8/3, 2011 at 9:8 Comment(1)
Python3 no longer allows tuples as lambda parameter so the function become this: ptdiff = lambda p: (p[0][0]-p[1][0], p[0][1]-p[1][1]) diffs = map (ptdiff , zip(pts[:-1],pts[1:])) path = sum(math.hypot(d1,d2) for d1,d2 in diffs)Macdonell
T
18

enter image description here It is an implementation of Pythagorean theorem. Link: http://en.wikipedia.org/wiki/Pythagorean_theorem

Titfer answered 8/3, 2011 at 4:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.