Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Tuesday 17 November 2009

Tessellated "curvy" triangles in ruby-processing


# alhambra.rb by Martin Prout

class CurvyTriangle < Processing::App

  def setup()  
    size(700, 650)
    @x_values = [100, 300, 500, 700]
    @y_values = [50 * Math.sqrt(3), 150 * Math.sqrt(3), 250 * Math.sqrt(3), 350 * Math.sqrt(3)]
    background 21, 15, 72
    smooth
    render
    save_frame "alahambra.png"
  end

  def draw_hexagon(xpos, ypos, sz, theta)
    begin_shape
      6.times do |i|
      vertex(xpos + sz*Math.cos((Math::PI/3 * i) + theta), ypos + sz*Math.sin((Math::PI/3 * i) +theta));
    end
    end_shape CLOSE
  end

  def draw_triangle(x0, y0, sz, color, disp)
    # Calculate  triangle points
    pts = Array.new(12)
    pts[0] = PVector.new(x0, y0 - sz/Math.sqrt(3))               # A
    pts[1] = PVector.new(x0 - 0.5 * sz, y0 + (Math.sqrt(3)*sz)/6)# B
    pts[2] = PVector.new(x0 + 0.5 * sz, y0 + (Math.sqrt(3)*sz)/6)# C
    pts[3] = get_mid_point(pts[0], pts[1])                       # Ab
    pts[4] = get_mid_point(pts[1], pts[2])                       # Bc
    pts[5] = get_mid_point(pts[0], pts[2])                       # Ca
    pts[6] = get_mid_point(pts[0], pts[3])                       # Aba
    adjust_bezier(pts[6], Math::PI/3, disp*sz)                   # Aba
    pts[7] = get_mid_point(pts[3], pts[1])                       # Abb
    adjust_bezier(pts[7], Math::PI/3, -disp*sz)                  # Abb
    pts[8] = get_mid_point(pts[1], pts[4])
    adjust_bezier(pts[8], Math::PI/2, -disp*sz)
    pts[9] = get_mid_point(pts[4], pts[2])
    adjust_bezier(pts[9], Math::PI/2, disp*sz)
    pts[10] = get_mid_point(pts[2], pts[5])
    adjust_bezier(pts[10], -Math::PI/3, -disp*sz)
    pts[11] = get_mid_point(pts[5], pts[0])
    adjust_bezier(pts[11], -Math::PI/3, disp*sz)
    # render triangle
    fill color
    begin_shape()
      vertex(pts[0].x, pts[0].y)
      bezier_vertex(pts[0].x, pts[0].y, pts[6].x, pts[6].y, pts[3].x, pts[3].y)
      bezier_vertex(pts[3].x, pts[3].y, pts[7].x, pts[7].y, pts[1].x, pts[1].y)
      bezier_vertex(pts[1].x, pts[1].y, pts[8].x, pts[8].y, pts[4].x, pts[4].y)
      bezier_vertex(pts[4].x, pts[4].y, pts[9].x, pts[9].y, pts[2].x, pts[2].y)
      bezier_vertex(pts[2].x, pts[2].y, pts[10].x, pts[10].y, pts[5].x, pts[5].y)
      bezier_vertex(pts[5].x, pts[5].y, pts[11].x, pts[11].y, pts[0].x, pts[0].y)
      end_shape(CLOSE)
      # set color and render small hexagon
      fill(255);
      draw_hexagon(x0 + 4, y0, sz * 0.214, 0);
    end
  
    def adjust_bezier(base, theta, disp)
      base.add(PVector.new(Math.cos(theta)*disp, Math.sin(theta)*disp))
    end
  
    def get_mid_point(a, b)
      mid = PVector.add(a, b)
      mid.div(2)
      return mid
    end
  
    def render
      @x_values.length.times do |column|
    
      @y_values.length.times do |row|
        if (row % 2 == 0) then
          if (column % 3 ==0) then
            draw_triangle(@x_values[column], @y_values[row], 200, color(255, 0, 0), 0.32)
          else
            draw_triangle(@x_values[column], @y_values[row], 200, color(255, 0, 255), 0.32)
          end
        # end
        # offset tiles so that they tesselate
        elsif ((column - 2) % 3 ==0) then
            draw_triangle(@x_values[column] - 100, @y_values[row], 200, color(255, 0, 0), 0.32)
          else
            draw_triangle(@x_values[column] - 100, @y_values[row], 200, color(255, 0, 255), 0.32)
          end
        end
      end
    end
end


No comments:

Post a Comment

Followers

Blog Archive

About Me

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