PDA

View Full Version : SUGGESTION: Change in hex grid calculation



ronnke
April 24th, 2010, 03:52
In playing around with some of the image script functions I have run into an issue when using the hex grid.

When using pointers, you are not able to get an accurate measure of distance due to the method in which the hex is calculated. It basically comes down to rounding, which you can't eliminate entirely, but you can reduce the margin of error introduced.

Currently:
The hexagonal shape is then calculated in an integer approximation by calculating a "half height" value (half of the grid size, rounded down) and a "quarter width" (grid size * tan(30 degrees) / 2, rounded down).

I would suggest that this formula be:
Half Height = (Grid Size / 2, rounded down)
Quarter Width = ((Half Height * tan(30 degrees)) + 0.5, rounded down).

This will round values to the nearest whole number. When determining the length of a pointer vector you can then divide the vector length by (2 * Half Height) and get a much more accurate approximation of the length of the vector in hexes.

An even better solution would be to make the calculation of the hex dimension a lua scripted event so the grid dimensions become a user configurable thing:

function onGridHexCalculation( )
Return values
(number) The quarter width of the hex
(number) The half height of the hex

this could be paired with

function onPointerVector(vector)
Return values
(number) The number of pixels in a vector

The current onMeasurePointer(pixellength) will then use the value returned from the onPointerVector(vector) function.

These functions would then allow for perfectly accurate length calculations on a user configurable grid.