-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrowing_tree.rb
More file actions
72 lines (59 loc) · 1.22 KB
/
growing_tree.rb
File metadata and controls
72 lines (59 loc) · 1.22 KB
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
require 'ruby-processing'
class TreeDrawer < Processing::App
def setup
size width, height
@original_length = 180
@color = ["FF", "00", "00"]
color_mode RGB, 1.0
smooth
end
def draw
background 0
color = get_color
red = color[0..1]
green = color[2..3]
blue = color[4..5]
stroke @color[0].to_i(16), @color[1].to_i(16), @color[2].to_i(16)
translate width / 2, height
line 0, 0, 0, -@original_length
translate 0, -@original_length
angle = (mouse_x.to_f / width.to_f) * 160.to_f
@rad = radians(angle)
branch @original_length
end
def branch h
h *= 0.66
return if h < 2
push_matrix
rotate @rad
color = get_color
stroke(color[0], color[1], color[2])
line 0,0, 0, -h
translate 0, -h
branch h
pop_matrix
push_matrix
rotate -@rad
line 0,0, 0, -h
translate 0, -h
branch h
pop_matrix
end
def get_color
y = mouse_y > 0 ? mouse_y : 250
c = "FFFFFF".to_i(16) / height.to_f * y
c = c.to_i.to_s(16)
while c.size < 6
c += '0'
end
puts "#{c[0..1]}, #{c[2..3]}, #{c[4..5]}}"
[c[0..1].to_i, c[2..3].to_i, c[4..5].to_i]
end
def mouse_clicked
if mouse_button == LEFT
@color = get_color
puts "color"
end
end
end
TreeDrawer.new :width => 650, :height => 650