Class: Ranicoma::Design::SpBox

Inherits:
Base
  • Object
show all
Defined in:
lib/ranicoma/design/spbox.rb

Constant Summary collapse

LINE =
1.0/30
MINSIZE =
LINE*2
COLS =
i( red blue yellow green )
COLMAKERS =
[]

Instance Attribute Summary

Attributes inherited from Base

#rng

Instance Method Summary collapse

Methods inherited from Base

add_subclass, #fill, inherited, #points_str, #rainbow, #rand, #rand_rotate, #rect_element, #rectpos, #stroke, subclasses

Methods included from Util

#element

Constructor Details

#initialize(rng) ⇒ SpBox

Returns a new instance of SpBox.



49
50
51
52
53
# File 'lib/ranicoma/design/spbox.rb', line 49

def initialize(rng)
  super
  @cols=create_cols
  @col_ix=rng.rand(@cols.size)
end

Instance Method Details

#createObject



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ranicoma/design/spbox.rb', line 93

def create
  total = Rect.new( LINE, LINE, 1-LINE*2, 1-LINE*2 )
  dir=i(h v).sample( random:rng )
  [
    element("rect", height:1, width:1, **fill(:black)),
    case dir
    when :h
      hsubbox(total, 0)
    when :v
      vsubbox(total, 0)
    end
  ].flatten
end

#create_colsObject



16
17
18
# File 'lib/ranicoma/design/spbox.rb', line 16

def create_cols
  send COLMAKERS.sample(random:rng)
end

#fill?(depth) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/ranicoma/design/spbox.rb', line 60

def fill?(depth)
  prob = [0.1, 0.4][depth] || 0.6
  rng.rand < prob
end

#hsubbox(rc, depth) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ranicoma/design/spbox.rb', line 65

def hsubbox( rc, depth )
  ratio=rng.rand(0.3..0.7)
  rest = (rc.w - LINE)
  return rect_element(rc, randcol) if rest<MINSIZE
  left = rest*ratio
  right = rest-left
  rc_l = Rect.new( rc.x, rc.y, left, rc.h )
  rc_r = Rect.new( rc.right-right, rc.y, right, rc.h )
  [
    ( fill?(depth) ? rect_element(rc_l, randcol) : vsubbox(rc_l, depth+1) ),
    ( fill?(depth) ? rect_element(rc_r, randcol) : vsubbox(rc_r, depth+1) )
  ]
end

#randcolObject



55
56
57
58
# File 'lib/ranicoma/design/spbox.rb', line 55

def randcol
  @col_ix = (@col_ix+1) % @cols.size
  @cols[ @col_ix ]
end

#vsubbox(rc, depth) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ranicoma/design/spbox.rb', line 79

def vsubbox( rc, depth )
  ratio=rng.rand(0.3..0.7)
  rest = (rc.h - LINE)
  return rect_element(rc, randcol) if rest<MINSIZE
  top = rest*ratio
  bottom = rest-top
  rc_t = Rect.new( rc.x, rc.y, rc.w, top )
  rc_b = Rect.new( rc.x, rc.bottom-bottom, rc.w, bottom)
  [
    ( fill?(depth) ? rect_element(rc_t, randcol) : hsubbox(rc_t, depth+1 ) ),
    ( fill?(depth) ? rect_element(rc_b, randcol) : hsubbox(rc_b, depth+1 ) )
  ]
end