11-02-2010, 01:58 PM
(Modification du message : 11-02-2010, 03:12 PM par Sephi-Chan.)
En fait, les coordonnées des 6 éléments (au maximum) qui composent le voisinage d'une sphère change selon que cette sphère est sur un axe ou non.
Donc, je peux déjà écrire ce code :
J'ai donc encore 2 tâches à gérer : faire abstraction de l'index demandé (quand on demande un index plus élevé que l'index maximum de l'anneau) et déterminer les coordonnées des voisins pour les sphères situées hors des axes.
Sephi-Chan
Donc, je peux déjà écrire ce code :
class Sphere
attr_reader :index, :ring_index
def initialize(ring, index)
@ring = ring
@index = index
end
# Return true if the sphere is on the x or y axe.
def on_axe?
return true if @index == 0
return true if @ring == @index
return true if @ring == @index * 2
return true if @ring == @index * 3
false
end
end
class Graph
attr_reader :grid
def initialize(grid)
@graph = grid
end
def neighbours(ring_index, index)
sphere = sphere_at_coordinates(ring_index, index)
neighbours = if sphere.on_axe?
[
sphere_at_coordinates(sphere.ring_index - 1, sphere.index),
sphere_at_coordinates(sphere.ring_index, sphere.index - 1),
sphere_at_coordinates(sphere.ring_index, sphere.index + 1),
sphere_at_coordinates(sphere.ring_index + 1, sphere.index - 1),
sphere_at_coordinates(sphere.ring_index + 1, sphere.index),
sphere_at_coordinates(sphere.ring_index + 1, sphere.index + 1)
]
else
# Trouver les relations comme plus haut, mais pour les sphères situées hors des axes.
[
]
end
neighbours.delete_if { |sphere| sphere.nil? }
end
# Return the sphere at the given coordinates or nil.
# Handle "outs of bounds" positions.
def sphere_at_coordinates(ring_index, index)
max_index_for_ring = @grid[ring_index].size - 1
if max_index_for_ring < index
# Et maintenant ? Il y a une histoire de modulo pour faire abstraction de l'index demandé.
else
# ...
end
nil
end
end
J'ai donc encore 2 tâches à gérer : faire abstraction de l'index demandé (quand on demande un index plus élevé que l'index maximum de l'anneau) et déterminer les coordonnées des voisins pour les sphères situées hors des axes.
Sephi-Chan