class RMath3D::RVec2

Document-class: RMath3D::RVec2 provies 2 element vector arithmetic.

Public Class Methods

cross(v_a,v_b) → value click to toggle source

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
dot(v_a,v_b) → value click to toggle source

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
new → (0,0) click to toggle source
new(e) → (e,e)
new( other ) : Copy Constructor
new( e0, e1 ) → (e0,e1)

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

* click to toggle source

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
+ click to toggle source

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
+ click to toggle source

+vec : Unary plus operator.

# File lib/rmath3d/rmath3d_plain.rb, line 3158
def +@
  return self
end
- click to toggle source

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
- click to toggle source

-vec : Unary minus operator.

# File lib/rmath3d/rmath3d_plain.rb, line 3167
def -@
  return RVec2.new( -@e[0], -@e[1] )
end
== click to toggle source

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
vec3[i] → value click to toggle source

Returns the element at i.

# File lib/rmath3d/rmath3d_plain.rb, line 3061
def [](i)
  @e[i]
end
vec2[i]= value click to toggle source

Stores value at i.

# File lib/rmath3d/rmath3d_plain.rb, line 3038
def []=(i,value)
  @e[i] = value
end
add!( vec2 ) click to toggle source

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
coerse(other) click to toggle source

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
getLength click to toggle source

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
getLengthSq click to toggle source

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
getNormalized → RVec2 click to toggle source

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
mul!( vec2 ) click to toggle source

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
normalize! → self click to toggle source

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
setElements( e0, e1 ) click to toggle source

Stores given 2 new values.

# File lib/rmath3d/rmath3d_plain.rb, line 3028
def setElements( x, y )
  self.x = x
  self.y = y
end
sub!( vec2 ) click to toggle source

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
to_a click to toggle source

Returns its elements as a new Array.

# File lib/rmath3d/rmath3d_plain.rb, line 3004
def to_a
  return @e
end
to_s click to toggle source

Returns human-readable string.

# File lib/rmath3d/rmath3d_plain.rb, line 2995
def to_s
  return "( #{@e[0]}, #{@e[1]} )"
end
transform(mtx2) → transformed RVec2 click to toggle source

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
x → value click to toggle source

Returns the value of x.

# File lib/rmath3d/rmath3d_plain.rb, line 3070
def x() return @e[0] end
x= value click to toggle source

Stores value as x.

# File lib/rmath3d/rmath3d_plain.rb, line 3047
def x=(value) @e[0] = value end
y → value click to toggle source

Returns the value of y.

# File lib/rmath3d/rmath3d_plain.rb, line 3077
def y() return @e[1] end
y= value click to toggle source

Stores value as y.

# File lib/rmath3d/rmath3d_plain.rb, line 3054
def y=(value) @e[1] = value end