Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works

# Manhattan Distance Function - Python

### #1 smitty1199 Posted 24 March 2009 - 09:19 AM

smitty1199

New Member

• Member
• 3 posts
Hello Everyone,

I've been trying to craft a Manhattan distance function in Python.
However, it seems quite straight forward but I am having trouble.

Can anyone help me out with Manhattan distance metric written in Python?

Smitty
• 0

### #2 smitty1199 Posted 24 March 2009 - 06:15 PM

smitty1199

New Member

• Topic Starter
• Member
• 3 posts
Any help would be greatly appreciated! Thanks.
• 0

### #3 Dan Posted 25 March 2009 - 10:57 PM

Dan

Trusted Tech

• Retired Staff
• 1,771 posts
Hey Smitty,

I've only ever done the Manhattan distance in C/C++ (for an A* Search, if memory serves). However, if you could post what you have so far, I may be able to point you in the right direction.

It really is quite a simple alg; all you want to do is get the difference of the x and y co-ordinates, in a sort of right-angle walk approach. So, say you were at (15,20) and you wanted to get the distance to (35,40), you would do (35-15) + (40-20), giving you a distance of 40.

So, a pseudo approach would be:
Let curr.x = 15, curr.y = 20;
Let dest.x = 35, dest.y = 40;

man_dist = ((dest.x - curr.x) + (dest.y - curr.y))

At least, thats how I remember the Manhattan distance. I could be wrong; it has been some time

Like I said, show us your code and we may be able to walk you through it.
• 0

### #4 smitty1199 Posted 27 March 2009 - 10:55 AM

smitty1199

New Member

• Topic Starter
• Member
• 3 posts
Here's my start....

Any suggestions?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#This operation returns the Manhattan distance.
#
def getManhattan(xList,yList):

return float(xList[0]-yList[0])+ abs(xList[1]-yList[1])
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
• 0

### #5 Dan Posted 27 March 2009 - 07:05 PM

Dan

Trusted Tech

• Retired Staff
• 1,771 posts
Hey Smitty,

Is xList a list of all of your x co-ords, and yList a list of all of your y co-ords? And you only want to ever get the distance between the first and second point (0 and 1)?

If so, try this:
```#This operation returns the Manhattan distance.
#
def getManhattan(xList,yList):

return float(xList[0]-xList[1]) + abs(yList[0]-yList[1])```
As a note here, you may need to swap the 1 and 0 around, depending on how you are storing points. I would expect xList[1]-xList[0] + yList[1]-yList[0] to be closer to what you need, but I'm not sure how exactly you are storing points, so I can't be 100% certain.
• 0

### Similar Topics

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users