class MiniGL::Vector

This class represents a point or vector in a bidimensional space.

Attributes

x[RW]

The x coordinate of the vector

y[RW]

The y coordinate of the vector

Public Class Methods

new(x = 0, y = 0) click to toggle source

Creates a new bidimensional vector.

Parameters:

x

The x coordinate of the vector

y

The y coordinate of the vector

# File lib/minigl/global.rb, line 18
def initialize(x = 0, y = 0)
  @x = x
  @y = y
end

Public Instance Methods

!=(other_vector, precision = 6) click to toggle source

Returns true if at least one coordinate of this vector is different from the corresponding coordinate of other_vector, with precision decimal places of precision.

# File lib/minigl/global.rb, line 34
def !=(other_vector, precision = 6)
  @x.round(precision) != other_vector.x.round(precision) or
      @y.round(precision) != other_vector.y.round(precision)
end
*(scalar) click to toggle source

Multiplies this vector by a scalar, i.e., each coordinate is multiplied by the given number.

# File lib/minigl/global.rb, line 53
def *(scalar)
  Vector.new @x * scalar, @y * scalar
end
+(other_vector) click to toggle source

Sums this vector with other_vector, i.e., sums each coordinate of this vector with the corresponding coordinate of other_vector.

# File lib/minigl/global.rb, line 41
def +(other_vector)
  Vector.new @x + other_vector.x, @y + other_vector.y
end
-(other_vector) click to toggle source

Subtracts other_vector from this vector, i.e., subtracts from each coordinate of this vector the corresponding coordinate of other_vector.

# File lib/minigl/global.rb, line 47
def -(other_vector)
  Vector.new @x - other_vector.x, @y - other_vector.y
end
/(scalar) click to toggle source

Divides this vector by a scalar, i.e., each coordinate is divided by the given number.

# File lib/minigl/global.rb, line 59
def /(scalar)
  Vector.new @x / scalar.to_f, @y / scalar.to_f
end
==(other_vector, precision = 6) click to toggle source

Returns true if both coordinates of this vector are equal to the corresponding coordinates of other_vector, with precision decimal places of precision.

# File lib/minigl/global.rb, line 26
def ==(other_vector, precision = 6)
  @x.round(precision) == other_vector.x.round(precision) and
      @y.round(precision) == other_vector.y.round(precision)
end
distance(other_vector) click to toggle source

Returns the euclidean distance between this vector and other_vector.

# File lib/minigl/global.rb, line 64
def distance(other_vector)
  dx = @x - other_vector.x
  dy = @y - other_vector.y
  Math.sqrt(dx ** 2 + dy ** 2)
end
rotate(radians) click to toggle source

Returns a vector corresponding to the rotation of this vector around the origin (0, 0) by radians radians.

# File lib/minigl/global.rb, line 72
def rotate(radians)
  sin = Math.sin radians
  cos = Math.cos radians
  Vector.new cos * @x - sin * @y, sin * @x + cos * @y
end
rotate!(radians) click to toggle source

Rotates this vector by radians radians around the origin (0, 0).

# File lib/minigl/global.rb, line 79
def rotate!(radians)
  sin = Math.sin radians
  cos = Math.cos radians
  prev_x = @x
  @x = cos * @x - sin * @y
  @y = sin * prev_x + cos * @y
end