SHARE
TWEET

Alphard

a guest Apr 11th, 2015 30 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """
  2. Created on Sun Apr 12 01:22:11 2015
  3.  
  4. @author: Alphard
  5. """
  6.  
  7. import math
  8. import Image
  9. import ImageDraw
  10.  
  11. def hexagonVertices(edge, center):
  12.     x, y = center
  13.     for angle in range(0, 360, 60):
  14.         x += edge*math.cos(math.radians(angle))
  15.         y += edge*math.sin(math.radians(angle))
  16.         yield x, y
  17.    
  18. def drawHexagon(draw, vertices, label):
  19.     if not label:
  20.         label = (150, 150, 150)
  21.     draw.polygon(list(vertices), outline='black', fill=label)
  22.  
  23. rows = 30
  24. cols = 6
  25. offsetX = 25;
  26. offsetY = 25;
  27. edge = 20;
  28.  
  29. labels = [[0 for x in range(cols)] for x in range(rows)]
  30.  
  31. for k in range(5, 13, 2):
  32.     labels[k][3] = 'red'
  33.     labels[k][2] = 'red'
  34.  
  35. for k in range(4, 12, 2):
  36.     labels[k][3] = 'red'
  37.     labels[k][2] = 'red'
  38.     labels[k][1] = 'red'
  39.    
  40. for k in range(12, 28, 2):
  41.     labels[k][2] = 'green'
  42.  
  43. labels[25][3] = 'green'
  44. labels[23][4] = 'green'
  45. labels[24][3] = 'green'
  46. labels[22][4] = 'green'
  47. labels[21][4] = 'green'
  48. labels[22][3] = 'green'
  49.  
  50.  
  51. image = Image.new('RGB', (int(3*edge*cols+2*offsetX), int(edge*rows+2*offsetY)), 'white')
  52. draw = ImageDraw.Draw(image)
  53.  
  54. for row in range(rows):
  55.     for col in range(cols):
  56.         oddOffset = 0 if row%2==1 else 1.5*edge
  57.         x = 3*edge*col + oddOffset + offsetX
  58.         y = 3**0.5*0.5*edge*row + offsetY
  59.         drawHexagon(draw, hexagonVertices(edge, (x, y)), labels[row][col])
  60.        
  61. image.show()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top