Randomizing Paths

July 2, 2008 | drawing

By applying the "randomize" method to a Path object, you can specify attributes that should be randomized each time the object is drawn to the canvas.


framework 'cocoa'
require 'rubygems'
require 'hotcocoa/graphics'
include HotCocoa
include Graphics

# create a new 400x400 pixel canvas to draw on
canvas = Canvas.new :type => :image, :filename => 'randomizing.png', :size => [400,400]

# create a flower shape
shape = Path.new
petals = 5
for i in 1..petals do
  shape.petal(0,0,40,100)       # petal at x,y with width,height
  shape.rotate(360 / petals)    # rotate by 1/5th

# randomize shape parameters
shape.randomize :fill, Color.blue.complementary
shape.randomize :stroke, Color.blue.complementary
shape.randomize :strokewidth, 1.0..10.0
shape.randomize :rotation, 0..360
shape.randomize :scale, 0.5..1.0
shape.randomize :scalex, 0.5..1.0
shape.randomize :scaley, 0.5..1.0
shape.randomize :alpha, 0.5..1.0
# shape.randomize :hue, 0.5..0.8
shape.randomize :saturation, 0.0..1.0
shape.randomize :brightness, 0.0..1.0
shape.randomize :x, -100.0..100.0
shape.randomize :y, -100.0..100.0

# draw 50 flowers starting at the center of the canvas