Solved

Calculate new coordinates given lat/long/bearing/distance in C

Posted on 2011-09-28
4
1,974 Views
Last Modified: 2012-08-13
I would like an example or some C code showing how to calculate the new longitude and latitude coordinates if one has the starting point, in longitude and latitude, along with the distance to travel, in feet, and the heading/bearing. So for example if I have the long/lat as:
4290816,-78793945 distance = 50 feet and bearing = 55 degrees I need the new long2 lat2.

I have seen the data at:
http://williams.best.vwh.net/avform.htm
and
http://www.movable-type.co.uk/scripts/latlong.html

but haven't been able to get the formulas to work correctly.

Again I need this in C, not Jave/Basic/Perl what have you.

Thanks
0
Comment
Question by:atomicgs12
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 400 total points
ID: 36720534
Using the formulas shown at your second link, the C code should look something like this:
      long double lat1;
      long double lon1;
      long double lat2;
      long double lon2;
      long double Th;
      long double d;
      long double R;

      const long double PI = 3.1415926535;

      lon1 = 42.90816;
      lat1 = -78.793945;

      Th = PI * 50 / 180;      //Must convert Bearing in degrees to radians
      d = 50;                        //Distance in feet, so...
      R = 3959 * 5280;      //Mean Radius of the earth in feet (note, earths radius varies from 3947 to 3968 miles)

      lat2 = asin( sin(lat1) * cos(d/R) + cos(lat1) * sin(d/R) * cos(Th) );
      lon2 = lon1 + atan2( sin(Th) * sin(d/R) * cos(lat1), cos(d/R) - sin(lat1) * sin(lat2) );

The key is to remember that the trig functions us radias.  So your 55 degees must be converted to radians.  The formula uses the value distance/earth radius over and over.  What ever your dimensions for distance need to match for earth's radius.  So if you distance is in feet, you must plug in earth's radius in feet.  Then just to throw confusion into the mix, the earth's radius is not fixed because the earth isn't a perfect sphere.  So the numbers you use might be different than the numbers someone else has used for earth's radius.
0
 

Author Comment

by:atomicgs12
ID: 36720649
HooKooDooKu:
When I run you code, with 55 as bearing, I get the following values:
lon2 = 42.908160000000002
lat2 = 0.95993108859688125
this was kind of the values I was getting before. I was wondering about the lat2 formula, maybe it is suppose to be lat2 = lat1 + asin( sin(lat1) * cos(d/R) + cos(lat1) * sin(d/R) * cos(Th) );

Did you run you code?
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 100 total points
ID: 36814257
are you converting lat and long to radians when you take sin and cos?
I get lat2=-1.375212233366 radians = -78.7938569 degrees
0
 

Author Comment

by:atomicgs12
ID: 36815551
Ah 'ozo' you are correct, I had the radian conversion in there but for testing I commented out that section. When converting back it looks like the correct values now. Thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question