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

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 ?

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

Check this Distance between two points (given their coordinates)[^] for detail

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

Permalink

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.

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.

Permalink

Share this answer

v2

Comments

It's just implementing Pythagoras theorem[^]

Treat the two points as the two ends of the hypotenuse of a right angle triangle.

Treat the two points as the two ends of the hypotenuse of a right angle triangle.

Permalink

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) ̅

-√(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