Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

replaced map with vine

  • Loading branch information...
commit 9f822bde13039dab105db50a3483c71387472bdb 1 parent 06eec38
@jashkenas authored
View
54 context_free.rb
@@ -5,13 +5,23 @@ module Processing
class ContextFree
+ include Processing::Proxy
+
attr_accessor :rules, :app
- STOP_SIZE = 1.5
AVAILABLE_OPTIONS = [:x, :y, :rotation, :size, :flip, :color, :hue, :saturation, :brightness]
HSB_ORDER = {:hue => 0, :saturation => 1, :brightness => 2}
+ # Define a context-free system. Use this method to create a ContextFree
+ # object. Call render() on it to make it draw.
+ def self.define(&block)
+ cf = ContextFree.new
+ cf.instance_eval &block
+ cf
+ end
+
+
# Initialize a bare ContextFree object with empty recursion stacks.
def initialize
@app = $app
@@ -30,14 +40,6 @@ def initialize
@values[option_name]
end
end
-
-
- # When you start a context-free drawing, you can initialize the variables
- # with starting values.
- def setup(some_hash)
- @starting_values = some_hash
- @starting_values[:stop_size] ||= STOP_SIZE
- end
# Here's the first serious method: A Rule has an
@@ -57,7 +59,7 @@ def rule(rule_name, prob=1, &proc)
def #{rule_name}(options)
merge_options(@values, options)
pick = determine_rule(#{rule_name.inspect})
- @finished = true if @values[:size] < STOP_SIZE
+ @finished = true if @values[:size] < @values[:stop_size]
unless @finished
get_ready_to_draw
pick[1].call(options)
@@ -159,16 +161,19 @@ def rewind
# Render the is method that kicks it all off, initializing the options
# and calling the first rule.
- def render(rule_name)
+ def render(rule_name, starting_values={})
@values = {:x => 0, :y => 0,
:rotation => 0, :flip => false,
:size => 20, :width => 20, :height => 20,
- :color => [0.5, 0.5, 0.5]}
- @values.merge!(@starting_values)
+ :color => [0.5, 0.5, 0.5],
+ :stop_size => 1.5}
+ @values.merge!(starting_values)
@finished = false
@app.reset_matrix
+ @app.rect_mode CENTER
+ @app.ellipse_mode CENTER
@app.no_stroke
- @app.color_mode(App::HSB, 1.0)
+ @app.color_mode HSB, 1.0
@app.translate @values[:start_x], @values[:start_y]
self.send(rule_name, {})
end
@@ -196,40 +201,27 @@ def get_shape_values(some_options)
# methods, but hopefully triangles will be added soon.
def square(some_options=nil)
size, options = *get_shape_values(some_options)
- @app.rect(-(size/2), -(size/2), size, size)
+ @app.rect(0, 0, size, size)
end
def circle(some_options=nil)
size, options = *get_shape_values(some_options)
- @app.ellipse(-(size/2), -(size/2), size, size)
+ @app.ellipse(0, 0, size, size)
end
- def ellipse(some_options=nil)
+ def ellipse(some_options={})
rot = some_options[:rotation]
@app.rotate(rot) if rot
size, options = *get_shape_values(some_options)
width = options[:width] || options[:size]
height = options[:height] || options[:size]
- @app.oval(options[:x], options[:y], width, height)
+ @app.oval(options[:x] || 0, options[:y] || 0, width, height)
@app.rotate(-rot) if rot
end
alias_method :oval, :ellipse
end
-
- # Processing::App gets a context_free method, as a hook for defining the rules.
- class App
-
- # Instantiate and setup a new ContextFree object.
- def context_free(&block)
- drawing = ContextFree.new
- drawing.instance_eval &block
- return drawing
- end
-
- end
-
end
View
39 samples/city.rb
@@ -1,57 +1,64 @@
load_library 'context_free'
def setup_the_city
- @city = context_free do
- rule :whole do
+
+ @city = ContextFree.define do
+
+ rule :neighborhood do
split do
- quad :x => -0.25, :y => -0.25
+ block :x => -0.25, :y => -0.25
rewind
- quad :x => 0.25, :y => -0.25
+ block :x => 0.25, :y => -0.25
rewind
- quad :x => 0.25, :y => 0.25
+ block :x => 0.25, :y => 0.25
rewind
- quad :x => -0.25, :y => 0.25
+ block :x => -0.25, :y => 0.25
end
end
- rule :quad do
- fill :size => 0.85
+ rule :block do
+ buildings :size => 0.85
end
- rule :quad, 5 do
- whole :size => 0.5, :rotation => rand(4), :hue => rand(2), :brightness => rand(3)
+ rule :block, 5 do
+ neighborhood :size => 0.5, :rotation => rand(8)-4, :hue => rand(2), :brightness => rand + 0.75
end
- rule :quad, 0.1 do
+ rule :block, 0.1 do
# Do nothing
end
- rule :fill do
+ rule :buildings do
square
end
+
end
+
end
def setup
size 600, 600
- setup_the_city
smooth
+ setup_the_city
@background = color 255, 255, 255
- the_color = [0.1, 0.1, 0.1]
- @city.setup :start_x => width/2, :start_y => height/2, :size => height/3, :color => the_color
draw_it
end
+
def draw
# Do nothing
end
+
def draw_it
background @background
- @city.render :whole
+ @city.render :neighborhood,
+ :start_x => width/2, :start_y => height/2,
+ :size => height/2.5, :color => [0.1, 0.1, 0.1]
end
+
def mouse_clicked
draw_it
end
View
1  samples/data/java_args.txt
@@ -0,0 +1 @@
+-Xss8M
View
71 samples/map.rb
@@ -1,71 +0,0 @@
-load_library 'context_free'
-
-def setup_the_map
-
- @map = context_free do
- @shrink = 0.985
-
- rule :map do
- square
- split do
- wall :size => 1
- rewind
- wall :rotation => 180
- end
- end
-
- rule :wall do
- wall :y => 0.95, :rotation => 1, :size => @shrink
- end
-
- rule :wall do
- square
- wall :y => 0.95, :rotation => -1, :size => @shrink
- end
-
- rule :wall, 0.09 do
- square
- split do
- wall :y => 0.95, :rotation => 90, :size => @shrink
- rewind
- wall :y => 0.95, :rotation => -90, :size => @shrink
- end
- end
-
- rule :wall, 0.005 do
- split do
- wall :y => 0.97, :rotation => 90, :size => 1.5
- rewind
- wall :y => 0.97, :rotation => 90, :size => 1.5
- end
- end
- end
-end
-
-
-def setup
- size(600, 600)
- setup_the_map
- no_stroke
- smooth
- frame_rate 1
- the_color = [0.1, 0.1, 0.5]
- @map.setup :start_x => width/2, :start_y => height/2, :size => height/90,
- :color => the_color, :stop_size => 0.01
- draw_it
-end
-
-def draw_the_background
- color_mode RGB, 1
- background 0.9, 0.8, 0.7
-end
-
-def draw_it
- Kernel::srand(@srand) if @srand
- draw_the_background
- @map.render :map
-end
-
-def mouse_clicked
- draw_it
-end
View
14 samples/tree.rb
@@ -9,7 +9,8 @@ def setup_the_trees
c.slider :srand, 0..100
end
- @tree = context_free do
+ @tree = ContextFree.define do
+
rule :seed do
square
leaf :y => 0 if size < 4.5 && rand < 0.018
@@ -37,17 +38,18 @@ def setup_the_trees
the_size = rand(25)
the_x = [1, 0, 0, 0][rand(4)]
circle :size => the_size, :hue => 0.15, :saturation => 1.25,
- :brightness => 1.9, :x => the_x, :color => [0.3, 0.5]
+ :brightness => 1.9, :x => the_x, :color => [0.95, 0.15]
end
rule :flower do
- split :brightness => rand(1.3)+4.7, :set_width => rand(15)+10, :set_height => rand(2)+2 do
+ split :saturation => 0, :brightness => rand(1.3)+4.7, :set_width => rand(15)+10, :set_height => rand(2)+2 do
oval :rotation => 0
oval :rotation => 45
oval :rotation => 90
oval :rotation => 135
end
end
+
end
end
@@ -59,13 +61,11 @@ def setup
no_stroke
smooth
frame_rate 5
- the_color = [0.5, 0.7, 0.8]
- @tree.setup :start_x => width/2, :start_y => height+20, :size => height/60, :color => the_color
draw_it
end
def draw
-
+ # Do nothing.
end
def draw_the_background
@@ -85,7 +85,7 @@ def draw_the_background
def draw_it
Kernel::srand(@srand) if @srand
draw_the_background
- @tree.render :seed
+ @tree.render :seed, :start_x => width/2, :start_y => height+20, :size => height/60, :color => [0.7, 0.15, 0.8]
end
def mouse_clicked
View
59 samples/vine.rb
@@ -0,0 +1,59 @@
+load_library 'context_free'
+
+def setup_the_vine
+
+ @vine = ContextFree.define do
+
+ shrink = 0.961
+
+ rule :root do
+ split do
+ shoot :y => 1
+ rewind
+ shoot :rotation => 180
+ end
+ end
+
+ rule :shoot do
+ square
+ shoot :y => 0.98, :rotation => 5, :size => shrink + rand * 0.05, :brightness => 0.990
+ end
+
+ rule :shoot, 0.02 do
+ square
+ split do
+ shoot :rotation => 90
+ rewind
+ shoot :rotation => -90
+ end
+ end
+
+ end
+end
+
+
+def setup
+ size 700, 700
+ setup_the_vine
+ no_stroke
+ color_mode HSB, 1.0
+ smooth
+ draw_it
+end
+
+
+def draw
+ # Do nothing.
+end
+
+
+def draw_it
+ background 0.75, 1.0, 0.15
+ @vine.render :root, :size => height/75, :color => [0.75, 0.1, 0.9],
+ :start_x => width/2, :start_y => height/2
+end
+
+
+def mouse_clicked
+ draw_it
+end
Please sign in to comment.
Something went wrong with that request. Please try again.