Battle Analysis: Calculate the hexagon on the map

 

Use a divide and conquer strategy to calculate the hexagon.

  1. check hexagon center
  2. check - hexside (upper and lower hexsides)
  3. check \ hexside (upper right and lower left hexsides)
  4. check / hexside (lower right and upper left hexsides)

Divide the map into columns of 3/4 * hexagon width.

Calculate the distance of the point from the edge

In this rectangle, the point on the grid is: and is adjusted to hexagon point:

The hexagon name: hexagon name is a calculation


map

Check if point is at center or hexside edge

  1. column count = Math.floor(x/columnWidth) =
  2. edge = count * columnWidth =
  3. (x distance from edge) = x - edge =
  4. if (x distance > hexsideWidth), [ : ]
    Check the center column
    else
    Check the hexsides column

 

Check the center column
Divide the map into
rows of 1/2 * hexagon height
Count the columns and rows
map
map

Calculate the hexagon point

  1. column count = Math.floor(x/columnWidth)
  2. column edge = count * columnWidth
  3. (x distance from edge) = x - column edge
  4. row count = Math.floor(y/rowHeight)
  5. row edge = count * rowHeight
  6. (y distance from edge) = y - row edge
  7. if (x distance < hexsideWidth ) Check the hexsides
    else
    using column,row
    Calculate the hexagon point from the center
        hexagon point coordinate (x,y) = (2*column, 2*row)
          = (x,y))
    else
    Calculate the hexagon point from the center hexside
    if (y distance < hexagonHeight/4)
        hexagon point coordinate (x,y) = (2*column, 2*row-2)
          = (x,y)
    else
        hexagon point coordinate (x,y) = (2*column, 2*row+2)
           = (x,y)

 

Check the hexsides column

Divide the map into
rows of 1/2 * hexagon height
Count the columns and rows
map
map

Calculate the hexagon point from the hexside

  1. x = (2 * col) - 1
  2. y = (2 * row) + 1
  3. tangent of hexside = (hexagonHeight/2)/(hexagonWidth/4)
    tangent:   should be close to tan(60°) = √3 = 1.732
  4. tangent of point = (y distance from edge)/(x distance from edge)
  5. if left of a / hexside: hexagon point = (column-1,row-1)
  6. if right of a / hexside: hexagon point = (column+1,row+1)
  7. if left of a \ hexside: hexagon point = (column-1,row+1)
  8. if right of a \ hexside: hexagon point = (column+1,row-1)
hexagon point coordinate :

 

 

Mark Butler
Copyright © 2009-2019