Web Wargaming

Web Wargaming Home map

bearing method

Now we can return the bearing
hexagon: 0000
click on the mouse, the bearing method will be initialized.
moving the mouse again will display the bearing

 
The bearing method is an algorithm, not a math function.
 
It's really a 'fake' function.
 
-1 if in same hexagon.
 
12 of the numbers refer to a straight line that is actually a real bearing.
 
0 - North
2 - 30 degrees
4 - 60 degrees
6 - East
8 - 120 degrees
10 - 150 degrees
12 - South
14 - 210 degrees
16 - 240 degrees
18 - West
20 - 300 degrees
22 - 330 degrees

the other 12 odd numbers refer to the areas in between.

Note: most of the time, I just divide the bearing by 4 to get a number for the hexagon side.
 
0 - up   1 - up right   2 - down right
3 - down   4 - down left   5 - up left
 
Otherwise, the bearing could be used to check a field of fire, like a broadside.

 
The if-then statements break out into 7 posibilities:

1.) North-South line [ 0, 12 ] only 2 possible
2.) East-West line [ 6, 18 ] only 2 possible
3.) hexagon-face line [ 4, 8, 16, 20 ]
4.) area near East-West line [ 5, 7, 17, 19 ]
5.) hexagon-corner line [ 2, 10, 22, 14 ]
6.) areas around NE-SW, NW-SE [ 3, 9, 15, 21 ]
7.) area near North-South line [ 1, 11, 13, 23 ]

Then it checks for which quadrant the bearing is in:
1st column is upper right
2nd column is lower right
3rd column is lower left
4th column is upper left
 

bearing form

// 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. ;-)