Skip to content

Instantly share code, notes, and snippets.

@ayucat
Created January 17, 2009 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayucat/48339 to your computer and use it in GitHub Desktop.
Save ayucat/48339 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# calc distances!
# example:
# %w[111 200 311 222 400].map { |s| printf "%s => %5.3f " s, o2d(s) }
def o2d(s)
return 0.0 unless s.is_a?(String) && s.size == 3
1 / (((0..2).to_a.inject(0) { |x, i| x += s[i,1].to_f**2 }) ** 0.5)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment