Welcome Guest [Log In] [Register]
We hope you enjoy your visit.


You're currently viewing the Ultimate 3D Community as a guest. This means that you can only read posts, but can not create posts or topics by yourself. To be able to post you need to register. Then you can participate in the community active and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.

Join our community!

If you are already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Game Math c++ code snippets; Can be adapted to use in other languages
Topic Started: Dec 12 2009, 10:28 PM (2,554 Views)
Dr. Best
Member Avatar
Administrator
[ *  *  *  *  *  * ]
Ok, enough fun. Time to clear some things up.

Your new code still does not give the Manhattan-Distance, because it is basically equivalent to this:
Code:
 
def PointDistance(x1,y1,x2,y2):
return abs((x2-x1) + (y2-y1));

Or if you change it a little bit further it is this:
Code:
 
def PointDistance(x1,y1,x2,y2):
return abs(x2+y2-(x1+y1));

It is easy to see that this is not right, because it gives PointDistance(-1,1,1,-1)==0, but the correct Manhattan-Distance in this case would be 4. A correct function for the Manhattan-Distance would be:
Code:
 
def PointDistance(x1,y1,x2,y2):
return abs(x2-x1) + abs(y2-y1);


To contribute a little to the topic here is a function for arbitrary metrics induced by p-Norms. Attend Analysis lectures at the college of your choice to find out what that is.
Code:
 
def PDistance(X1,Y1,X2,Y2,P=2.0):
return (abs(x2-x1)**P + abs(y2-y1)**P)**(1.0/P);

For P=2.0 you get the Euclidean distance, which is the length of the shortest way between the two points, for P=1.0 you get the Manhattan-Distance, which is the length of the shortest way between the two points, if you can move only along the axes of the coordinate-system.
Offline Profile Quote Post Goto Top
 
Sothh
Member Avatar
Shaman Of Time
[ *  *  *  *  *  * ]
Dr. Best
Feb 26 2010, 12:53 AM
Ok, enough fun. Time to clear some things up.

Your new code still does not give the Manhattan-Distance, because it is basically equivalent to this:
Code:
 
def PointDistance(x1,y1,x2,y2):
return abs((x2-x1) + (y2-y1));

Or if you change it a little bit further it is this:
Code:
 
def PointDistance(x1,y1,x2,y2):
return abs(x2+y2-(x1+y1));

It is easy to see that this is not right, because it gives PointDistance(-1,1,1,-1)==0, but the correct Manhattan-Distance in this case would be 4. A correct function for the Manhattan-Distance would be:
Code:
 
def PointDistance(x1,y1,x2,y2):
return abs(x2-x1) + abs(y2-y1);


To contribute a little to the topic here is a function for arbitrary metrics induced by p-Norms. Attend Analysis lectures at the college of your choice to find out what that is.
Code:
 
def PDistance(X1,Y1,X2,Y2,P=2.0):
return (abs(x2-x1)**P + abs(y2-y1)**P)**(1.0/P);

For P=2.0 you get the Euclidean distance, which is the length of the shortest way between the two points, for P=1.0 you get the Manhattan-Distance, which is the length of the shortest way between the two points, if you can move only along the axes of the coordinate-system.
I got that code of the internet in the first place. I know nothing of geometry...
“You can’t outrun Death forever.
But you can make the Bastard work for it.”

Major Korgo Korgar
“Last of The Lancers” - AFC 32
(Andromeda Ascendant Record Database)
Offline Profile Quote Post Goto Top
 
Gandalf20000
Member Avatar
Geek
[ *  *  *  *  *  * ]
Here's one that is blatantly obvious, but I feel it should be mentioned anyway:

You mentioned a radians to degrees method, but not a degrees to radians method, which is just as simple, but you flip the 180 and the pi values.
Code:
 

double degreesToRadians(double radians)
{
return (radians*180/3.1415926);
}
Offline Profile Quote Post Goto Top
 
« Previous Topic · Tutorials · Next Topic »
Add Reply