Created
June 19, 2011 06:01
-
-
Save pencilcheck/1033808 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
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 scalar=(scalar) | |
@x, @y = scalar, scalar | |
end | |
def add(vector_or_scalar) | |
if vector_or_scalar.instance_of? Vector2 | |
@x += vector_or_scalar.x | |
@y += vector_or_scalar.y | |
else | |
@x += vector_or_scalar | |
@y += vector_or_scalar | |
end | |
end | |
def subtract(vector_or_scalar) | |
if vector_or_scalar.instance_of? Vector2 | |
@x -= vector_or_scalar.x | |
@y -= vector_or_scalar.y | |
else | |
@x -= vector_or_scalar | |
@y -= vector_or_scalar | |
end | |
end | |
def multiply(vector_or_scalar) | |
if vector_or_scalar.instance_of? Vector2 | |
@x *= vector_or_scalar.x | |
@y *= vector_or_scalar.y | |
else | |
@x *= vector_or_scalar | |
@y *= vector_or_scalar | |
end | |
end | |
def divide(vector_or_scalar) | |
if vector_or_scalar.instance_of? Vector2 | |
@x /= vector_or_scalar.x | |
@y /= vector_or_scalar.y | |
else | |
@x /= vector_or_scalar | |
@y /= vector_or_scalar | |
end | |
end | |
def -@ | |
@x = -@x | |
@y = -@y | |
end | |
def eql?(vector) | |
[vector.x, vector.y] == [@x, @y] | |
end | |
def [](i) | |
assert {0 <= i.to_i and i.to_i < 2} | |
case i | |
when 0 | |
x | |
when 1 | |
y | |
end | |
end | |
def to_s | |
"%f, %f" % [@x, @y] | |
end | |
end | |
end | |
v2 = Ogre::Vector2.new(0.0123, 1.0321) | |
v3 = Ogre::Vector2.scalar(3.123123) | |
v3.scalar = 1.111111 | |
p v3 | |
v4 = Ogre::Vector2.scalar(1.111111) | |
p v4.eql? v3 | |
v4.add v3 | |
p v4 | |
v3.subtract v4 | |
p -v3 |
Sailias
commented
Jun 19, 2011
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment