TGC Codebase Backup



Get Distance by CrazyCoder

6th Jul 2010 12:58
Summary

Returns the distance between 2 2D or 3D points



Description

These functions use pythagoras' theorem to find the distance between 2 points by finding the square root of the x-distance squared plus by the y-distance squared and if 3D then the z-distance squared as well. It doesn't matter if X1-X2 is negative because you square it.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    float Get2DDistance(int X1, int Y1, int X2, int Y2)
{
     return sqrt((X1-X2)*(X1-X2)+(Y1-Y2)*(Y1-Y2));
}

float Get3DDistance(float X1, float Y1, float Z1, float X2, float Y2, float Z2)
{
     return sqrt((X1-X2)*(X1-X2)+(Y1-Y2)*(Y1-Y2)+(Z1-Z2)*(Z1-Z2));
}