// bearing function
var mini_sectors = new Array (
0, 0, 16, 17, 14, 15, 13
, 0, 18, 20, 19, 22, 21, 23
, 12, 0, 8, 7, 10, 9, 11
, 0, 6, 4, 5, 2, 3, 1);
function bearingCalc(x1, y1, x2, y2) {
var bearing;
var delta_x, delta_y;
var absolute_x, absolute_y;
var x3times, sector, quadrant;
// step 1. find the sector
delta_x = x2 - x1;
delta_y = y2 - y1;
if( delta_x == 0 && delta_y == 0 )
bearing = -1;
else
{
absolute_x = Math.abs(delta_x);
absolute_y = Math.abs(delta_y);
x3times = 3 * absolute_x;
if( delta_x == 0 ) sector = 0;
else{
if(delta_y == 0) sector = 1;
else{
if( absolute_x == absolute_y) sector = 2;
else{
if(absolute_x > absolute_y) sector = 3;
else{
if( x3times == absolute_y) sector = 4;
else{
if( x3times > absolute_y) sector = 5;
else
sector = 6;
}
}
}
}
}
// step 2. find the quadrant
if( delta_x < 0 ) {
if( delta_y > 0) quadrant = 0;
else quadrant = 1;
}
else {
if( delta_y > 0) quadrant = 2;
else quadrant = 3;
}
bearing = mini_sectors[ ( quadrant * 7 ) + sector ];
}
return ( bearing );
}
Copyright (c) 1998-2007 Mark Butler
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. ;-)