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.



2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
# File 'lib/rmath3d/rmath3d_plain.rb', line 2820

def initialize( *a )
  @e = []
  case a.length
  when 0
    @e = [0.0, 0.0]
  when 1
    case a[0]
    when Fixnum, Float
      @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 Fixnum, Float
        @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.



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

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.



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

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.



3064
3065
3066
3067
3068
3069
3070
3071
3072
# File 'lib/rmath3d/rmath3d_plain.rb', line 3064

def *( arg )
  case arg
  when Fixnum, Float
    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.



3038
3039
3040
3041
3042
3043
3044
# File 'lib/rmath3d/rmath3d_plain.rb', line 3038

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.



3020
3021
3022
# File 'lib/rmath3d/rmath3d_plain.rb', line 3020

def +@
  return self
end

#-(arg) ⇒ Object

call-seq: -

vec1 - vec2 : Binary minus operator.



3051
3052
3053
3054
3055
3056
3057
# File 'lib/rmath3d/rmath3d_plain.rb', line 3051

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.



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

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

#==(other) ⇒ Object

call-seq: ==

vec1 == vec2 : evaluates equality.



3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
# File 'lib/rmath3d/rmath3d_plain.rb', line 3079

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.



2923
2924
2925
# File 'lib/rmath3d/rmath3d_plain.rb', line 2923

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

#[]=(i, value) ⇒ Object

call-seq: vec2= value

Stores value at i.



2900
2901
2902
# File 'lib/rmath3d/rmath3d_plain.rb', line 2900

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.



3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
# File 'lib/rmath3d/rmath3d_plain.rb', line 3097

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.



2875
2876
2877
2878
2879
2880
2881
2882
2883
# File 'lib/rmath3d/rmath3d_plain.rb', line 2875

def coerce( arg )
  case arg
  when Fixnum, Float, Bignum
    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.



2946
2947
2948
# File 'lib/rmath3d/rmath3d_plain.rb', line 2946

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

#getLengthSqObject

call-seq: getLengthSq

Returns the squared Euclidean length.



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

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

#getNormalizedObject

call-seq: getNormalized -> RVec2

Returns normalized vector.



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

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



3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
# File 'lib/rmath3d/rmath3d_plain.rb', line 3131

def mul!( arg )
  if arg.class != Fixnum && arg.class != Float
    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.



3007
3008
3009
3010
3011
3012
3013
# File 'lib/rmath3d/rmath3d_plain.rb', line 3007

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.



2890
2891
2892
2893
# File 'lib/rmath3d/rmath3d_plain.rb', line 2890

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.



3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
# File 'lib/rmath3d/rmath3d_plain.rb', line 3114

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.



2866
2867
2868
# File 'lib/rmath3d/rmath3d_plain.rb', line 2866

def to_a
  return @e
end

#to_sObject

call-seq: to_s

Returns human-readable string.



2857
2858
2859
# File 'lib/rmath3d/rmath3d_plain.rb', line 2857

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


2983
2984
2985
2986
2987
2988
2989
# File 'lib/rmath3d/rmath3d_plain.rb', line 2983

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.



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

def x() return @e[0] end

#x=(value) ⇒ Object

call-seq: x= value

Stores value as x.



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

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

#yObject

call-seq: y -> value

Returns the value of y.



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

def y() return @e[1] end

#y=(value) ⇒ Object

call-seq: y= value

Stores value as y.



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

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