Class: Node
Instance Attribute Summary collapse
-
#best_distance ⇒ Object
Returns the value of attribute best_distance.
-
#closest_centroid ⇒ Object
Returns the value of attribute closest_centroid.
-
#position ⇒ Object
Returns the value of attribute position.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(position) ⇒ Node
constructor
A new instance of Node.
- #update_closest_centroid(centroids) ⇒ Object
Constructor Details
#initialize(position) ⇒ Node
Returns a new instance of Node.
15 16 17 |
# File 'lib/node.rb', line 15 def initialize(position) @position = position end |
Instance Attribute Details
#best_distance ⇒ Object
Returns the value of attribute best_distance.
13 14 15 |
# File 'lib/node.rb', line 13 def best_distance @best_distance end |
#closest_centroid ⇒ Object
Returns the value of attribute closest_centroid.
13 14 15 |
# File 'lib/node.rb', line 13 def closest_centroid @closest_centroid end |
#position ⇒ Object
Returns the value of attribute position.
13 14 15 |
# File 'lib/node.rb', line 13 def position @position end |
Class Method Details
.create_nodes(data) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/node.rb', line 4 def create_nodes(data) nodes = [] data.each do |position| nodes << new(position) end nodes end |
Instance Method Details
#update_closest_centroid(centroids) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/node.rb', line 19 def update_closest_centroid(centroids) calculate_initial_centroid(centroids.first) unless @closest_centroid updated = false centroids.each do |centroid| distance = calculate_distance(centroid) if distance < best_distance updated = true @closest_centroid = centroid @best_distance = distance end end updated == true ? 1 : 0 end |