Skip to content

Instantly share code, notes, and snippets.

@urschrei
Last active May 4, 2022 11:57
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urschrei/17cf0be92ca90a244a91 to your computer and use it in GitHub Desktop.
Save urschrei/17cf0be92ca90a244a91 to your computer and use it in GitHub Desktop.
Python Hexgrid
import math
def calculate_polygons(startx, starty, endx, endy, radius):
"""
Calculate a grid of hexagon coordinates of the given radius
given lower-left and upper-right coordinates
Returns a list of lists containing 6 tuples of x, y point coordinates
These can be used to construct valid regular hexagonal polygons
You will probably want to use projected coordinates for this
"""
# calculate side length given radius
sl = (2 * radius) * math.tan(math.pi / 6)
# calculate radius for a given side-length
# (a * (math.cos(math.pi / 6) / math.sin(math.pi / 6)) / 2)
# see http://www.calculatorsoup.com/calculators/geometry-plane/polygon.php
# calculate coordinates of the hexagon points
# sin(30)
p = sl * 0.5
b = sl * math.cos(math.radians(30))
w = b * 2
h = 2 * sl
# offset start and end coordinates by hex widths and heights to guarantee coverage
startx = startx - w
starty = starty - h
endx = endx + w
endy = endy + h
origx = startx
origy = starty
# offsets for moving along and up rows
xoffset = b
yoffset = 3 * p
polygons = []
row = 1
counter = 0
while starty < endy:
if row % 2 == 0:
startx = origx + xoffset
else:
startx = origx
while startx < endx:
p1x = startx
p1y = starty + p
p2x = startx
p2y = starty + (3 * p)
p3x = startx + b
p3y = starty + h
p4x = startx + w
p4y = starty + (3 * p)
p5x = startx + w
p5y = starty + p
p6x = startx + b
p6y = starty
poly = [
(p1x, p1y),
(p2x, p2y),
(p3x, p3y),
(p4x, p4y),
(p5x, p5y),
(p6x, p6y),
(p1x, p1y)]
polygons.append(poly)
counter += 1
startx += w
starty += yoffset
row += 1
return polygons
@benmaier
Copy link

Can I refactor this into a PyPI package under the MIT License?

@urschrei
Copy link
Author

Hi ben. Of course, if you'd like to (please include me as an author for copyright purposes). However, note that http://postgis.net/docs/manual-dev/ST_HexagonGrid.html exists if your data are in a DB, also note that I published https://github.com/urschrei/hexcover a couple of years ago, which might be helpful.

@benmaier
Copy link

awesome, thanks! didn't know about this postgis function, might useful in the future, similar to your other package. Atm I just needed a simple procedure that tiles a rectangle with hexagonal polygons, and this gist is the only thing I could find and was exactly what I was looking for. I just thought it'd be cool to have it easily accessible through PyPI. Of course I'll list you as the author. Cheers!

@urschrei
Copy link
Author

No prob. See also this package: https://github.com/alexkaz2/hexalattice

@benmaier
Copy link

huh. How did I not find this one. thanks

@cornhundred
Copy link

hi @urschrei, thanks for sharing the code. I was unable to import hexcover after pip installing see colab notebook. Would we be able to use this Gist code as part of a larger project if we include you as the author?

@urschrei
Copy link
Author

urschrei commented May 4, 2022

Hi @cornhundred I've just uploaded 0.5.0 to pypi – try installing that, otherwise feel free to use the gist code.

@cornhundred
Copy link

thanks @urschrei, I'm still having trouble getting 0.5.0 to install (see updated Colab notebook). I'll go ahead with the Gist code - thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment