-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.rb
83 lines (70 loc) · 1.99 KB
/
tree.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# http://processing.org/learning/topics/tree.html
# by Joe Holt
require 'ruby-processing'
class Sketch < Processing::App
def setup
color_mode RGB, 1
frameRate 30
smooth
@x = 0.0
@dx = width / 100
@start_time = Time.now
@frame_time = nil
end
def radians(x)
return x * (Math::PI / 180)
end
def draw
t = Time.now
if @frame_time
fps = 1.0 / (t - @frame_time)
# printf "%0.1ffps\n", fps
end
@frame_time = t
background 0
stroke 1, 1, 1
# Let's pick an angle 0 to 90 degrees based on the mouse position
a = (@x / width) * 90
# Convert it to radians
@theta = radians(a)
# Start the tree from the bottom of the screen
translate(width/2,height)
# Draw a line 60 pixels
h = height / 3
line(0,0,0,-h)
# Move to the end of that line
translate(0,-h)
# Start the recursive branching!
branch(h)
@x += @dx
if @x < 0
puts "Time after this iteration: " + (Time.now - @start_time).to_s
end
if @x > width || @x < 0
@dx = - @dx
@x += @dx * 2
end
end
def branch(h)
# Each branch will be 2/3rds the size of the previous one
h *= 0.66;
# All recursive functions must have an exit condition!!!!
# Here, ours is when the length of the branch is 2 pixels or less
if h > 2
pushMatrix # Save the current state of transformation (i.e. where are we now)
rotate(@theta) # Rotate by theta
line(0,0,0,-h) # Draw the branch
translate(0,-h) # Move to the end of the branch
branch(h) # Ok, now call myself to draw two new branches!!
popMatrix # Whenever we get back here, we "pop" in order to restore the previous matrix state
# Repeat the same thing, only branch off to the "left" this time!
pushMatrix
rotate(- @theta)
line(0,0,0,-h)
translate(0,-h)
branch(h)
popMatrix
end
end
end
Sketch.new(:width => 200, :height => 200, :title => "Auto Tree")