class Rubyvis::Vector

Attributes

x[RW]
y[RW]

Public Class Methods

new(x,y) click to toggle source

Constructs a {@link pv.Vector} for the specified x and y coordinate. This constructor should not be invoked directly; use {@link pv.vector} instead.

@class Represents a two-dimensional vector; a 2-tuple &x27e8;x, y&x27e9;. 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.

@param {number} x the x coordinate. @param {number} y the y coordinate.

# File lib/rubyvis/vector.rb, line 28
def initialize(x,y) 
  @x=x
  @y=y
end

Public Instance Methods

==(v) click to toggle source
# File lib/rubyvis/vector.rb, line 32
def ==(v)
  @x==v.x and @y==v.y
end
dot(x, y=nil) click to toggle source

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.

@param {number} x the x coordinate to dot. @param {number} y the y coordinate to dot. @returns {number} a dot product.

# 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() click to toggle source

Returns the magnitude of this vector, defined as sqrt(x * x + y * y).

@returns {number} a length.

# File lib/rubyvis/vector.rb, line 55
def length
  Math.sqrt(@x**2 + @y**2)
end
minus(x,y=nil) click to toggle source

Returns this vector minus the vector v: &x27e8;x - v.x, y - v.y&x27e9;. If only one argument is specified, it is interpreted as the vector v.

@param {number} x the x coordinate to subtract. @param {number} y the y coordinate to subtract. @returns {pv.Vector} a new vector.

# 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() click to toggle source

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.

@returns {pv.Vector} a unit vector.

# File lib/rubyvis/vector.rb, line 47
def norm 
  l=length()
  times(l!=0 ? (1.0 / l) : 1.0)
end
perp() click to toggle source
Returns a vector perpendicular to this vector: <i>&#x27e8;-y, x&#x27e9;</i>.

@returns {pv.Vector} a perpendicular vector.

/

# File lib/rubyvis/vector.rb, line 39
def perp
  Rubyvis::Vector.new(-@y,@x)
end
plus(x,y=nil) click to toggle source

Returns this vector plus the vector v: &x27e8;x + v.x, y + v.y&x27e9;. If only one argument is specified, it is interpreted as the vector v.

@param {number} x the x coordinate to add. @param {number} y the y coordinate to add. @returns {pv.Vector} a new vector.

# 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
times(k) click to toggle source

Returns a scaled copy of this vector: &x27e8;x * k, y * k&x27e9;. To perform the equivalent divide operation, use 1 / k.

@param {number} k the scale factor. @returns {pv.Vector} a scaled vector.

# File lib/rubyvis/vector.rb, line 64
def times(k)
  Rubyvis::Vector.new(@x * k, @y * k)
end