First, I just want to say I know that "ManhattanDistance" is a built in command for finding the distance between 2 pairs of points. But I have created my own function as follows:

Clear[taxicab, x1, x2, y1, y2];
taxicab[{{x1_, y1_}, {x2_, y2_}}] := Abs[(x2 - x1)] + Abs[(y2 - y1)];
taxicab[{{2, 3}, {1, 4}}]

Now, this is great for 2 pairs of coordinates, but what if I was driving around town (we will use an x,y plane) and go to different locations such as (1,1),(5,2), you get the idea?..I will start at the origin (0,0), go to specific locations defined by coordinates, and then end up back at the origin (0,0) at the end of my journey..I'm trying to create a loop function that can in turn, find the total distance I traveled, regardless of how many coordinates I give it..It's important to know that at the very end, I cannot travel just a straight line back to the origin, because, in real life this would be impossible..

I'm just getting started on some basic ideas but I have hit a wall.. Here is what I have thus far (without even creating the actual loop)

 Clear[getdistance, x, y, distance];
 distancelength := Length[{{1, 1}, {2, 2}}];
 totaldistance[{x2_, y2_}] := Abs[(x2 - 0)] + Abs[(y2 - 0)];
 totaldistance[{2, 3}]

In my totaldistance function I have initialized the distance to be the distance between the origin and the first location, great. Now I know I'm going to need to update that totaldistance using a loop..For each location I go to, I must find the new distance (totaldistance + taxicab[current point, previous point]). Lastly I'll need to finalize that totaldistance including distance from last point to the origin, and then have my output be that total distance traveled for all the coordinates..

I just don't understand how I am going to get back the origin at the very end, without traveling in some straight line. I'm just looking for some ideas to get my brain thinking in the right direction. it just wouldn't make sense for me to travel back to the origin using the coordinates (locations). Couldn't I just find the shortest distance to the origin using coordinates that don't just produce a straight line ?


2 Answers 2


Let's generalise your function for any number of points. Here I define

cab[pts__] := Total@Abs@Differences[pts[[All, 1]]] + Total@Abs@Differences[pts[[All, 2]]]

cab[{{2, 3}, {1, 4}}]


Now for any random set of coordinates

pts = Join[{{0, 0}}, RandomInteger[9, {10, 2}], {{0, 0}}]

{{0, 0}, {2, 3}, {4, 0}, {6, 3}, {2, 0}, {0, 6}, {3, 5}, {7, 8}, {4, 3}, {2, 5}, {2, 8}, {0, 0}}


  • $\begingroup$ A bit more compact: cab[pts__] := Total[#, 2] &@(Abs /@ Differences /@ Transpose@pts) $\endgroup$
    – corey979
    Oct 24, 2016 at 0:42

I hope this will be the expected answer

 Clear[taxicab, x1, x2, y1, y2];
 taxicab[{{x1_, y1_}, {x2_, y2_}}] := Abs[(x2 - x1)] + Abs[(y2 - y1)];
 taxicab[{{2, 3}, {1, 4}}]
 l = {{0, 0}, {1, 0}, {1, 1}, {2, 1}, {2, 3}, {1, 3}, {1, 2}, {0, 2}, {0, 0}}
 la := Riffle[l, l]
 lb = Drop[Drop[ll, 1], -1] 
 lc = Partition[lll, 2]
 ld = Thread[taxicab[lc]]

First, I you are decided to use Mathematica seriously you must forgot loops. Mathematica use mainly a functional programming paradigm ---even if for newcomers it authorizes the others programming paradigms.

Now I explain what ar the command ---they may be gathered but after the program becomes unreadible.

1) create a list l. 2) mix this list with herself --- Riflle 3) Drop the first and last entry of the list 4) Apply the new list to your function --- Thread --- which gives you the distance of the points two by two on you list 5) Make the Total to obtain what you expect.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.