SFML/bindings/ruby/testing/vector2.rb
LaurentGom 0e2297af28 Moved all bindings to the "bindings" sub-directory
Renamed the CSFML directory to c
Renamed the DSFML directory to d
--> bindings must now be updated to match the new organization!

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1630 4e206d99-4929-0410-ac5d-dfc041789085
2010-11-09 17:13:17 +00:00

78 lines
1.5 KiB
Ruby

class Vector2
attr_accessor :x, :y
def initialize( *args )
if args.size == 0
@x = 0
@y = 0
elsif args.size == 1
copyFrom( args[0] )
elsif args.size == 2
Vector2.valid? args[0], args[1]
@x = args[0]
@y = args[1]
else
raise ArgumentError.new( "invalid argument list" )
end
@dataType = x.class
end
def copyFrom( source )
unless source.is_a?( Array ) || source.is_a?( Vector2 )
raise ArgumentError.new( "expected Array or Vector2" )
end
Vector2.valid? source[0], source[1]
@x = source[0]
@y = source[1]
end
def -@
Vector2.new( -x, -y )
end
def +( right )
Vector2.new( x + right.x, y + right.y )
end
def -( right )
Vector2.new( x - right.x, y - right.y )
end
def *( right )
Vector2.new( x * right.x, y * right.y )
end
def /( right )
Vector2.new( x / right.x, y / right.y )
end
def ==( right )
x == right.x && y == right.y
end
def []( index )
if index == 0 || index == :x
return x
elsif index == 1 || index == :y
return y
end
raise ArgumentError.new( "Expected index to be either 0..1 or :x and :y" )
end
def self.valid?( x, y )
if x.class != y.class
raise RuntimeError.new( "x and y must be of same type" )
end
if x.is_a?( Numeric ) == false
raise RuntimeError.new( "x and y must be numeric!" )
end
true
end
end