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.



2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
# 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

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.



3111
3112
3113
# File 'lib/rmath3d/rmath3d_plain.rb', line 3111

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.



3102
3103
3104
# File 'lib/rmath3d/rmath3d_plain.rb', line 3102

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.



3202
3203
3204
3205
3206
3207
3208
3209
3210
# 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

#+(arg) ⇒ Object

call-seq: +

vec1 + vec2 : Binary plus operator.



3176
3177
3178
3179
3180
3181
3182
# 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

#+@Object

call-seq: +

+vec : Unary plus operator.



3158
3159
3160
# File 'lib/rmath3d/rmath3d_plain.rb', line 3158

def +@
  return self
end

#-(arg) ⇒ Object

call-seq: -

vec1 - vec2 : Binary minus operator.



3189
3190
3191
3192
3193
3194
3195
# 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

#-@Object

call-seq: -

-vec : Unary minus operator.



3167
3168
3169
# File 'lib/rmath3d/rmath3d_plain.rb', line 3167

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

#==(other) ⇒ Object

call-seq: ==

vec1 == vec2 : evaluates equality.



3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
# 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

#[](i) ⇒ Object

call-seq: vec3 -> value

Returns the element at i.



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

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

#[]=(i, value) ⇒ Object

call-seq: vec2= value

Stores value at i.



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

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.



3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
# 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

#coerce(arg) ⇒ Object

call-seq: coerse(other)

Resolves type mismatch.



3013
3014
3015
3016
3017
3018
3019
3020
3021
# 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

#getLengthObject

call-seq: getLength

Returns the Euclidean length.



3084
3085
3086
# File 'lib/rmath3d/rmath3d_plain.rb', line 3084

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

#getLengthSqObject

call-seq: getLengthSq

Returns the squared Euclidean length.



3093
3094
3095
# File 'lib/rmath3d/rmath3d_plain.rb', line 3093

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

#getNormalizedObject

call-seq: getNormalized -> RVec2

Returns normalized vector.



3134
3135
3136
3137
3138
# 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!(arg) ⇒ Object

call-seq: vec1.mul!( vec2 )

vec1 *= vec2



3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
# 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!Object

call-seq: normalize! -> self

Normalizes itself.



3145
3146
3147
3148
3149
3150
3151
# 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(x, y) ⇒ Object

call-seq: setElements( e0, e1 )

Stores given 2 new values.



3028
3029
3030
3031
# File 'lib/rmath3d/rmath3d_plain.rb', line 3028

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.



3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
# 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_aObject

call-seq: to_a

Returns its elements as a new Array.



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

def to_a
  return @e
end

#to_sObject

call-seq: to_s

Returns human-readable string.



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

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).


3121
3122
3123
3124
3125
3126
3127
# 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

#xObject

call-seq: x -> value

Returns the value of x.



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

def x() return @e[0] end

#x=(value) ⇒ Object

call-seq: x= value

Stores value as x.



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

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

#yObject

call-seq: y -> value

Returns the value of y.



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

def y() return @e[1] end

#y=(value) ⇒ Object

call-seq: y= value

Stores value as y.



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

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