Stuck converting Java code

This is a discussion on Stuck converting Java code within the C Programming forums, part of the General Programming Boards category; Hi Guys, I'm trying to port the destination formula from here: Calculate distance and bearing between two Latitude/Longitude points using ...

1. 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. It might me better if you base your code on this Source: Arc and distance between two points on Earth surface

3. Unlike Java, the modulus operator only works with integer types in C.

Also, see abs vs fabs.

4. Originally Posted by Click_here
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. 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. instead of %, check out fmod

7. Originally Posted by xArt
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)