|
| 1 | +# ===== Default : Default |
| 2 | +GEM_HOME = '/home/tux/.gem/ruby/2.6.0' |
| 3 | +require 'vecmath' |
| 4 | +require 'forwardable' |
| 5 | + |
| 6 | +# Here we use the JRubyArt Vec2D class, and AppRender class (for vec.to_vertex) |
| 7 | +# Further we use the power of ruby (metaprogramming) to make Branch enumerable |
| 8 | +# and use Forwardable to define which enumerable methods we want to use. |
| 9 | +visor_class :Branch do |
| 10 | + include Enumerable |
| 11 | + extend Forwardable |
| 12 | + def_delegators(:@children, :<<, :each, :length) |
| 13 | + # variance angle for growth direction per time step |
| 14 | + THETA = Math::PI / 6 |
| 15 | + # max segments per branch |
| 16 | + MAX_LEN = 100 |
| 17 | + # max recursion limit |
| 18 | + MAX_GEN = 3 |
| 19 | + # branch chance per time step |
| 20 | + BRANCH_CHANCE = 0.05 |
| 21 | + # branch angle variance |
| 22 | + BRANCH_THETA = Math::PI / 3 |
| 23 | + attr_reader :position, :dir, :path, :children, :xbound, :speed, :ybound, :app |
| 24 | + |
| 25 | + def initialize(pos, dir, speed) |
| 26 | + @renderer ||= AppRender.new(graphics) |
| 27 | + @position = pos |
| 28 | + @dir = dir |
| 29 | + @speed = speed |
| 30 | + @path = [] |
| 31 | + @children = [] |
| 32 | + @xbound = Boundary.new(0, width) |
| 33 | + @ybound = Boundary.new(0, height) |
| 34 | + path << pos |
| 35 | + end |
| 36 | + |
| 37 | + def run |
| 38 | + grow |
| 39 | + display |
| 40 | + end |
| 41 | + |
| 42 | + private |
| 43 | + |
| 44 | + # NB: use of both rotate! (changes original) rotate (returns a copy) of Vec2D |
| 45 | + def grow |
| 46 | + check_bounds(position + (dir * speed)) if path.length < MAX_LEN |
| 47 | + @position += (dir * speed) |
| 48 | + dir.rotate!(rand(-0.5..0.5) * THETA) |
| 49 | + path << position |
| 50 | + if (length < MAX_GEN) && (rand < BRANCH_CHANCE) |
| 51 | + branch_dir = dir.rotate(rand(-0.5..0.5) * BRANCH_THETA) |
| 52 | + self << Branch.new(position.copy, branch_dir, speed * 0.99) |
| 53 | + end |
| 54 | + each(&:grow) |
| 55 | + end |
| 56 | + |
| 57 | + def display |
| 58 | + begin_shape |
| 59 | + stroke(255) |
| 60 | + no_fill |
| 61 | + path.each { |vec| vec.to_vertex(@renderer) } |
| 62 | + end_shape |
| 63 | + each(&:display) |
| 64 | + end |
| 65 | + |
| 66 | + def check_bounds(pos) |
| 67 | + dir.x *= -1 if xbound.exclude? pos.x |
| 68 | + dir.y *= -1 if ybound.exclude? pos.y |
| 69 | + end |
| 70 | +end |
| 71 | + |
| 72 | +# we are looking for excluded values |
| 73 | +Boundary = Struct.new(:lower, :upper) do |
| 74 | + def exclude?(val) |
| 75 | + !(lower...upper).cover? val |
| 76 | + end |
| 77 | +end |
| 78 | + |
| 79 | +@root = Branch.new( |
| 80 | + Vec2D.new(0, height / 2), |
| 81 | + Vec2D.new(1, 0), |
| 82 | + 10.0 |
| 83 | +) |
| 84 | + |
| 85 | +def draw |
| 86 | + background(0) |
| 87 | + root.run |
| 88 | +end |
| 89 | +# ===== circles : circles |
| 90 | +GEM_HOME = '/home/tux/.gem/ruby/2.6.0' |
| 91 | +require 'vecmath' |
| 92 | + |
| 93 | +visor_class :Circle do |
| 94 | + RADIUS = 50 |
| 95 | + |
| 96 | + def initialize |
| 97 | + @pos = Vec2D.new(rand(width), rand(height)) |
| 98 | + end |
| 99 | + |
| 100 | + def update(a) |
| 101 | + @pos.y += speed * a |
| 102 | + @pos.y = -RADIUS if @pos.y > height + RADIUS |
| 103 | + end |
| 104 | + |
| 105 | + def draw(a) |
| 106 | + with_translate @pos.x, @pos.y do |
| 107 | + scale a |
| 108 | + noStroke |
| 109 | + fill 255 |
| 110 | + ellipse 0, 0, RADIUS, RADIUS |
| 111 | + end |
| 112 | + end |
| 113 | +end |
| 114 | + |
| 115 | +@circles = [] |
| 116 | +150.times do |
| 117 | + @circles << Circle.new |
| 118 | +end |
| 119 | + |
| 120 | +@speed ||= 10 |
| 121 | +set_range :@speed, 0, 30 |
| 122 | + |
| 123 | +def draw |
| 124 | + rectMode CORNER |
| 125 | + noStroke |
| 126 | + fill 0, 50 |
| 127 | + rect 0, 0, width, height |
| 128 | + |
| 129 | + @circles.each_with_index do |circle, i| |
| 130 | + a = i / @circles.size.to_f |
| 131 | + |
| 132 | + circle.update a + 0.1 |
| 133 | + circle.draw a + 0.1 |
| 134 | + end |
| 135 | +end |
| 136 | + |
| 137 | +set_blend_mode MULTIPLY |
0 commit comments