class RMath3D::RVec2
Document-class: RMath3D::RVec2
provies 2 element vector arithmetic.
Public Class Methods
Calculates the cross product of v_a
and v_b
.
# File lib/rmath3d/rmath3d_plain.rb, line 3111 def RVec2.cross( v1, v2 ) return v1.x*v2.y - v1.y*v2.x end
Calculates the dot product of v_a
and v_b
.
# File lib/rmath3d/rmath3d_plain.rb, line 3102 def RVec2.dot( v1, v2 ) return v1.x*v2.x + v1.y*v2.y end
Creates a new 3 element vector.
# File lib/rmath3d/rmath3d_plain.rb, line 2958 def initialize( *a ) @e = [] case a.length when 0 @e = [0.0, 0.0] when 1 case a[0] when Float, Integer @e = [ a[0], a[0] ] when RVec2 @e = [ a[0].x, a[0].y ] else raise TypeError, "RVec2#initialize : Unknown type #{a[0].class}." return nil end when 2 a.each_with_index do |elem, index| case elem when Float, Integer @e[index] = elem else raise TypeError, "RVec2#initialize : Unknown type #{elem.class}." return nil end end else raise RuntimeError, "RVec2#initialize : wrong # of arguments (#{a.length})" return nil end return self end
Public Instance Methods
vec1 * vec2 : Binary multiply operator.
# File lib/rmath3d/rmath3d_plain.rb, line 3202 def *( arg ) case arg when Float, Integer return RVec2.new( @e[0]*arg, @e[1]*arg ) else raise TypeError, "RVec2#* : Unknown type #{arg}." return nil end end
vec1 + vec2 : Binary plus operator.
# File lib/rmath3d/rmath3d_plain.rb, line 3176 def +( arg ) if arg.class != RVec2 raise TypeError, "RVec2#+ : Unknown type #{arg.class}." return nil end RVec2.new( x+arg.x, y+arg.y ) end
+vec : Unary plus operator.
# File lib/rmath3d/rmath3d_plain.rb, line 3158 def +@ return self end
vec1 - vec2 : Binary minus operator.
# File lib/rmath3d/rmath3d_plain.rb, line 3189 def -( arg ) if arg.class != RVec2 raise TypeError, "RVec2#- : Unknown type #{arg.class}." return nil end RVec2.new( x-arg.x, y-arg.y ) end
-vec : Unary minus operator.
# File lib/rmath3d/rmath3d_plain.rb, line 3167 def -@ return RVec2.new( -@e[0], -@e[1] ) end
vec1 == vec2 : evaluates equality.
# File lib/rmath3d/rmath3d_plain.rb, line 3217 def ==( other ) if other.class == RVec2 if (x-other.x).abs<=Float::EPSILON && (y-other.y).abs<=Float::EPSILON return true else return false end else return false end end
Returns the element at i
.
# File lib/rmath3d/rmath3d_plain.rb, line 3061 def [](i) @e[i] end
Stores value
at i
.
# File lib/rmath3d/rmath3d_plain.rb, line 3038 def []=(i,value) @e[i] = value end
vec1 += vec2 : appends the elements of vec2
into corresponding vec1
elements.
# File lib/rmath3d/rmath3d_plain.rb, line 3235 def add!( other ) if other.class != RVec2 raise TypeError, "RVec2#add! : Unknown type #{other.class}." return nil end self.x += other.x self.y += other.y return self end
Resolves type mismatch.
# File lib/rmath3d/rmath3d_plain.rb, line 3013 def coerce( arg ) case arg when Float, Integer return [ self, arg ] else raise TypeError, "RVec2#coerce : #{arg.self} can't be coerced into #{self.class}." return nil end end
Returns the Euclidean length.
# File lib/rmath3d/rmath3d_plain.rb, line 3084 def getLength return Math.sqrt( @e[0]*@e[0] + @e[1]*@e[1] ) end
Returns the squared Euclidean length.
# File lib/rmath3d/rmath3d_plain.rb, line 3093 def getLengthSq return (@e[0]*@e[0] + @e[1]*@e[1]).to_f end
Returns normalized vector.
# File lib/rmath3d/rmath3d_plain.rb, line 3134 def getNormalized l = getLength() l = 1.0/l return RVec2.new( @e[0]*l, @e[1]*l ) end
vec1 *= vec2
# File lib/rmath3d/rmath3d_plain.rb, line 3269 def mul!( arg ) if !(arg.class == Float || arg.class == Integer) raise TypeError, "RVec2#mul! : Unknown type #{arg.class}." return nil end self.x *= arg self.y *= arg return self end
Normalizes itself.
# File lib/rmath3d/rmath3d_plain.rb, line 3145 def normalize! l = getLength() l = 1.0/l @e[0] *= l @e[1] *= l return self end
Stores given 2 new values.
# File lib/rmath3d/rmath3d_plain.rb, line 3028 def setElements( x, y ) self.x = x self.y = y end
vec1 -= vec2 : subtracts the elements of vec2
from corresponding vec1
elements.
# File lib/rmath3d/rmath3d_plain.rb, line 3252 def sub!( other ) if other.class != RVec2 raise TypeError, "RVec2#sub! : Unknown type #{other.class}." return nil end self.x -= other.x self.y -= other.y return self end
Returns its elements as a new Array.
# File lib/rmath3d/rmath3d_plain.rb, line 3004 def to_a return @e end
Returns human-readable string.
# File lib/rmath3d/rmath3d_plain.rb, line 2995 def to_s return "( #{@e[0]}, #{@e[1]} )" end
Returns new RVec2
containing the result of the transformation of
RVec2(self.x,self.y) by +mtx2+ (RMtx2).
# File lib/rmath3d/rmath3d_plain.rb, line 3121 def transform( mtx2 ) result = RVec2.new result.x = mtx2.e00 * self[0] + mtx2.e01 * self[1] result.y = mtx2.e10 * self[0] + mtx2.e11 * self[1] return result end
Returns the value of x
.
# File lib/rmath3d/rmath3d_plain.rb, line 3070 def x() return @e[0] end
Stores value
as x
.
# File lib/rmath3d/rmath3d_plain.rb, line 3047 def x=(value) @e[0] = value end
Returns the value of y
.
# File lib/rmath3d/rmath3d_plain.rb, line 3077 def y() return @e[1] end
Stores value
as y
.
# File lib/rmath3d/rmath3d_plain.rb, line 3054 def y=(value) @e[1] = value end