Created
March 7, 2012 04:06
-
-
Save dbrady/1990881 to your computer and use it in GitHub Desktop.
Ruby 3D Vector w/UTF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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