Basic4ppc - Windows Mobile Development  

Go Back   Basic4ppc - Windows Mobile Development > Main Category > Code Samples & Tips
Home Register FAQ Members List Search Today's Posts Mark Forums Read

Code Samples & Tips Share your recent discoveries and ideas with other users.


Calculating distance


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-06-2008, 02:56 PM
Newbie
 
Join Date: Mar 2008
Posts: 6
Post Calculating distance

Here is some sample code to calculate distance between two points:

Code:
Sub getDistanceKM(lat1, long1, lat2, long2)

	DEGREES_TO_RADIANS = (cPI / 180.0)
	EARTH_RADIUS = 6371.0
	
	rlat1 = DEGREES_TO_RADIANS * lat1
	rlong1 = DEGREES_TO_RADIANS * long1
	rlat2 = DEGREES_TO_RADIANS * lat2
	rlong2 = DEGREES_TO_RADIANS * long2
	
	'	There is no real reason To break this lot into 
	'	4 statements but I just feel it's a little more 
	'	readable.
	p1 = Cos(rlat1) * Cos(rlong1) * Cos(rlat2) * Cos(rlong2)
	p2 = Cos(rlat1) * Sin(rlong1) * Cos(rlat2) * Sin(rlong2)
	p3 = Sin(rlat1) * Sin(rlat2)

	ret = p1 + p2 + p3
	If ret = 1 Then Return 0
	
	ret = ACos(ret)
	ret = ret * EARTH_RADIUS
	
	Return ret
	
End Sub
Can we add this to the gps.dll?
Reply With Quote
  #2 (permalink)  
Old 05-06-2008, 03:05 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 2,572
Default

Generally speaking, libraries should only include functions that it isn't possible to implement from code.
Reply With Quote
  #3 (permalink)  
Old 05-06-2008, 03:09 PM
Newbie
 
Join Date: Mar 2008
Posts: 6
Default

Quote:
Originally Posted by Erel View Post
Generally speaking, libraries should only include functions that it isn't possible to implement from code.
I was thinking of the processing power. Would complex calculations not run faster in a library?

Just a thought - not based on fact.
Reply With Quote
  #4 (permalink)  
Old 05-06-2008, 03:14 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 2,572
Default

Calculations will run faster in a library but the difference should not be too significant.
Reply With Quote
  #5 (permalink)  
Old 05-15-2008, 06:11 PM
Junior Member
 
Join Date: Apr 2008
Location: FRANCE
Posts: 13
Default

Hi there,

Just a little correction for your code

" If ret >1 Then Return 0 "

My tests give me sometime 1.0000000002 :-)

Bye,

Bruno.
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
GPS Distance Problem Georg Questions & Help Needed 6 04-25-2008 04:10 AM
calculating differences in time sunnyboyj Questions & Help Needed 1 02-07-2008 07:36 PM
Distance-test on UTM and LAT-LON Put Claude Questions & Help Needed 1 10-16-2007 01:06 PM
Distance between UTM zones jerryjukem Questions & Help Needed 3 10-10-2007 10:19 AM
Distance Calculator PatrikL Share Your Creations 7 04-30-2007 05:15 PM


All times are GMT. The time now is 03:34 AM.


Powered by vBulletin® Version 3.6.10
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0