Class: Rubyvis::Vector

Inherits:
Object show all
Defined in:
lib/rubyvis/vector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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&#x27e9;</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.

Parameters:

  • x (number)

    the x coordinate.

  • y (number)

    the y coordinate.



28
29
30
31
# File 'lib/rubyvis/vector.rb', line 28

def initialize(x,y) 
  @x=x
  @y=y
end

Instance Attribute Details

#xObject

Returns the value of attribute x.



15
16
17
# File 'lib/rubyvis/vector.rb', line 15

def x
  @x
end

#yObject

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.

Parameters:

  • x (number)

    the x coordinate to dot.

  • y (number) (defaults to: nil)

    the y coordinate to dot.



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

#lengthObject

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: &#x27e8;x - v.x, y - v.y&#x27e9;. If only one argument is specified, it is interpreted as the vector v.

Parameters:

  • x (number)

    the x coordinate to subtract.

  • y (number) (defaults to: nil)

    the y coordinate to subtract.



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

#normObject

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

#perpObject

Returns a vector perpendicular to this vector: &#x27e8;-y, x&#x27e9;.

/



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: &#x27e8;x + v.x, y + v.y&#x27e9;. If only one argument is specified, it is interpreted as the vector v.

Parameters:

  • x (number)

    the x coordinate to add.

  • y (number) (defaults to: nil)

    the y coordinate to add.



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

#times(k) ⇒ Object

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

Parameters:

  • k (number)

    the scale factor.



64
65
66
# File 'lib/rubyvis/vector.rb', line 64

def times(k)
  Rubyvis::Vector.new(@x * k, @y * k)
end