# Taxi Cab problem - Loops

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 ?

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}}]


2

Now for any random set of coordinates

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


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

66

• A bit more compact: cab[pts__] := Total[#, 2] &@(Abs /@ Differences /@ Transpose@pts) – corey979 Oct 24 '16 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]