Class: Ranicoma::Design::PolyPoly

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

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

Returns a new instance of PolyPoly.



12
13
14
15
16
# File 'lib/ranicoma/design/polypoly.rb', line 12

def initialize(rng)
  super
  @psize = rng.rand(6..20)
  @line_width = 1.0/(@psize*5)
end

Instance Attribute Details

#line_widthObject (readonly)

Returns the value of attribute line_width.



19
20
21
# File 'lib/ranicoma/design/polypoly.rb', line 19

def line_width
  @line_width
end

#psizeObject (readonly)

Returns the value of attribute psize.



18
19
20
# File 'lib/ranicoma/design/polypoly.rb', line 18

def psize
  @psize
end

Instance Method Details

#colors(n) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ranicoma/design/polypoly.rb', line 21

def colors(n)
  f = lambda{ |t0|
    v = lambda{ |t|
      case t
      when 0..1 then t
      when 1..2 then 2-t
      else 0
      end
    }[t0 % 3]
    (v**0.5*255).round
  }
  Array.new(n){ |i|
    t = i*3.0/n+2
    [f[t],f[t+1],f[t+2]]
  }
end

#createObject



81
82
83
84
85
# File 'lib/ranicoma/design/polypoly.rb', line 81

def create
  [
    poly([0.5,0.5],0.5-line_width)
  ].flatten
end

#poly(center, r) ⇒ Object



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

def poly(center, r)
  delta = Math::PI*2/psize*0.7
  dirs = Array.new(psize){ |ix| Math::PI*2*ix / psize + rng.rand(delta) }
  outer = dirs.map{ |e|
    [Math.cos(e)*r+center[0], Math.sin(e)*r+center[1]]
  }
  edelta = rng.rand(-0.2..0.2) * Math::PI*2
  ri = r * rng.rand(0.2..0.5)
  cd = rng.rand(Math::PI)
  rdelta = rng.rand((r-ri-line_width)*0.8)
  inner_cx = center[0] + Math.cos(cd)*rdelta
  inner_cy = center[1] + Math.sin(cd)*rdelta
  inner = dirs.map{ |e|
    t = e + edelta
    [Math.cos(t)*ri+inner_cx, Math.sin(t)*ri+inner_cy]
  }
  use = Array.new(inner.size){ rng.rand<0.8 }
  cols = rand_rotate(colors(use.count(true)))
  center_poly = element( "polygon",
    points:points_str(inner),
    **fill(i(black white gray).sample( random:rng)),
    **stroke(:black, line_width),
    "stroke-linejoin": :round )
  Array.new( outer.size ){ |ix|
    if ! use[ix]
      []
    else
      pts = [
        outer[ix % psize],
        outer[(ix+1) % psize],
        inner[(ix+1) % psize],
        inner[ix % psize]
      ]
      element(
        "polygon",
        points:points_str(pts),
        **fill(cols.pop),
        **stroke(:black, line_width),
        "stroke-linejoin": :round )
    end
  } + [center_poly]
end