Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
(float X1,float Y1,float X2,float Y2)
    {
        float dx = X1 - X2;
        float dy = Y1 - Y2;
        float sx = (float)cos( Y1 * 0.01745329252f);
        return ((float)sqrt(dx * dx * sx * sx + dy * dy) * 111195.0f);
    }

I found a implementation like this. What is its meaning ?
Posted

I can't recognize the mentioned math. The simplest way to calculate two points distance is

C++
float GetDistance(float X1,float Y1,float X2,float Y2)
    {
        float dx = X1 - X2;
        float dy = Y1 - Y2;
        return (float)sqrt(dx * dx + dy * dy);
    }


Check this Distance between two points (given their coordinates)[^] for detail
 
Share this answer
 
This is an approximation formula for the distance of two points on a sphere. The factor 0.0174... is PI / 180. That tells us that the Y values are provided in degrees. The multiplication with PI/180 converts the value to radiant.

The multiplication of dx*dx with sx*sx is the compensation for the fact that meridians run closer together in higher latitudes.

The factor 111195 corresponds to distance of one degree on the surface of the earth.

So in summary: The X und Y values are latitude and longitude in degrees and the formula delivers the distance of the two points in meters. It works only if the two points are relatively close together.
 
Share this answer
 
v2
Comments
_Asif_ 27-Aug-14 7:06am    
+5 for the explanation, i have googled earlier but there was nothing of this sort.
nv3 27-Aug-14 11:23am    
You are welcome.
It's just implementing Pythagoras theorem[^]

Treat the two points as the two ends of the hypotenuse of a right angle triangle.
 
Share this answer
 
Comments
_Asif_ 27-Aug-14 5:13am    
This is not a Pythagoras implementation. Pythagoras theorem is √(x^2+y^2 ). But the question is
-√(x^2 cos^2⁡ϑ+y^2) ̅

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900