Need help to calculate a point in a hexagon.

I wrote a script to generate a 3d hexagon which can be hollow or completely filled. The generated mesh looks like this.


I am calculating P1 and P2 for each 60-degree angel and than set it up. If its hollow i render some wall faces between those pairs. But that results in some ugly corners like the one you see in the picture.

For those corners (wich have hollow neigbor tiles) i have to calculate P3 and P4 to get a nice flush corner. I think P1, P2 and P3 or P1, P2 and P4 will always be a isosceles triangle but i cant my head around it. How do i cacluate the Vector3 for P3 with the given information? Radius to P1, Radius to P2, isosceles triangle. It should be half of the difference of the radiuses in one axis and the disctance (thickness of border) between the outer lines in the other axis, shouldn’t it?

And than i add that amounts to the vector of P1? Maybe someone could help me out with an example calculation.

I’m not sure isosceles triangles should be part of hex cell vertex math… wouldn’t it all be equilateral triangles as far as the points?

I think I would just make a six-sided circle (six pieces of pizza), then make another one slightly larger as your source corner verts.

The outer borders would end up being long slender irregular triangles, as you have it above, but they would be sourced from the smaller and slightly-larger six-sided circles.

Then combine the center, inner and outer edges into the geometry you need.

If you offset them properly, they’ll be a hex grid.

This code makes circles:

The project contains full scene and script examples to exercise it all.

Not only are these triangles isosceles, they’re also equilateral. I’ll put how I arrived at that conclusion in a spoiler:
Why are the triangles equilateral The sum of the inner angles of a polygon with n sides is (n - 2) * 180°, so 6 * 180° = 720° for hexagons. That means the angle between P2P4 and P2P3 is 1/6 of that or 120°, which makes the angle between P2P4 and P2P1 half that or 60°. For the same reason and due to P1P4P2P3 being a parallelogram the angle between P1P4 and P1P2 is also 60°, leaving exactly 60° for the last angle in that triangle.
With that information, you can calculate the points you’re after like so:

  • for a given corner point (let’s call it P2 to match your image), calculate the directions to the two surrounding corner points (e.g. Vector2 toNextCorner = (nextCorner - p2).normalized;)
  • To calculate P3 and and P4, add the respective directional vector scaled by the distance between P1 and P2 to the position of the current corner (e.g. Vector2 p3 = p2 + Vector3.Distance(p2, p1) * toNextCorner;)

I hope that’s understandable.

1 Like

You’re absolutly right about that. Thanks for making it so clear and understandable!

And the approach you mentionied absolutly makes sense too and seems to be the simpelest one to me. I thought of calculating the perpendicular of the line between P1 and P2 and stuff like that. I am not used to calculate with vectors. To calculate the direction and than use this to calculate the third point of the triangle is realy straight forward.

Was able to generate nice flush corners now, only have to add the top face and its done. Thx again!

8414595--1112469--upload_2022-9-4_18-46-17.png

1 Like