Class: Automaton::Layout
- Inherits:
-
Object
- Object
- Automaton::Layout
- Defined in:
- lib/layout.rb
Overview
An ad-hoc implmenentation of a spring-force algorithm to layout the automaton graph.
Defined Under Namespace
Classes: Node
Constant Summary collapse
- SPRING_LENGTH =
3
- REPULSION =
3
- ATTRACTION =
0.40
- TIMESTEP =
0.25
- ENERGY_TRESHOLD =
0.1
- DAMPING =
0.8
Instance Attribute Summary collapse
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
Instance Method Summary collapse
- #force_direct ⇒ Object
-
#initialize(*nodes) ⇒ Layout
constructor
A new instance of Layout.
- #normalize ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*nodes) ⇒ Layout
Returns a new instance of Layout.
20 21 22 |
# File 'lib/layout.rb', line 20 def initialize(*nodes) @nodes = nodes end |
Instance Attribute Details
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
19 20 21 |
# File 'lib/layout.rb', line 19 def nodes @nodes end |
Instance Method Details
#force_direct ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/layout.rb', line 25 def force_direct nodes.each{|node| node.velocity = Vector[0.0, 0.0]} total_kinetic_energy = ENERGY_TRESHOLD + 1 until total_kinetic_energy < ENERGY_TRESHOLD total_kinetic_energy = 0.0 nodes.each do |node| net_force = (nodes - [node]).reduce(Vector[0, 0]) do |sum, other_node| sum += node.repulsion(other_node) + node.attraction(other_node) end node.velocity = (node.velocity + net_force * TIMESTEP) * DAMPING node.position += node.velocity * TIMESTEP total_kinetic_energy += node.speed**2 end end end |
#normalize ⇒ Object
41 42 43 44 45 46 |
# File 'lib/layout.rb', line 41 def normalize min_x = nodes.map{|node| node.x}.min min_y = nodes.map{|node| node.y}.min min = Vector[min_x - 1, min_y - 1] nodes.each{|node| node.position -= min} end |
#to_s ⇒ Object
48 49 50 |
# File 'lib/layout.rb', line 48 def to_s nodes.map{|node| node.position}.inspect end |