How can I calculate center coordinates for an infinite series of tangent hexagons?

167 Views Asked by At

I'm trying to draw an infinite field of tangent hexagons, and am having trouble determining the centers in the series, as you can see from my current result

Not sure how to format my formulas since it's code, I'll do my best:

    oddColumn = (column % 2) - 1
    offsetY = oddColumn * radius

    centerX = diameter * column
    centerY = diameter * row + offsetY

So as you can see, I'm currently offsetting the Y position by the radius for oddColumns, but that leads to the hexagons being horizontally detached.

I need to calculate the corresponding 'offsetX' for odd columns (which should be negative to draw the rectangles closer to the previous columns) but don't know a formula for this.

1

There are 1 best solutions below

3
On BEST ANSWER

I think the change that you want is best accomplished by making the horizontal positions $\frac{\sqrt 3}2$ diameters apart. The code might be ...

centerX = diameter * column * 0.5 * sqrt(3)

They should fit nicely once you do that.

**** EDIT

Label the vertices of a regular hexagon A through F starting at the top and proceeding clockwise.

The vertical height of the hexagon will be the distance between diametrically opposite points A and D.

The horizontal width of the hexagon will be the distance between the non diametrically opposite points F and B.

Consider the triangle FAB, since this is a regular hexagon... $$|FA=|AB| = r $$ and these two sides meet at an angle of 120 degrees.

Applying cosine Law ... $$ |FB|^2=r^2+r^2-2(r)(r)\cos(120)=3r^2 $$

So the width $|FB| = \sqrt 3 r = \frac {\sqrt 3}2 d$