Class: Doodl::MorphLayout

Inherits:
Layout
  • Object
show all
Defined in:
lib/layout/morph_layout.rb

Instance Attribute Summary

Attributes inherited from Layout

#graph, #iterations, #locations

Instance Method Summary collapse

Methods inherited from Layout

#center_x, #center_y, #deep_copy, #finished?, #get_nearest_node, #get_nearest_node_within, #layout, #move, #rotate, #set_location

Constructor Details

#initialize(view, layout = RandomLayout) ⇒ MorphLayout

Returns a new instance of MorphLayout.



8
9
10
11
12
13
# File 'lib/layout/morph_layout.rb', line 8

def initialize(view, layout = RandomLayout)
  super(view)
  @target_layout_class = layout
  @steps = 45
  @motion = :retard
end

Instance Method Details

#accelerate(step) ⇒ Object



55
56
57
# File 'lib/layout/morph_layout.rb', line 55

def accelerate(step)
  Math.cos((step / @steps.to_f) * Math::PI/2)
end

#dist(step) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/layout/morph_layout.rb', line 41

def dist(step)
  if @motion == :accelerate
    accelerate(step)
  elsif @motion == :retard
    retard(step)
  else
    smooth(step)
  end
end

#dolayout(graph) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/layout/morph_layout.rb', line 31

def dolayout(graph)
  (1..@steps).each do |step|
    graph.each_node do |node|
      @locations[node] = @target[node] + (@source[node]-@target[node]) * dist(step)
    end
    changed
    notify_observers(self)
  end
end

#init(graph) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/layout/morph_layout.rb', line 15

def init(graph)
  if graph.node_data(:layout)
    @source = graph.node_data(:layout).clone
  else
    sl = RandomLayout.new(@view)
    sl.graph = graph
    sl.layout
    @source = sl.locations.clone
  end
  tl = @target_layout_class.new(@view)
  tl.graph = graph
  tl.layout
  @target = tl.locations.clone
  @locations = graph.node_data(:layout)
end

#retard(step) ⇒ Object



59
60
61
# File 'lib/layout/morph_layout.rb', line 59

def retard(step)
  1 - Math.sin((step / @steps.to_f) * Math::PI/2)
end

#smooth(step) ⇒ Object



51
52
53
# File 'lib/layout/morph_layout.rb', line 51

def smooth(step)
   (0.5 * Math.cos((step / @steps.to_f) * Math::PI)) + 0.5
end