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

Monday, 10 May 2010

Op Art using Voronoi Cell, Ruby Processing


load_libraries 'mesh'         # http://www.leebyron.com/else/mesh/
import 'megamu.mesh.Voronoi'
require 'set'
attr_reader :voronoi, :points, :left, :right, :top, :bottom

def setup()
  size(1000, 1000)
  color_mode HSB, 1.0
  @points = Set.new
  @points.clear
  @left = MRect.new 10, height/2, 20, height, 20
  @right = MRect.new 990, height/2, 20, height, 20
  @top = MRect.new width/2, 10, width, 20, 20
  @bottom = MRect.new width/2, 990, width, 20, 20
  smooth
#  no_loop
end

def draw
  @points.clear
  shape = MPoly.new width/3, height/3, 8, 150, 4
  shape1 = MPoly.new width/2, height/2, 8, 120, 4
  shape2 = MPoly.new width*0.667, height*0.667, 8, 150, 4
  shape3 = MPoly.new width/3, height*0.667, 3, 150, 4
  shape4 = MPoly.new width*0.667, height/3, 3, 150, 4
  @points.merge(left.points)
  @points.merge(right.points)
  @points.merge(top.points)
  @points.merge(bottom.points)
  @points.merge(shape.points)
  @points.merge(shape1.points)
  @points.merge(shape2.points)
  @points.merge(shape3.points)
  @points.merge(shape4.points)
  voronoi = Voronoi.new((@points.to_a).to_java(Java::float[]))
  regions = voronoi.get_regions
  regions.each do |region|
    region_coordinates = region.get_coords
    fill(rand)
    region.draw(self)
  end
end

class MRect # border for now
        attr_reader :x, :y, :w, :h, :skip, :points     
  def initialize x, y, w, h, skip
    @x = x - w/2
    @y = y - h/2
    @w = w
    @h = h
    @skip = skip
    @points = calculate_points
  end
  def calculate_points
    mpoints = Array.new
    (w/skip).times do
      (h/skip).times do
        mpoints.push([rand*w + x, rand*h + y])
      end
    end
    return mpoints
  end         
end

class MPoly # shape
  attr_reader :x, :y, :sides, :size, :theta, :delta, :repeats, :len, :points           
  def initialize x, y, sides, size, density
    @delta = -PI/sides
    @x = x
    @y = y
    @sides = sides
    @size = size
    @theta = 2 * PI/sides
    @repeats = density
    @len = size/repeats
    @points = calculate_points
  end
  def calculate_points
    mpoints = Array.new
    repeats.times do |i|
      sides.times do |j|
        mpoint_x = x + (i + 1) * len * cos(j * theta + delta)
        mpoint_y = y - (i + 1) * len * sin(j * theta + delta)
        mpoints.push [mpoint_x, mpoint_y]
      end
    end
    return mpoints
  end
  # def rotate delta
    # @delta += delta
    # calculate_points    
  # 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