On Mon, Sep 08, 2014 at 01:47:57PM -0500, Bill Gee wrote: > The starting point is two azimuth readings. Call them FOR1 and BACK1. > > 1) FOR2 = BACK1 + 180 > 2) FOR2 = FOR2 MOD 360.0 > 3) If (FOR1 - FOR2) > 180 then FOR2 = FOR2 + 360.0 > 4) FINAL = ((FOR1 + FOR2) / 2) MOD 360.0)
For 1 and 179, that gives me 180, rather than 0. What Survex does is more complex, as the foresight and backsight may have different standard deviations specified, and it's working in radians at this point, but in the case where the two instruments are the same specified accuracy (which seems likely to be by far the most common), it boils down to this (fabs() is the absolute floating point value - i.e. drop any minus sign): backcomp -= 180; diff = comp - backcomp; adj = fabs(diff) > 180 ? 180 : 0; final = (comp + backcomp) / 2 + adj; You can see the full version here: https://github.com/ojwb/survex/blob/master/src/datain.c#L893 That's pretty close to what you have - I think the key difference is the fabs() on the wrap-around check. I believe I proved that the averaging algorithm Survex uses was correct back when I implemented this, though unhelpfully I didn't add the proof as a comment in the code. Though looking at the code now, I think it should be wrapping the two bearings to the range [0,360) after it corrects for zero errors and declination and before it does the actual averaging. I'll take a look at that. Footleg's idea of averaging vectors is interesting - I'd not thought about that before. One nice feature is that it naturally extends to more than 2 bearings. It also helpfully gives "invalid" for trying to average bearings which completely oppose (whereas feeding the same bearing in as foresight and backsight above will give you a bearing that's 90 degrees away to one side or the other, arbitrarily). Cheers, Olly