Skip to content

Instantly share code, notes, and snippets.

@blfisher
Created February 26, 2013 06:41
Show Gist options
  • Save blfisher/c81e5ee96da53d3fb6f0 to your computer and use it in GitHub Desktop.
Save blfisher/c81e5ee96da53d3fb6f0 to your computer and use it in GitHub Desktop.
def calculate_spacial_index(index)
spacial_index = Array.new(@dimensions)
spacial_index[-1] = index % @shape[-1]
dimensions = @dimensions - 2
for dimension in (dimensions..0) do
offset = calculate_internal_offset(dimension)
previous_index = spacial_index[dimension + 1]
current_shape = @shape[dimension]
current_index = index - previous_index
current_index /= offset
current_index %= current_shape
spacial_index[dimension] = current_index
end
return spacial_index
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment