/    Sign up×
Community /Pin to ProfileBookmark

Anyone good at Maths?!!

Hi,

I have been working on a distance finding tool which gives the mileage between two UK postcodes.

I have a slight problem in that the formula I use will only work with Longitude & Latitude figures and not with the UTM system (Eastern & Northing).

The formula looks like this:

function distance($lat1, $lon1, $lat2, $lon2, $unit) {

$theta = $lon1 – $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist *
60 * 1.1515;
$unit = strtoupper($unit);

if ($unit == “K”) {
return ($miles * 1.609344);
} else if ($unit == “N”) {
return ($miles *
0.8684);
} else {
return $miles;
}
}

echo distance(51.75444444, -0.335277778, 51.65583333, -0.064166667, “M”) . ” Miles<br>”;
echo distance(51.75444444, -0.335277778, 51.65583333, -0.064166667, “K”) . ” Kilometers<br>”;
echo distance(51.75444444, -0.335277778, 51.65583333, -0.064166667, “N”) . ” Nautical Miles<br>”;

The Long & Lat look like the figures used above. Eastern & Northing are generally 5 figure numbers (51400 etc etc).

any ideas how i might change this formula to work with the eastern/northing figures?

Maths isn’t really my strong subject!

Thanks

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@gizmoJan 13.2004 — Start again from first principles. The solution is simpler than with lat/long because the units are already metric. Split the co-ordinates into E & N to give E1, N1 and E2, N2. The distance is then the hypoteneuse of the right triangle of sides E1-E2 and N1-N2 which as Mr Pythagorus says is the square root of the sum of the squares.

I hope that is clear without resorting to trying to reproduce maths symbols on the BB.
Copy linkTweet thisAlerts:
@Sux0rZh_jc0rzJan 13.2004 — gizmo, it's a sad day when a 14 year old understands what you just said. and its a sad day indeed.

what you were saying was easy, but you made it sound so advanced, even though it was basic math skills. u should be in politics. confuse everyone so that they agree with you by instinct.
Copy linkTweet thisAlerts:
@gizmoJan 13.2004 — Sux0rZh@jc0rz the obfuscation wasn't intentional, I've just got home and hadn't cleared my mind properly. If it were that simple, how come our friend hadn't figured it out? ?
Copy linkTweet thisAlerts:
@c_p_cauthorJan 13.2004 — I said that maths wasn't my strongest subject!

I understand the Pythagorus way of doing things now, although I tried it a couple of times and got some very inaccurate results.

It has been quite a while since I did these kind of sums.

I must have done something wrong somewhere but I'll get it eventually.
×

Success!

Help @c_p_c spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...