Skip to content

Instantly share code, notes, and snippets.

@pencilcheck
Created June 18, 2011 21:10
Show Gist options
  • Save pencilcheck/1033508 to your computer and use it in GitHub Desktop.
Save pencilcheck/1033508 to your computer and use it in GitHub Desktop.
module Ogre
def assert
raise "Assertion failed !" unless yield if $DEBUG
end
end
require_relative 'helper'
module Ogre
class Vector2
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