Thread: Stuck converting Java code

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    106

    Stuck converting Java code

    Hi Guys,
    I'm trying to port the destination formula from here:
    Calculate distance and bearing between two Latitude/Longitude points using Haversine formula in JavaScript

    So far, I have this:
    Code:
        int brng;
        brng = 90;
        float lat1 = GPSlat;
        float lon1 = GPSlon;
        float lat2;
        float lon2;
    
        double dLat = d*cos(brng);
        lat2 = lat1 + dLat;
        double dPhi = log(tan(lat2/2+PI/4)/tan(lat1/2+PI/4));
        double q = (isfinite(dLat/dPhi)) ? dLat/dPhi : cos(lat1);  // E-W line gives dPhi=0
        double dLon = d*sin(brng)/q;
    
        // check for some daft bugger going past the pole, normalise latitude if so
        if (abs(lat2) > PI/2) lat2 = lat2>0 ? PI-lat2 : -PI-lat2;
    
        lon2 = (lon1+dLon+PI)%(2*PI) - PI;

    but the C compiler doesn't like the % sign in the last line.
    What is that doing?
    Cheers, Art.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    It might me better if you base your code on this Source: Arc and distance between two points on Earth surface
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Unlike Java, the modulus operator only works with integer types in C.

    Also, see abs vs fabs.
    Last edited by Tclausex; 02-28-2013 at 06:58 PM.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    106
    Quote Originally Posted by Click_here View Post
    It might me better if you base your code on this Source: Arc and distance between two points on Earth surface
    This one takes distance, heading, and one one coordinate pair,
    and outputs a coordinate pair for destination.
    I can calculate distance between two points with Haversine already.
    This one is for drawing the ruler on a GPS screen,
    and the accuracy circle around the dot that marks the user's position.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    106
    I got it with this tutorial:
    Bearing and Distance Calculator Calculation Methods
    The guts of it are already in C, and you can leave out the
    bearing calculations if you just want the point coordinates.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    instead of %, check out fmod

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by xArt View Post
    Hi Guys,
    I'm trying to port the destination formula from here:
    Calculate distance and bearing between two Latitude/Longitude points using Haversine formula in JavaScript

    So far, I have this:
    Code:
        int brng;
        brng = 90;
        float lat1 = GPSlat;
        float lon1 = GPSlon;
        float lat2;
        float lon2;
    
        double dLat = d*cos(brng);
        lat2 = lat1 + dLat;
        double dPhi = log(tan(lat2/2+PI/4)/tan(lat1/2+PI/4));
        double q = (isfinite(dLat/dPhi)) ? dLat/dPhi : cos(lat1);  // E-W line gives dPhi=0
        double dLon = d*sin(brng)/q;
    
        // check for some daft bugger going past the pole, normalise latitude if so
        if (abs(lat2) > PI/2) lat2 = lat2>0 ? PI-lat2 : -PI-lat2;
    
        lon2 = (lon1+dLon+PI)%(2*PI) - PI;

    but the C compiler doesn't like the % sign in the last line.
    What is that doing?
    Cheers, Art.
    Code:
    void distandbearing(float lat1, float lon1, float brng, flaot d, float *latout, float *lonout)
    {
    
        float lat2;
        float lon2;
       double dLat;
       double dPhi;
       double q;
       double dLon;
    
        dLat = d*cos(brng);
        lat2 = lat1 + dLat;
        dPhi = log(tan(lat2/2+PI/4)/tan(lat1/2+PI/4));
        q = (isfinite(dLat/dPhi)) ? dLat/dPhi : cos(lat1);  // E-W line gives dPhi=0
        dLon = d*sin(brng)/q;
    
        // check for some daft bugger going past the pole, normalise latitude if so
        if (abs(lat2) > PI/2) lat2 = lat2>0 ? PI-lat2 : -PI-lat2;
    
        lon2 = fmod((lon1+dLon+PI),(2*PI)) - PI;
       
       *latout = lat2;
       *lonout = lon2;
    }
    (Untested)
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help converting a java program to C
    By rpmischris in forum C Programming
    Replies: 8
    Last Post: 08-09-2012, 08:34 PM
  2. Converting Java code into C, I need help >.<
    By minidragon in forum C Programming
    Replies: 3
    Last Post: 05-26-2011, 04:30 AM
  3. Help Converting Java to C
    By handsomedan in forum C Programming
    Replies: 1
    Last Post: 03-16-2010, 09:17 PM
  4. Replies: 6
    Last Post: 08-07-2003, 02:05 PM
  5. converting from java
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2001, 11:17 AM