# Irregular Hexagon Tile Map Woes

## Recommended Posts

joeyburgett    100
[ using Tiled and cocos2d ]
I have an issue and could use some guidance.

Note: for the purposes of this post, I'm referencing the information on this site [url="http://www.mathgoodies.com/lessons/vol1/area_trapezoid.html"]Trapezoid[/url].

Scenario: Irregular Hexagon's (horizontal sides on top and bottom) - 340 Width, 170 Height (both measured as pixels) on a map of 20x20.

By definition a true hexagon has 6 equal sides. This doesn't; hence irregular.

Essentially, these tiles are to isosceles trapezoids. All I know is the full tiles size - no lengths of the outer sides. If I turn the tile into two equal trapezoids and solve for one of them I have:

Angle = 45 (am I correct to assume 45 degrees since the width is twice the height?)

b1 = ?
b2 = 340
h = .5(170) = 85

I can solve for the two triangles on the ends since I know the length of one side. After dusting off the trig books, I came up with:

a = 85
b = ?
c = ?

Solving to find the triangles hypotenuse:

c = 85 / Sin(45) = 120

Now use Pythagorean theorem to solve for the last side:

85^2 + b^2 = 120^2
b = sqrt(7175)
b = 84.7 (Rounded to 85)

I also could have solved using:

b = Tan(45) * 85 = 1 * 85 = 85

Now that I have the other side of the triangle, I can find b1:

b1 = b2 - [ 2 * 85 ]
b1 = 340 - [170]
b1 = 170

Now I can find the perimeter, the sum of all the sides:

P = 170 + 340 + 120 + 120
Perimeter = 750

Multiply by 2 since the above only solves for half the hex.
Perimeter = 1500

Now I can find the trapezoids area using the formula at the above link:

A = h [ (b1 + b2) / 2 ]
A = 85 [ (170 + 340) / 2 ]
A = 85 [ 250 ]
Area = 21250

Multiply by 2 since the above only solves for half the hex.
Area = 42500

First, am I making this WAY more complicated then it needs to be? I ultimately need to determine the tile (if any) touch coordinates falls. Would I have to brute force by iterating thru each tile to check if it's selected?

I'd appreciate any help.

##### Share on other sites
Tiblanc    560
If I understand correctly, you want to figure out which tile the user clicks/touchs? You could use a mask translated to the tile position and check the corresponding pixel color.

##### Share on other sites
joeyburgett    100
[quote name='Tiblanc' timestamp='1310329442' post='4833442']
If I understand correctly, you want to figure out which tile the user clicks/touchs? You could use a mask translated to the tile position and check the corresponding pixel color.
[/quote]

Think that would require creating another layer and I would still have to find the touch position on that one?

##### Share on other sites
Tiblanc    560
You don't need another layer. When you do the check, translate the screen coordinates into the tile's local coordinates and check where it falls on the mask's bitmap. That mask is somehow linked to the tile you want to check, but never displayed. Check this article at the bottom : [url="http://www.gamedev.net/page/reference/index.html/_/reference/programming/isometric-and-tile-based-games/298/isometric-n-hexagonal-maps-part-i-r747"]http://www.gamedev.net/page/reference/index.html/_/reference/programming/isometric-and-tile-based-games/298/isometric-n-hexagonal-maps-part-i-r747[/url]