Class: Ranicoma::Design::Geji

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

Constant Summary collapse

PROJ_METHODS =
[]

Instance Attribute Summary collapse

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) ⇒ Geji

Returns a new instance of Geji.



14
15
16
17
18
19
# File 'lib/ranicoma/design/geji.rb', line 14

def initialize(rng)
  super
  @line_width = 1.0/80
  @projsize = rand(0.1..0.3)
  @center_rc = Rect.new( projsize, projsize, 1-projsize*2, 1-projsize*2 )
end

Instance Attribute Details

#center_rcObject (readonly)

Returns the value of attribute center_rc.



27
28
29
# File 'lib/ranicoma/design/geji.rb', line 27

def center_rc
  @center_rc
end

#line_widthObject (readonly)

Returns the value of attribute line_width.



25
26
27
# File 'lib/ranicoma/design/geji.rb', line 25

def line_width
  @line_width
end

#projsizeObject (readonly)

Returns the value of attribute projsize.



26
27
28
# File 'lib/ranicoma/design/geji.rb', line 26

def projsize
  @projsize
end

Instance Method Details

#bezier(rc, corners) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ranicoma/design/geji.rb', line 76

def bezier(rc, corners)
  q = corners.each_cons(2).map{ |(x0,y0),(x1,y1)|
    mx = (x0+x1)/2.0
    my = (y0+y1)/2.0
    "Q #{x0} #{y0} #{mx} #{my} "
  }
  path = <<~PATH
    M #{rc.x} #{rc.bottom}
    #{q.join}
    L #{corners.last.join(" ")}
    L #{rc.x} #{rc.bottom}
  PATH
  element( "path", d:path, **proj_attr )
end

#center_rectObject



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
# File 'lib/ranicoma/design/geji.rb', line 35

def center_rect
  gx = center_rc.w * rand(0.05..0.3)
  gy = center_rc.h * rand(0.05..0.3)
  w0 = (center_rc.w-gx*2)*rand(0.6..0.9)
  h0 = (center_rc.h-gy*2)*rand(0.6..0.9)
  w1 = (center_rc.w-gx*2)*rand(0.6..0.9)
  h1 = (center_rc.h-gy*2)*rand(0.6..0.9)
  subrects = if rand(2)==0
    [
      Rect.new( center_rc.x+gx, center_rc.y+gy, w0, h0 ),
      Rect.new( center_rc.right-gx-w1, center_rc.bottom-gy-h1, w1, h1 ),
    ]
  else
    [
      Rect.new( center_rc.right-gx-w0, center_rc.y+gy, w0, h0 ),
      Rect.new( center_rc.x+gx, center_rc.bottom-gy-h1, w1, h1 ),
    ]
  end.shuffle( random:rng )
  if rand(2)==0
    h = [h0, h1].min * rand(0.3..1.1)
    w = [w0, w1].min * rand(0.3..1.1)
    subrects.push( Rect.new( center_rc.cx-w/2, center_rc.cy-h/2, w, h ) )
  end
  cols = eqcolors(subrects.size+1)
  makeattr=->(){
    {
      rx:line_width*3,
      **fill(cols.pop),
      **stroke(:black, line_width)
    }
  }
  ([ center_rc  ]+subrects).map{ |rc|
    element( "rect", **rectpos(rc), **makeattr[] )
  }
end

#createObject



238
239
240
241
242
243
244
245
# File 'lib/ranicoma/design/geji.rb', line 238

def create
  [
    Array.new(4){ |pos|
      projects(pos)
    },
    center_rect
  ].flatten
end

#eqcolors(n) ⇒ Object



29
30
31
32
33
34
# File 'lib/ranicoma/design/geji.rb', line 29

def eqcolors(n)
  base=rand(3.0)
  Array.new(n){ |ix|
    rainbow(base+3.0*ix/n)
  }
end

#proj_attrObject



21
22
23
# File 'lib/ranicoma/design/geji.rb', line 21

def proj_attr
  fill(rainbow(rand(3.0))).merge(stroke(:black, line_width))
end

#projects(pos) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/ranicoma/design/geji.rb', line 216

def projects(pos)
  count = rand(2..5)
  rot = "rotate(#{90*pos},0.5,0.5)"
  proj_rects = center_rc.hsplit(*([1]*count))
  proj_rects.each{ |e|
    e.x += e.w*0.1
    e.w *= 0.8
    e.y=line_width
    e.h = projsize
  }
  pattern = rand(1..2**count-1) | rand(1..2**count-1)
  element("g", transform:rot ){
    Array.new(count){ |ix|
      if pattern[ix]==0
        []
      else
        send PROJ_METHODS.sample(random:rng), proj_rects[ix]
      end
    }.flatten
  }
end

#shorten_rc(rc) ⇒ Object



71
72
73
74
# File 'lib/ranicoma/design/geji.rb', line 71

def shorten_rc(rc)
  d = rand(rc.h/2)
  Rect.new( rc.x, rc.y+d, rc.w, rc.h-d )
end