Class: RMath3D::RVec2
- Inherits:
-
Object
- Object
- RMath3D::RVec2
- Defined in:
- lib/rmath3d/rmath3d_plain.rb
Overview
Document-class: RMath3D::RVec2 provies 2 element vector arithmetic.
Class Method Summary collapse
-
.cross(v1, v2) ⇒ Object
call-seq: RVec2.cross(v_a,v_b) -> value.
-
.dot(v1, v2) ⇒ Object
call-seq: RVec2.dot(v_a,v_b) -> value.
Instance Method Summary collapse
-
#*(arg) ⇒ Object
call-seq: *.
-
#+(arg) ⇒ Object
call-seq: +.
-
#+@ ⇒ Object
call-seq: +.
-
#-(arg) ⇒ Object
call-seq: -.
-
#-@ ⇒ Object
call-seq: -.
-
#==(other) ⇒ Object
call-seq: ==.
-
#[](i) ⇒ Object
call-seq: vec3 -> value.
-
#[]=(i, value) ⇒ Object
call-seq: vec2= value.
-
#add!(other) ⇒ Object
call-seq: vec1.add!( vec2 ).
-
#coerce(arg) ⇒ Object
call-seq: coerse(other).
-
#getLength ⇒ Object
call-seq: getLength.
-
#getLengthSq ⇒ Object
call-seq: getLengthSq.
-
#getNormalized ⇒ Object
call-seq: getNormalized -> RVec2.
-
#initialize(*a) ⇒ RVec2
constructor
call-seq: RVec2.new -> (0,0) RVec2.new(e) -> (e,e) RVec2.new( other ) : Copy Constructor RVec2.new( e0, e1 ) -> (e0,e1).
-
#mul!(arg) ⇒ Object
call-seq: vec1.mul!( vec2 ).
-
#normalize! ⇒ Object
call-seq: normalize! -> self.
-
#setElements(x, y) ⇒ Object
call-seq: setElements( e0, e1 ).
-
#sub!(other) ⇒ Object
call-seq: vec1.sub!( vec2 ).
-
#to_a ⇒ Object
call-seq: to_a.
-
#to_s ⇒ Object
call-seq: to_s.
-
#transform(mtx2) ⇒ Object
call-seq: transform(mtx2) -> transformed RVec2.
-
#x ⇒ Object
call-seq: x -> value.
-
#x=(value) ⇒ Object
call-seq: x= value.
-
#y ⇒ Object
call-seq: y -> value.
-
#y=(value) ⇒ Object
call-seq: y= value.
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.
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 2851 2852 2853 2854 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2824 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.
2977 2978 2979 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2977 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.
2968 2969 2970 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2968 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.
3068 3069 3070 3071 3072 3073 3074 3075 3076 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3068 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.
3042 3043 3044 3045 3046 3047 3048 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3042 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.
3024 3025 3026 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3024 def +@ return self end |
#-(arg) ⇒ Object
call-seq: -
vec1 - vec2 : Binary minus operator.
3055 3056 3057 3058 3059 3060 3061 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3055 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.
3033 3034 3035 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3033 def -@ return RVec2.new( -@e[0], -@e[1] ) end |
#==(other) ⇒ Object
call-seq: ==
vec1 == vec2 : evaluates equality.
3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3083 def ==( other ) if other.class != RVec2 raise TypeError, "RVec2#== : Unknown type #{other.class}." return nil end if (x-other.x).abs<=Float::EPSILON && (y-other.y).abs<=Float::EPSILON return true else return false end end |
#[](i) ⇒ Object
call-seq: vec3 -> value
Returns the element at i.
2927 2928 2929 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2927 def [](i) @e[i] end |
#[]=(i, value) ⇒ Object
call-seq: vec2= value
Stores value at i.
2904 2905 2906 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2904 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.
3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3102 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.
2879 2880 2881 2882 2883 2884 2885 2886 2887 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2879 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 |
#getLength ⇒ Object
call-seq: getLength
Returns the Euclidean length.
2950 2951 2952 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2950 def getLength return Math.sqrt( @e[0]*@e[0] + @e[1]*@e[1] ) end |
#getLengthSq ⇒ Object
call-seq: getLengthSq
Returns the squared Euclidean length.
2959 2960 2961 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2959 def getLengthSq return (@e[0]*@e[0] + @e[1]*@e[1]).to_f end |
#getNormalized ⇒ Object
call-seq: getNormalized -> RVec2
Returns normalized vector.
3000 3001 3002 3003 3004 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3000 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
3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3136 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.
3011 3012 3013 3014 3015 3016 3017 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3011 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.
2894 2895 2896 2897 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2894 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.
3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3119 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 ⇒ Object
call-seq: to_a
Returns its elements as a new Array.
2870 2871 2872 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2870 def to_a return @e end |
#to_s ⇒ Object
call-seq: to_s
Returns human-readable string.
2861 2862 2863 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2861 def to_s return "( #{@e[0]}, #{@e[1]} )\n" 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).
2987 2988 2989 2990 2991 2992 2993 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2987 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 ⇒ Object
call-seq: x -> value
Returns the value of x.
2936 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2936 def x() return @e[0] end |
#x=(value) ⇒ Object
call-seq: x= value
Stores value as x.
2913 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2913 def x=(value) @e[0] = value end |
#y ⇒ Object
call-seq: y -> value
Returns the value of y.
2943 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2943 def y() return @e[1] end |
#y=(value) ⇒ Object
call-seq: y= value
Stores value as y.
2920 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2920 def y=(value) @e[1] = value end |