Skip to content

Instantly share code, notes, and snippets.

@dbrady
Created March 7, 2012 04: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 dbrady/1990881 to your computer and use it in GitHub Desktop.
Save dbrady/1990881 to your computer and use it in GitHub Desktop.
Ruby 3D Vector w/UTF
#!/usr/bin/env ruby
# encoding: utf-8
class Vector
attr_accessor :x, :y, :z
def initialize(x,y,z)
@x,@y,@z = x,y,z
end
def to_s
"<#{x}, #{y}, #{z}>"
end
def ×(v)
Vector.new(
y*v.z - z*v.y,
z*v.x - x*v.z,
x*v.y - y*v.x
)
end
def •(v)
x*v.x + y*v.y + z*v.z
end
end
a = Vector.new(1,0,0)
b = Vector.new(0,1,0)
puts a.× b
puts a.• b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment