Web Wargaming

Web Wargaming Home

Offset function for the Line of sight

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

Line of Sight

From Analytical Geometry, there is a formula to calculate the distance from a point to a line.  Since the coordinates can be converted to geometry, we can use the formula.   After factoring out the translation factors (the denominator), the formula boils down to a offset function.

Here it is: offset = abs(  x1*y2 - x1*y3 - x2*y1 + x2*y3 + x3*y1 - x3*y2)

x1, y1 is the origin of the line.  x2,y2 is the endpoint of the line
x3, y3 is the coordinate of the point being tested

click here to see the offset function in action

For example: hexagon 0104 (4,20) to hexagon 0302 (8,12)
The 2 possible hexagons to check are 0103 (4,16) and 0203 ( 6,18)

x1, y1 is the origin of the line.  x2,y2 is the endpoint of the line
x3, y3 is the coordinate of the point being tested From hexagon 0104, we need to choose between 0103 or 0203
For the first check of hexagon 0103  x1,y1 = 4,20  x2,y2 = 8,12  x3,y3 = 4,16
the offset check is abs( 4*12 - 4*16 - 8*20 + 8*16 + 4*20 - 4*12 ) =
abs( 48 - 64 - 160 + 128 + 80 - 48) = 16

For the second check of hexagon 0203 x1,y1= 4,20  x2,y2=8,12  x3,y3 = 6,18
the offset check is abs( 4*12 - 4*18 - 8*20 + 8*18 + 6*20 - 6*12) =
abs( 48 - 72 - 160 + 144 + 120 - 72) = 8

We pick the lessor value 0203

Continue on up the LOS line checking. 
Brute force, the only way.