In my last post I received some help in creating a function that would find the distance between 2 pairs of coordinates:
Clear[distance, x1, x2, y1, y2];
distance[{{x1_, y1_}, {x2_, y2_}}] := Abs[(x2 - x1)] + Abs[(y2 - y1)];
distance[{{0, 0}, {1, 1}}]
2
Now I am trying to generalize this function so that I could input any amount of coordinates (locations), and in the very end, return back to the origin (0,0). Here is an example. distance[{{1,1},{2,2}} = 8. So we would started at the origin (0,0) and travel to (1,1) which is the legs of a right triangle and equals 2. From (1,1) we travel to (2,2) which is another distance of 2, so now the total is 4..Now we go back to the origin (which is a distance of 4), thus the total distance is 8. This idea is very straight forward conceptually. I thought about creating a function that would iterate from location (0,0) and go to any list of coordinates, then back to (0,0), maybe by using something like the Length[{list of coordinates}]. I don’t know if that would be possible.
I have just a little bit of code that finds the distance from the origin to some other point, I just don’t understand how to implement something that will get me back to the origin, while going to any amount of coordinates I give it. I cant just go in a straight line because in real life one couldn’t drive through a building.
Clear[totaldistance, x1, x2, y1, y2];
totaldistance[{{x2_, y2_}}] := Abs[(x2 - 0)] + Abs[(y2 - 0)];
totaldistance[{{2, 2}}]
4
Thanks for any tips. Like I said, this problem is easy and makes sense conceptually, but putting my ideas into Mathematica is always the hard part.