hexworld.html


001| '''Hex World.'''
002| ####################################################################################################
003| from os import path
004| from math import pi,sin,cos,radians
005| from canvas import Canvas
006| ####################################################################################################
007| BasePath = r'C:\CJS\prj\Python\blog'
008| NbrSegs = 7                             # Number of segments (in a hemisphere)
009| R = 1.0                                 # Nominal radius is 1.0 (unit circle)
010| HexWidth = R * 2.0                      # Width of a single hexagon
011| HexHeight = R * sin(radians(60.0))      # Height of a single hexagon
012| SegWidth = 6.0                          # Width of any segment (nominal)
013| SegHeight = 20 * HexHeight              # Height of all segments
014| 
015| fname = 'hexes.png'
016| dims = (2400,960)  # H = W/2.5
017| xmin, xmax = (0.0, (NbrSegs*SegWidth)+1)
018| ymin, ymax = (0.0,  SegHeight+0.40)
019| cborder = (0,0,0)
020| thold = 1.10
021| 
022| x0 = 0.0
023| x1 = cos(radians(60.0))
024| 
025| xadj = 0.25
026| yadj = 0.25
027| 
028| LtPts =[(x1 if ix&1 else x0, float(ix)*HexHeight) for ix in range(21)]
029| RtPts =[(x0 if ix&1 else x1, float(ix)*HexHeight) for ix in range(21)]
030| 
031| plst = []
032| hlst = []
033| glst = []
034| 
035| # Generate segment point lists...
036| for ix in [n*6.0 for n in range(0,NbrSegs)]:
037|     # South "Avenues" (but these are just borders)...
038|     p1 = ((2.5+xadj+ix), ((0.0*HexHeight)+yadj))
039|     p2 = ((2.5+xadj+ix), ((2.0*HexHeight)+yadj))
040|     glst.append((p1,p2))
041|     # Northern Avenues...
042|     p1 = ((2.5+xadj+ix), ((20.0*HexHeight)+yadj))
043|     p2 = ((2.5+xadj+ix), ((18.0*HexHeight)+yadj))
044|     glst.append((p1,p2))
045| 
046|     p1s = [((p[0]+0.0+ix+xadj), (p[1]+yadj)) for p in LtPts]
047|     p2s = [((p[0]+1.5+ix+xadj), (p[1]+yadj)) for p in RtPts[1:-1]]
048|     p3s = [((p[0]+3.0+ix+xadj), (p[1]+yadj)) for p in LtPts[1:-1]]
049|     p4s = [((p[0]+4.5+ix+xadj), (p[1]+yadj)) for p in RtPts]
050| 
051|     # Segment connector horizontals...
052|     if 0 < len(plst):
053|         for p1,p2 in zip(plst[-1], p1s):
054|             if (p2[0]-p1[0]) < thold:
055|                     hlst.append((p1,p2))
056| 
057|     # Horizontals...
058|     for p1,p2 in zip(p1s[1:-1], p2s):
059|         if (p2[0]-p1[0]) < thold:
060|                 hlst.append((p1,p2))
061|     for p1,p2 in zip(p2s, p3s):
062|         if (p2[0]-p1[0]) < thold:
063|                 hlst.append((p1,p2))
064|     for p1,p2 in zip(p3s, p4s[1:-1]):
065|         if (p2[0]-p1[0]) < thold:
066|                 hlst.append((p1,p2))
067| 
068|     # Top and bottom horizontals...
069|     hlst.append((p1s[0],p4s[0]))
070|     hlst.append((p1s[-1],p4s[-1]))
071| 
072|     # Vertical zig-zags...
073|     plst.append(p1s)
074|     plst.append(p2s)
075|     plst.append(p3s)
076|     plst.append(p4s)
077| 
078| # Add the last segment (right-most)...
079| pts = [((p[0]+(NbrSegs*SegWidth)+xadj), (p[1]+yadj)) for p in LtPts]
080| plst.append(pts)
081| for p1,p2 in zip(plst[-2], plst[-1]):
082|     if (p2[0]-p1[0]) < thold:
083|             hlst.append((p1,p2))
084| 
085| 
086| # Create the image...
087| im = Canvas(dims, ((xmin,xmax),(ymin,ymax)))
088| im.border([(0,0,0),(31,31,31),(63,63,63),(95,95,95)], lw=1)
089| 
090| # Draw vertical zig-zag borders...
091| for pts in plst:
092|     p0 = pts[0]
093|     for p in pts[1:]:
094|         im.line(p0, p, c=cborder, lw=3)
095|         p0 = p
096| 
097| # Draw horizontal borders...
098| for pts in hlst:
099|     im.line(pts[0],pts[1], c=cborder, lw=3)
100| 
101| # Draw vertical alleys...
102| for pts in glst:
103|     im.line(pts[0],pts[1], c=cborder, lw=3)
104| 
105| # Save the image...
106| im.save(path.join(BasePath,fname))
107| 
108| ####################################################################################################
109| '''eof'''
110|