Maze Generator - Hexagons

Hexagon Code Example

Two major orientations for hexagons.
image missing

Calculate X and Y distances from the center.
image missing

# ================================================================ # draw a regular hexagon # ---------------------------------------------------------------- # A regular hexagon is a 6-edged convex polygon with all edges # and internal angles equal, featuring 120 degree interior # angles, a 720 degree angle sum. Unlike irregular hexagons, # they are equilateral and equiangular, possessing high # symmetry used in nature (honeycombs) and engineering. # ---------------------------------------------------------------- # # points that define a regular hexagon # edges that define a regular hexagon # # p1 # . . # upper-left . . upper-right # . . # p6 p2 # . . # left . p0 . right # . . # p5 p3 # . . # lower-left . . lower-right # . . # p4 # # ---------------------------------------------------------------- # A 30-60-90 degree triangle is a special right triangle with # edge lengths ratio 1 : sqrt(3) : 2 (short leg : long leg : # hypotenuse). The shortest edge (x) is opposite the 30 # degree angle, the longer leg (x*sqrt(3)) is opposite the # 60 degree angle, and the hypotenuse (2x) is opposite the # 90 degree angle. # # / # / # / # / 30 deg # +---------- Note: each interior angle is 120 deg # | 90 deg # | # # ---------------------------------------------------------------- # regular hexagon (each edge is 1.0 unit long. each interior # angle is 120 degrees.) # # dy = math.sin(math.radians(30.0)) # 0.5 # dx = math.cos(math.radians(30.0)) # 0.8660254037844386 # # p0 = (0.0, 0.0) # p1 = (0.0, -0.5-dy) # p2 = ( dx, -0.5) # p3 = ( dx, 0.5) # p4 = (0.0, 0.5+dy) # p5 = (-dx, 0.5) # p6 = (-dx, -0.5) # # (P0 is the center of the graphics window and the hexagon) # (coordinates are graphics window coordinates) # ---------------------------------------------------------------- # # Cartesian Coordinate System Graphics Window Coordinates # (viewer is at +z infinity) (viewer is at +z infinity) # # +y (0,0) # | +------------- +x # | | # | | # | | # | | # +------------- +x +y # (0,0) # # ================================================================ import math import graphics as gr from enum import IntEnum import coordinate_conversion as cc VERBOSE = False hex_dy = math.sin(math.radians(30.0)) # 0.5 hex_dx = math.cos(math.radians(30.0)) # 0.8660254037844386 # ---------------------------------------------------------------- # ---- each hexagon has six edges that are stored in a # ---- list. enums are used to specify which edge. # ---------------------------------------------------------------- class EDGES(IntEnum): URIGHT = 0 RIGHT = 1 LRIGHT = 2 LLEFT = 3 LEFT = 4 ULEFT = 5 #----------------------------------------------------------------- # ---- create/draw a line graphics-object # ---- Note: x0,y0,x1,y1 are graphics window coordinates # ---------------------------------------------------------------- def draw_line(win,x0,y0,x1,y1,width=1,color='black'): lobj = gr.Line(gr.Point(x0,y0),gr.Point(x1,y1)) lobj.setWidth(width) lobj.setFill(color) lobj.draw(win) return lobj # ---------------------------------------------------------------- # ---- create/draw a hexagon graphics-object # ---- draw a hexagon around a center point scaled up # ---- note: coordinates are graphics window coordinates # ---------------------------------------------------------------- def draw_hex(win,center_point,scale,outline_width=2, fill_color=None,outline_color='black'): # ---- distances from center point dx = scale * hex_dx dy = scale *(hex_dy + 0.5) # ---- hexagon corner points x = center_point[0] y = center_point[1] - dy p1 = (x,y) x = center_point[0] + dx y = center_point[1] - scale*0.5 p2 = (x,y) x = center_point[0] + dx y = center_point[1] + scale*0.5 p3 = (x,y) x = center_point[0] y = center_point[1] + dy p4 = (x,y) x = center_point[0] - dx y = center_point[1] + scale*0.5 p5 = (x,y) x = center_point[0] - dx y = center_point[1] - scale*0.5 p6 = (x,y) if VERBOSE: print(f'center = {center_point}') print(f'scale = {scale}') print(f'p1 = {p1}') print(f'p2 = {p2}') print(f'p3 = {p3}') print(f'p4 = {p4}') print(f'p5 = {p5}') print(f'p6 = {p6}') hobj = gr.Polygon(gr.Point(p1[0],p1[1]), gr.Point(p2[0],p2[1]), gr.Point(p3[0],p3[1]), gr.Point(p4[0],p4[1]), gr.Point(p5[0],p5[1]), gr.Point(p6[0],p6[1])) hobj.setOutline(outline_color) hobj.setWidth(outline_width) if fill_color is not None: hobj.setFill(fill_color) hobj.draw(win) # ---------------------------------------------------------------- # ---- main # ---------------------------------------------------------------- if __name__ == '__main__': # ---- graphics window and X,Y axes win_title = 'Regular Hexagon' win_width = 800 win_height = 800 win_color = 'white' win = gr.GraphWin(win_title,win_width,win_height) win.setBackground(win_color) center_x = round(win_width/2.0) center_y = round(win_height/2.0) x_axis = draw_line(win,0,center_y, round(win_width-1),center_y) y_axis = draw_line(win,center_x,0, center_x,round(win_height-1)) # --- draw hexagon a at the center of the graphics window scale = 100.0 hobj = draw_hex(win,(center_x,center_y),scale) # ---- add more hexagons hobj = draw_hex(win,(center_x,center_y),40.0, outline_color='red') hobj = draw_hex(win,(center_x,center_y),150.0, outline_color='blue') hobj = draw_hex(win,(center_x,center_y),200.0, outline_color='green') click = win.getMouse() win.close() print()

