Created
June 18, 2011 21:13
-
-
Save pencilcheck/1033511 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module Ogre | |
def assert | |
raise "Assertion failed !" unless yield if $DEBUG | |
end | |
end |
This file contains hidden or 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
require_relative 'helper' | |
module Ogre | |
class Vector2 | |
include Ogre | |
attr_accessor :x, :y | |
def initialize(x, y) | |
@x = x.to_f | |
@y = y.to_f | |
end | |
def Vector2.scalar(scale) | |
new(scale, scale) | |
end | |
def Vector2.coordinates(list) | |
new(list[0], list[1]) | |
end | |
def swap(other) | |
raise "The parameter is not an instance of Vector2." unless other.respond_to? 'swap' and other.instance_of? Vector2 | |
x, y = @x, @y | |
@x, @y = other.x, other.y | |
other.x, other.y = x, y | |
end | |
def [](i) | |
assert {i.to_i < 2} | |
case i | |
when 0 | |
x | |
when 1 | |
y | |
end | |
end | |
def to_s | |
"%.2f, %.2f" % [@x, @y] | |
end | |
end | |
end | |
v2 = Ogre::Vector2.new(0.0, 1.0) | |
v3 = Ogre::Vector2.scalar(3) | |
puts 'before' | |
p v2 | |
p v3 | |
v2.swap v3 | |
puts 'after' | |
p v2 | |
p v3 | |
p v2[0] | |
p v3[1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment