Iterating Paths

July 2, 2008 | drawing

By sending the "increment" method to a Path object, you can specify changes that will continue to add up or "drift" each time the path 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 = :type => :image, :filename => 'iterating.png', :size => [400,400]

# create a petal shape with base at (0,0), size 40x150, and bulge at 30px
shape =,0,40,150,30)
# add a circle
# color it red

# increment shape parameters by the specified amount each iteration,
# or by a random value selected from the specified range
shape.increment(:rotation, 5.0)
#shape.increment(:scale, 0.95)
shape.increment(:scalex, 0.99)
shape.increment(:scaley, 0.96)
shape.increment(:x, 10.0)
shape.increment(:y, 12.0)
shape.increment(:saturation, -0.1..0.1)
shape.increment(:brightness, -0.1..0.1)
shape.increment(:alpha, -0.1..0.1)

# draw 200 petals on the canvas starting at location 50,200