Recently I've been looking at Povray, pyprocessing, and cfdg (version 3.0) as tools for creating digital images. I have branched two separate blogs where I mainly explore jruby + processing and processing.py

Wednesday, 16 February 2011

Alhambra Tiling Sketch in processing.py

   1 """
   2 alhambra.py by Martin Prout
   3 A first stab at converting my Alhambra tiling sketch to processing.py
   4 """
   5 import math
   6 
   7 xValues = [100, 300, 500, 700]
   8 yValues = [50 * math.sqrt(3), 150 * math.sqrt(3), 250 * math.sqrt(3), 350 * math.sqrt(3)]
   9 
  10 
  11 def setup():
  12     """
  13     processing setup
  14     """
  15     size(700, 650)
  16     background(21, 15, 72)
  17     smooth()
  18     render()
  19     saveFrame("alahambra.png")
  20     
  21     
  22 def draw_hexagon(xpos, ypos, sz, theta):
  23     """
  24     hexagon draw function
  25     """
  26     beginShape()
  27     for i in range(0, 6):
  28         vertex(xpos + sz*math.cos((math.pi/3 * i) + theta), ypos + sz*math.sin((math.pi/3 * i) +theta));
  29     endShape(CLOSE)
  30         
  31         
  32 def draw_triangle(x0, y0, sz, couluer, disp):
  33     """
  34     Wavy triangle draw function
  35     """
  36     # Calculate the triangle points
  37     pts = []
  38     pts.append(PVector(x0, y0 - sz/math.sqrt(3)))               # A
  39     pts.append(PVector(x0 - 0.5 * sz, y0 + (math.sqrt(3)*sz)/6))# B
  40     pts.append( PVector(x0 + 0.5 * sz, y0 + (math.sqrt(3)*sz)/6))# C
  41     pts.append(get_mid_point(pts[0], pts[1]))                    # Ab
  42     pts.append(get_mid_point(pts[1], pts[2]))                    # Bc
  43     pts.append(get_mid_point(pts[0], pts[2]))                    # Ca
  44     pts.append(get_mid_point(pts[0], pts[3]))                    # Aba
  45     adjustBezier(pts[6], math.pi/3, disp*sz)                     # Aba
  46     pts.append(get_mid_point(pts[3], pts[1]))                    # Abb
  47     adjustBezier(pts[7], math.pi/3, -disp*sz)                    # Abb
  48     pts.append(get_mid_point(pts[1], pts[4]))
  49     adjustBezier(pts[8], math.pi/2, -disp*sz)
  50     pts.append(get_mid_point(pts[4], pts[2]))
  51     adjustBezier(pts[9], math.pi/2, disp*sz)
  52     pts.append(get_mid_point(pts[2], pts[5]))
  53     adjustBezier(pts[10], -math.pi/3, -disp*sz)
  54     pts.append(get_mid_point(pts[5], pts[0]))
  55     adjustBezier(pts[11], -math.pi/3, disp*sz)
  56     # render triangle
  57     fill(couluer)
  58     beginShape()
  59     vertex(pts[0].x, pts[0].y)
  60     bezierVertex(pts[0].x, pts[0].y, pts[6].x, pts[6].y, pts[3].x, pts[3].y)
  61     bezierVertex(pts[3].x, pts[3].y, pts[7].x, pts[7].y, pts[1].x, pts[1].y)
  62     bezierVertex(pts[1].x, pts[1].y, pts[8].x, pts[8].y, pts[4].x, pts[4].y)
  63     bezierVertex(pts[4].x, pts[4].y, pts[9].x, pts[9].y, pts[2].x, pts[2].y)
  64     bezierVertex(pts[2].x, pts[2].y, pts[10].x, pts[10].y, pts[5].x, pts[5].y)
  65     bezierVertex(pts[5].x, pts[5].y, pts[11].x, pts[11].y, pts[0].x, pts[0].y)
  66     endShape(CLOSE)
  67     # set color and render small hexagon
  68     fill(255)
  69     draw_hexagon(x0 + 4, y0, sz * 0.214, 0)
  70     
  71     
  72 def adjustBezier(base, theta, disp):
  73     """
  74     Adjust the Bezier control point
  75     """
  76     base.add(PVector(math.cos(theta)*disp, math.sin(theta)*disp))
  77     
  78     
  79 def get_mid_point(a, b):
  80     """
  81     Use PVector operations to calculate the mid point
  82     """
  83     mid = PVector.add(a, b)
  84     mid.div(2)
  85     return mid
  86     
  87     
  88 def render():
  89     """
  90     Tesselate the wavy triangles
  91     """
  92     for column in range(0,len(xValues)):
  93         for row in range(0,len(yValues)):
  94             if (row % 2 == 0):
  95                 if (column % 3 ==0):
  96                     draw_triangle(xValues[column], yValues[row], 200, color(255, 0, 0), 0.32)
  97                 else:
  98                     draw_triangle(xValues[column], yValues[row], 200, color(255, 0, 255), 0.32)
  99             elif ((column - 2) % 3 ==0):
 100                 draw_triangle(xValues[column] - 100, yValues[row], 200, color(255, 0, 0), 0.32)
 101             else:
 102                 draw_triangle(xValues[column] - 100, yValues[row], 200, color(255, 0, 255), 0.32)
 103                 
 104 def draw():
 105     """
 106     Is the processing draw loop
 107     """
 108     pass

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
Pembrokeshire, United Kingdom
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2