Class: Automaton::Layout::Node

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

Overview

A spring connected, repulsing node used by the spring force algorithm

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, x, y) ⇒ Node

Returns a new instance of Node.



56
57
58
59
60
61
# File 'lib/layout.rb', line 56

def initialize(name, x, y)
  @name = name
  @position = Vector[x, y]
  @velocity = Vector[0, 0]
  @connections = Set.new
end

Instance Attribute Details

#connectionsObject

Returns the value of attribute connections.



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

def connections
  @connections
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#positionObject

Returns the value of attribute position.



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

def position
  @position
end

#velocityObject

Returns the value of attribute velocity.



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

def velocity
  @velocity
end

Instance Method Details

#attraction(other) ⇒ Object



79
80
81
82
83
# File 'lib/layout.rb', line 79

def attraction(other)
  return Vector[0, 0] unless connected?(other)
  vector = vector(other)
  vector.unit * -(ATTRACTION * (SPRING_LENGTH - vector.r.abs))
end

#connect(*nodes) ⇒ Object



63
64
65
66
67
68
# File 'lib/layout.rb', line 63

def connect(*nodes)
  @connections = @connections | nodes.to_set
  @connections.each do |node|
    node.connect(self) unless node.connected?(self)
  end
end

#connected?(other) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/layout.rb', line 70

def connected?(other)
  @connections.member?(other)
end

#repulsion(other) ⇒ Object



74
75
76
77
# File 'lib/layout.rb', line 74

def repulsion(other)
  vector = vector(other)
  vector.unit * REPULSION * (1 / (4 * Math::PI * vector.r**2))
end

#speedObject



89
90
91
# File 'lib/layout.rb', line 89

def speed
  velocity.r
end

#to_aObject



101
102
103
# File 'lib/layout.rb', line 101

def to_a
  [name, x, y]
end

#vector(other) ⇒ Object



85
86
87
# File 'lib/layout.rb', line 85

def vector(other)
  other.position - self.position
end

#xObject



93
94
95
# File 'lib/layout.rb', line 93

def x
  position[0]
end

#yObject



97
98
99
# File 'lib/layout.rb', line 97

def y
  position[1]
end