Message Boards Message Boards

0
|
5052 Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Creating a function that can find the distance between pairs of points

Posted 8 years ago

I am attempting to create a function that can find the distance between a pair of points. Imagine I was walking or driving on a (x,y) plane and I started at the point (1,1) and ended up at (4,7). In general, it could be any 2 pairs of points..

Here is what I have come up with thus far...

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

Do I have to explicitly define my list as a pair of points, like (1,1) and (4,7)? Why cant I define it as the way I have it with the x1,x2 and y1,y2?? Then at the very end I could plug in any pair of points for the distance function I created.. Unless I have a syntax error, or maybe I don't have something define correctly.

Any suggestions is greatly appreciated!

B

POSTED BY: Brandon Davis
3 Replies
Posted 8 years ago

Great Sander!

The more I explore mathematica and attempt to create my own functions, the more I realize there is already built in functions for the tasks I'm trying to do.

Thank you

POSTED BY: Brandon Davis

Most common correct syntax would be:

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

notice := and the pattern in place.

But also not that the taxicab norm is built-in as ManhattanNorm:

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

not that is received two vectors, not, like your function, 2 x-values, and 2 y-values...

POSTED BY: Sander Huisman
Posted 8 years ago

It appears I have solved my own problem (which is always a great feeling).. It appears I couldn't use the "Abs" command for the whole function, but I have to apply it to both..Is this true? This way works great for me.

Clear[x1, x2, y2, y1, x, y, taxicab, points, list];
list = {{x1_, x2_}, {y1_, y2_}};
taxicab[list] = Abs[(x2 - x1)] + Abs[(y2 - y1)];
taxicab[{{2, 1}, {3, 4}}]
POSTED BY: Brandon Davis
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract