Class: RMath3D::RVec2

Inherits:
Object
  • Object
show all
Defined in:
lib/rmath3d/rmath3d_plain.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*a) ⇒ RVec2

call-seq:

RVec2.new -> (0,0)
RVec2.new(e) -> (e,e)
RVec2.new( other ) : Copy Constructor
RVec2.new( e0, e1 ) -> (e0,e1)

Creates a new 3 element vector.



2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
# File 'lib/rmath3d/rmath3d_plain.rb', line 2861

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

Class Method Details

.cross(v1, v2) ⇒ Object

call-seq: RVec2.cross(v_a,v_b) -> value

Calculates the cross product of v_a and v_b.



3014
3015
3016
# File 'lib/rmath3d/rmath3d_plain.rb', line 3014

def RVec2.cross( v1, v2 )
  return v1.x*v2.y - v1.y*v2.x
end

.dot(v1, v2) ⇒ Object

call-seq: RVec2.dot(v_a,v_b) -> value

Calculates the dot product of v_a and v_b.



3005
3006
3007
# File 'lib/rmath3d/rmath3d_plain.rb', line 3005

def RVec2.dot( v1, v2 )
  return v1.x*v2.x + v1.y*v2.y
end

Instance Method Details

#*(arg) ⇒ Object

call-seq: *

vec1 * vec2 : Binary multiply operator.



3105
3106
3107
3108
3109
3110
3111
3112
3113
# File 'lib/rmath3d/rmath3d_plain.rb', line 3105

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

#+(arg) ⇒ Object

call-seq: +

vec1 + vec2 : Binary plus operator.



3079
3080
3081
3082
3083
3084
3085
# File 'lib/rmath3d/rmath3d_plain.rb', line 3079

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

#+@Object

call-seq: +

+vec : Unary plus operator.



3061
3062
3063
# File 'lib/rmath3d/rmath3d_plain.rb', line 3061

def +@
  return self
end

#-(arg) ⇒ Object

call-seq: -

vec1 - vec2 : Binary minus operator.



3092
3093
3094
3095
3096
3097
3098
# File 'lib/rmath3d/rmath3d_plain.rb', line 3092

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

#-@Object

call-seq: -

-vec : Unary minus operator.



3070
3071
3072
# File 'lib/rmath3d/rmath3d_plain.rb', line 3070

def -@
  return RVec2.new( -@e[0], -@e[1] )
end

#==(other) ⇒ Object

call-seq: ==

vec1 == vec2 : evaluates equality.



3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
# File 'lib/rmath3d/rmath3d_plain.rb', line 3120

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

#[](i) ⇒ Object

call-seq: vec3 -> value

Returns the element at i.



2964
2965
2966
# File 'lib/rmath3d/rmath3d_plain.rb', line 2964

def [](i)
  @e[i]
end

#[]=(i, value) ⇒ Object

call-seq: vec2= value

Stores value at i.



2941
2942
2943
# File 'lib/rmath3d/rmath3d_plain.rb', line 2941

def []=(i,value)
  @e[i] = value
end

#add!(other) ⇒ Object

call-seq: vec1.add!( vec2 )

vec1 += vec2 : appends the elements of vec2 into corresponding vec1 elements.



3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
# File 'lib/rmath3d/rmath3d_plain.rb', line 3138

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

#coerce(arg) ⇒ Object

call-seq: coerse(other)

Resolves type mismatch.



2916
2917
2918
2919
2920
2921
2922
2923
2924
# File 'lib/rmath3d/rmath3d_plain.rb', line 2916

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

#getLengthObject

call-seq: getLength

Returns the Euclidean length.



2987
2988
2989
# File 'lib/rmath3d/rmath3d_plain.rb', line 2987

def getLength
  return Math.sqrt( @e[0]*@e[0] + @e[1]*@e[1] )
end

#getLengthSqObject

call-seq: getLengthSq

Returns the squared Euclidean length.



2996
2997
2998
# File 'lib/rmath3d/rmath3d_plain.rb', line 2996

def getLengthSq
  return (@e[0]*@e[0] + @e[1]*@e[1]).to_f
end

#getNormalizedObject

call-seq: getNormalized -> RVec2

Returns normalized vector.



3037
3038
3039
3040
3041
# File 'lib/rmath3d/rmath3d_plain.rb', line 3037

def getNormalized
  l = getLength()
  l = 1.0/l
  return RVec2.new( @e[0]*l, @e[1]*l )
end

#mul!(arg) ⇒ Object

call-seq: vec1.mul!( vec2 )

vec1 *= vec2



3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
# File 'lib/rmath3d/rmath3d_plain.rb', line 3172

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!Object

call-seq: normalize! -> self

Normalizes itself.



3048
3049
3050
3051
3052
3053
3054
# File 'lib/rmath3d/rmath3d_plain.rb', line 3048

def normalize!
  l = getLength()
  l = 1.0/l
  @e[0] *= l
  @e[1] *= l
  return self
end

#setElements(x, y) ⇒ Object

call-seq: setElements( e0, e1 )

Stores given 2 new values.



2931
2932
2933
2934
# File 'lib/rmath3d/rmath3d_plain.rb', line 2931

def setElements( x, y )
  self.x = x
  self.y = y
end

#sub!(other) ⇒ Object

call-seq: vec1.sub!( vec2 )

vec1 -= vec2 : subtracts the elements of vec2 from corresponding vec1 elements.



3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
# File 'lib/rmath3d/rmath3d_plain.rb', line 3155

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_aObject

call-seq: to_a

Returns its elements as a new Array.



2907
2908
2909
# File 'lib/rmath3d/rmath3d_plain.rb', line 2907

def to_a
  return @e
end

#to_sObject

call-seq: to_s

Returns human-readable string.



2898
2899
2900
# File 'lib/rmath3d/rmath3d_plain.rb', line 2898

def to_s
  return "( #{@e[0]}, #{@e[1]} )"
end

#transform(mtx2) ⇒ Object

call-seq: transform(mtx2) -> transformed RVec2

Returns new RVec2 containing the result of the transformation of

RVec2(self.x,self.y) by +mtx2+ (RMtx2).


3024
3025
3026
3027
3028
3029
3030
# File 'lib/rmath3d/rmath3d_plain.rb', line 3024

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

#xObject

call-seq: x -> value

Returns the value of x.



2973
# File 'lib/rmath3d/rmath3d_plain.rb', line 2973

def x() return @e[0] end

#x=(value) ⇒ Object

call-seq: x= value

Stores value as x.



2950
# File 'lib/rmath3d/rmath3d_plain.rb', line 2950

def x=(value) @e[0] = value end

#yObject

call-seq: y -> value

Returns the value of y.



2980
# File 'lib/rmath3d/rmath3d_plain.rb', line 2980

def y() return @e[1] end

#y=(value) ⇒ Object

call-seq: y= value

Stores value as y.



2957
# File 'lib/rmath3d/rmath3d_plain.rb', line 2957

def y=(value) @e[1] = value end