Angular misclosure and leg directions
64-bitThe angular misclosure of a loop and the direction of each leg after the angles are corrected.
misclosure = ((again − first) mod 360 + 540) mod 360 − 180; per angle = −misclosure ÷ n; az(i) = az(i − 1) + 180 + angle(i) + per angle
Sums of degrees in 64-bit. Whole-degree parts add exactly; the misclosure is a difference of two accumulated azimuths, so it carries the rounding of the fractional parts of the angles. The azimuth grows by 180 degrees and more per leg and is not reduced before it is converted to radians.
let dirs, angular = { misclosure: null, perAngle: 0 };
if (mode === 'angles') {
const a = legs.map((l) => l.angle);
if (close === 'loop' && closingAngle !== null && n >= 2) {
let az = backsight + a[0];
for (let i = 1; i < n; i++) az += 180 + a[i];
const again = az + 180 + closingAngle, first = backsight + a[0];
const mis = ((((again - first) % 360) + 540) % 360) - 180;
angular = { misclosure: mis, perAngle: -mis / n };
}
dirs = [];
let az = backsight + a[0];
dirs.push(az2dir(az));
for (let i = 1; i < n; i++) { az += 180 + a[i] + angular.perAngle; dirs.push(az2dir(az)); }
} else dirs = legs.map((l) => l.dir);