# Triangular tile map

Recall from the previous post there are only three regular polygons that can be used as tiles. Let's have a look at triangles now. Such tiles are practically not used in games because there is no direct path on the map and the game unit should be turned at each tile. Besides, it is rather hard to place units within acute corners because a lot of free space is wasted.

A triangular tile map can be easily converted from a square tile map transformed to a parallelogram:

» |

Let me introduce several definitions to be used in further calculations:

`size`

- The tile size defines distance between the central points of neighbors. The size is set during a map initialization and is used to calculate other values.
`x,y`

- Coordinates of a point.
`i,j`

- Indices of a tile.
`r = size/2`

- Radius of the inscribed circle.
`h = r + size`

- Height of the triangle.
`w = h / sin 60°`

- Width of the triangle.

### Calculating the central point of a tile by its index

`x = (i - j) × w/2`

** y = j × h + r** if a triangle is turned over

** y = j × h + size** otherwise

### Calculating the index of a tile by a point inside

`j = (int) y/h`

`i = (int) 2 × x/w + j + 1`

`d`

_{x} = 2 × x - i × w

`d`

_{y} = y - j × h

** d** only if triangle is turned over

_{y}= h - d

_{y}

** i--** only if (

**)**

`w × d`_{y} > h × d_{x}

### Calculating distance between two tiles

`d`

_{i} = 2 × |i_{1}»1 - i_{2}»1|

`d`

_{j} = 2 × |j_{1} - j_{2}|

** d = d** if

_{i}+ d

_{j}

**or**

`(i`_{1} ≤ i_{2})&(j_{1} ≥ j_{2})

`(i`_{1} ≥ i_{2})&(j_{1} ≤ j_{2})

** d = max(d** otherwise

_{i},d

_{j})

The following calculations should be made if one triangle is normal and another one is turned over:

** d++** if first triangle is turned over and

`(j`_{1} - j_{2}) < (i_{1} - i_{2}) / 2

** d++** if second triangle is turned over and

`(j`_{1} - j_{2}) ≥ (i_{1} - i_{2}) / 2

** d++** otherwise

### Running the example

The following example shows an implementation of the triangular tiling map. Each tile contains information about its indices and a distance from the selected tile.

The source code is **available**, though, it is not optimal. It is `diff`

-optimized.

- Login or register to post comments
- Printer-friendly version
- malenkov's blog
- 3529 reads