91

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?

TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • 1
    It didn't work. So I asked here. I figured out why. It's coz I did ^ instead of ** – TIMEX Mar 08 '11 at 04:42
  • 2
    @Greg: His track record says no. @TIMEX: Searching didn't work? Seriously: http://www.google.com/search?q=python+distance+points – Glenn Maynard Mar 08 '11 at 04:48
  • 5
    -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. – johnsyweb Mar 08 '11 at 08:03
  • 13
    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. – Rob Fisher Mar 18 '13 at 07:51
  • 4
    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. – Dan Dec 19 '13 at 18:47
  • 4
    @GlennMaynard I came here by searching "python distance point", what do I do now? – melvinmt Apr 05 '15 at 16:47
  • 1
    Be wise, generalise "dist = numpy.linalg.norm(a-b)" where a and b are your two vectors. – SeF Mar 10 '17 at 16:51
  • 1
    A closed question yet so popular !!!! – Jeru Luke Mar 14 '17 at 17:53

3 Answers3

149
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)
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • This is, by the way, the [distance formula](http://en.wikipedia.org/wiki/Distance#Geometry) – Andrew Marshall Mar 08 '11 at 04:37
  • 2
    did you mean http://en.wikipedia.org/wiki/Euclidean_distance ? – Mitch Wheat Mar 08 '11 at 04:38
  • This isn't how to do the "power" in python? Isn't it **? – TIMEX Mar 08 '11 at 04:40
  • 1
    @TIMEX: Yes it is. The change is now reflected on @MitchWheat's post – inspectorG4dget Mar 08 '11 at 04:43
  • 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? – Rob Fisher Mar 17 '13 at 22:55
  • 6
    @RobFisher - explicitly writing this expression may actually be *faster* than calling `math.hypot` since it replaces a function call with inline bytecodes. – PaulMcG Jul 16 '13 at 19:58
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)
PaulMcG
  • 62,419
  • 16
  • 94
  • 130
  • 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) – Enrico Ferreguti Mar 09 '18 at 07:59
18

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

PaulMcG
  • 62,419
  • 16
  • 94
  • 130
Maciej Ziarko
  • 11,494
  • 13
  • 48
  • 69