class V

Simple and fast 2 dimensional vector

Constants

ONE

one vector

ZERO

zero vector

Attributes

x[RW]

x coordinate accessors – preferably float

y[RW]

y coordinate accessors – preferably float

Public Class Methods

new(x, y) click to toggle source

Create a new vector with x & y coordinates.

# File lib/graphics/v.rb, line 17
def initialize x, y
  @x = x
  @y = y
end

Public Instance Methods

*(s) click to toggle source

Multiply a vector by a scalar, returning a new vector.

# File lib/graphics/v.rb, line 52
def * s
  V[x*s, y*s]
end
+(v) click to toggle source

Add two vectors, returning a new vector.

# File lib/graphics/v.rb, line 31
def + v
  V[x+v.x, y+v.y]
end
-(v) click to toggle source

Subtract two vectors, returning a new vector.

# File lib/graphics/v.rb, line 38
def - v
  V[x-v.x, y-v.y]
end
-@() click to toggle source

Unary negation.

# File lib/graphics/v.rb, line 45
def -@
  V[-x, -y]
end
/(s) click to toggle source

Divide a vector by a scalar, returning a new vector.

# File lib/graphics/v.rb, line 59
def / s
  V[x/s, y/s]
end
magnitude() click to toggle source

Return the length of the vector from the origin.

# File lib/graphics/v.rb, line 70
def magnitude
  @magnitude ||= Math.sqrt(x*x + y*y)
end