Another Example

Points are equal distance from the center.
image missing

# ================================================================ # draw a regular hexagon # ---------------------------------------------------------------- # A regular hexagon is a 6-sided convex polygon with all sides # and internal angles equal, featuring 120 degree interior # angles, a 720 degree angle sum. Unlike irregular hexagons, # they are equilateral and equiangular, possessing high # symmetry used in nature (honeycombs) and engineering. # ---------------------------------------------------------------- # # points that define a regular hexagon # (not to scale) # # p1 # p6 | p2 # \ | / # p0 # / | \ # p5 | p3 # p4 # # ================================================================ import math import graphics as gr VERBOSE = False #----------------------------------------------------------------- # ---- create/draw a line graphics-object # ---- Note: x0,y0,x1,y1 are graphics window coordinates # ---------------------------------------------------------------- def draw_line(win,x0,y0,x1,y1,width=1,color='black'): lobj = gr.Line(gr.Point(x0,y0),gr.Point(x1,y1)) lobj.setWidth(width) lobj.setFill(color) lobj.draw(win) return lobj # ---------------------------------------------------------------- # ---- create/draw a hexagon graphics-object # ---- draw a hexagon around a center point scaled up # ---- note: coordinates are graphics window coordinates # ---------------------------------------------------------------- def draw_hexagon(win,center_point,scale,outline_width=2, fill_color=None,outline_color='black'): # ---- create a list of polygon points points = [] points.append(center_point) ang_start = 90 ang_delta = 360/6 ang = ang_start for i in range(6): x = math.cos(math.radians(ang)) * scale + center_point[0] y = math.sin(math.radians(ang)) * scale + center_point[1] point = (x,y,ang) points.append(point) ang -= ang_delta if VERBOSE: print() print(f'center = {points[0]}') print(f'scale = {scale}') print(f'ang start = {ang_start}') print(f'ang delta = {ang_delta}') for idx in range(1,6+1): p = f'({points[idx][0]:>8.4f},' +\ f '{points[idx][1]:>8.4f})' +\ f' ang={points[idx][2]:<.4f}' print(f'points[{idx}] {p}') # ---- draw polygon hobj = gr.Polygon(gr.Point(points[1][0],points[1][1]), gr.Point(points[2][0],points[2][1]), gr.Point(points[3][0],points[3][1]), gr.Point(points[4][0],points[4][1]), gr.Point(points[5][0],points[5][1]), gr.Point(points[6][0],points[6][1])) hobj.setOutline(outline_color) hobj.setWidth(outline_width) if fill_color is not None: hobj.setFill(fill_color) hobj.draw(win) return hobj # ---------------------------------------------------------------- # ---- main # ---------------------------------------------------------------- if __name__ == '__main__': # ---- graphics window and X,Y axes win_title = 'Regular Hexagon' win_width = 800 win_height = 800 win_color = 'white' win = gr.GraphWin(win_title,win_width,win_height) win.setBackground(win_color) center_x = round(win_width/2.0) center_y = round(win_height/2.0) x_axis = draw_line(win,0,center_y, round(win_width-1),center_y) y_axis = draw_line(win,center_x,0, center_x,round(win_height-1)) # --- draw hexagon at the center of the graphics window scale = 200.0 hobj = draw_hexagon(win,(center_x,center_y),scale) # ---- add more hexagrams hobj = draw_hexagon(win,(center_x,center_y),40.0, outline_color='red') hobj = draw_hexagon(win,(center_x,center_y),100.0, outline_color='blue') hobj = draw_hexagon(win,(center_x,center_y),150.0, outline_color='green') click = win.getMouse() win.close() print()