Class: Rubyvis::Vector
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #==(v) ⇒ Object
-
#dot(x, y = nil) ⇒ Object
Returns the dot product of this vector and the vector v: x * v.x + y * v.y.
-
#initialize(x, y) ⇒ Vector
constructor
Constructs a pv.Vector for the specified x and y coordinate.
-
#length ⇒ Object
Returns the magnitude of this vector, defined as sqrt(x * x + y * y).
-
#minus(x, y = nil) ⇒ Object
Returns this vector minus the vector v: ⟨x - v.x, y - v.y⟩.
-
#norm ⇒ Object
Returns a normalized copy of this vector: a vector with the same direction, but unit length.
-
#perp ⇒ Object
Returns a vector perpendicular to this vector: ⟨-y, x⟩.
-
#plus(x, y = nil) ⇒ Object
Returns this vector plus the vector v: ⟨x + v.x, y + v.y⟩.
-
#times(k) ⇒ Object
Returns a scaled copy of this vector: ⟨x * k, y * k⟩.
Constructor Details
#initialize(x, y) ⇒ Vector
Constructs a pv.Vector for the specified x and y coordinate. This constructor should not be invoked directly; use pv.vector instead.
y⟩</i>. The intent of this class is to simplify vector math. Note that in performance-sensitive cases it may be more efficient to represent 2D vectors as simple objects with x
and y
attributes, rather than using instances of this class.
28 29 30 31 |
# File 'lib/rubyvis/vector.rb', line 28 def initialize(x,y) @x=x @y=y end |
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x.
15 16 17 |
# File 'lib/rubyvis/vector.rb', line 15 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
15 16 17 |
# File 'lib/rubyvis/vector.rb', line 15 def y @y end |
Instance Method Details
#==(v) ⇒ Object
32 33 34 |
# File 'lib/rubyvis/vector.rb', line 32 def ==(v) @x==v.x and @y==v.y end |
#dot(x, y = nil) ⇒ Object
Returns the dot product of this vector and the vector v: x * v.x + y * v.y. If only one argument is specified, it is interpreted as the vector v.
99 100 101 |
# File 'lib/rubyvis/vector.rb', line 99 def dot(x, y=nil) (y.nil?) ? @x * x.x + @y * x.y : @x * x + @y * y end |
#length ⇒ Object
Returns the magnitude of this vector, defined as sqrt(x * x + y * y).
55 56 57 |
# File 'lib/rubyvis/vector.rb', line 55 def length Math.sqrt(@x**2 + @y**2) end |
#minus(x, y = nil) ⇒ Object
Returns this vector minus the vector v: ⟨x - v.x, y - v.y⟩. If only one argument is specified, it is interpreted as the vector v.
87 88 89 90 |
# File 'lib/rubyvis/vector.rb', line 87 def minus(x,y=nil) return (y.nil?) ? Rubyvis::Vector.new(@x - x.x, @y - x.y) : Rubyvis::Vector.new(@x - x, @y - y) end |
#norm ⇒ Object
Returns a normalized copy of this vector: a vector with the same direction, but unit length. If this vector has zero length this method returns a copy of this vector.
47 48 49 50 |
# File 'lib/rubyvis/vector.rb', line 47 def norm l=length() times(l!=0 ? (1.0 / l) : 1.0) end |
#perp ⇒ Object
Returns a vector perpendicular to this vector: ⟨-y, x⟩.
/
39 40 41 |
# File 'lib/rubyvis/vector.rb', line 39 def perp Rubyvis::Vector.new(-@y,@x) end |
#plus(x, y = nil) ⇒ Object
Returns this vector plus the vector v: ⟨x + v.x, y + v.y⟩. If only one argument is specified, it is interpreted as the vector v.
74 75 76 77 |
# File 'lib/rubyvis/vector.rb', line 74 def plus(x,y=nil) return (y.nil?) ? Rubyvis::Vector.new(@x + x.x, @y + x.y) : Rubyvis::Vector.new(@x + x, @y + y) end |