Hexagonal grids
point location
hexagon algorithms
geometry
spatial analysis

Hexagonal Grids, how do you find which hexagon a point is in?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Hexagonal grids are a fascinating structure that finds application in various domains, including computer graphics, geographical information systems, and board games. Unlike square grids, hexagonal grids offer the advantage of representing some phenomena more naturally due to their symmetry and connection properties. Understanding how to work with hexagonal grids can be particularly advantageous for simulations that require equal connectivity to adjacent cells, which is a feature not provided by traditional grid systems.

Basics of Hexagonal Grids

A hexagonal grid is a tessellation of space with hexagons, where each hexagon shares edges with six neighboring hexagons. This structure can be visualized as a combination of tiles covering a plane without any gaps.

Types of Hexagonal Grids

There are two common orientations for hexagonal grids:

  1. Flat-topped hexagons • These hexagons have flat sides parallel to the x-axis. • Arrangement looks column-aligned.
  2. Pointy-topped hexagons • These hexagons have vertices pointed in the y-axis direction. • Arrangement appears row-aligned.

Each type has different implications for generating and interpreting the grid, as well as for determining the hexagon in which a point lies.

Coordinate Systems

Axial Coordinates

One way to represent the coordinate system for a hexagonal grid is by using axial coordinates. In this system, each hexagon is represented by a pair of coordinates, (q,r)(q, r), where qq is the column and rr is the row. This simpler two-dimensional system is derived from the three-dimensional cube coordinates.

Cube Coordinates

Every hexagonal grid can be expressed in three dimensions using cube coordinates (x,y,z)(x, y, z) such that the sum of xx, yy, and zz is zero (x+y+z=0x + y + z = 0). The connection between cube and axial coordinates is given by:

x=qx = qz=rz = ry=xzy = -x - z

Cube coordinates offer the advantage of straightforward calculations of distances and transformations.

Offset Coordinates

Offset coordinates can also describe hexagonal grids, much like addressing a rectangular grid. This system offsets every other column or row, leading to straightforward alignments with existing data structures for traditional grid-based implementations.

Determining Hexagon Containing a Point

To find which hexagon in the grid contains a given point (x,y)(x, y), you can follow these steps:

  1. Convert the Point to Cube Coordinates:
    Using the transformations: • fx=33(x)13(y)\text{fx} = \frac{\sqrt{3}}{3} (x) - \frac{1}{3} (y)fz=23(y)\text{fz} = \frac{2}{3} (y)
  2. Round to the Nearest Cube Coordinate:
    Cube coordinates involving floating-point numbers should be rounded to the nearest integers. The rounding for each axis is done as: • rx=round(fx)\text{rx} = \text{round}(\text{fx})ry=round(fy)\text{ry} = \text{round}(\text{fy})rz=round(fz)\text{rz} = \text{round}(\text{fz})
    After rounding, ensure the constraint rx+ry+rz=0rx + ry + rz = 0 by adjusting the component with the largest rounding error.
  3. Convert to Axial or Offset Coordinates:
    Finally, convert the resulting cube coordinates back to the desired coordinate system.

Example Calculation

Suppose we have a point (x,y)=(1.0,2.0)(x, y) = (1.0, 2.0) and aim to identify its containing hexagon:

Using the formulae: • fx=33(1.0)13(2.0)0.2887\text{fx} = \frac{\sqrt{3}}{3} (1.0) - \frac{1}{3} (2.0) \approx 0.2887fz=23(2.0)=1.3333\text{fz} = \frac{2}{3} (2.0) = 1.3333

Rounding these to the nearest integers: • rx=0\text{rx} = 0rz=1\text{rz} = 1

Ensuring that rx+ry+rz=0rx + ry + rz = 0, we find ry=1\text{ry} = -1.

Converted to axial coordinates, this point lies in (q,r)=(0,1)(q, r) = (0, 1).

Applications of Hexagonal Grids

Hexagonal grids find significant applications in diverse fields:

Games: Board games like "Settlers of Catan" extensively use hexagonal grids for their maps, offering a uniform distribution. • Geographic Information Systems (GIS): Hexagonal tiling shows better sampling properties for certain spatial operations. • Computer Graphics: Hexagons can reduce visual artifacts like aliasing and produce more visually pleasing rendering results.

Summary Table

Hexagonal Grid ElementDescription
TypesFlat-topped Pointy-topped
Coordinate SystemsAxial Cube Offset
Determining HexagonConvert point to cube coordinates Round cube coordinates Convert back to desired coordinate system
Fields of ApplicationGames GIS Computer Graphics

Hexagonal grids provide a geometric structure suitable for many use cases that necessitate equidistant neighboring nodes and isotropic properties in simulations. By understanding their coordinate transformations and calculation methods, you can leverage this grid system for robust applications in both two-dimensional and three-dimensional contexts.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.