Sha256: 8a9cf8d508a64846ca5450eb93933add22e4566e29e1c0d223a1d948eaeefce5

Contents?: true

Size: 791 Bytes

Versions: 1

Compression:

Stored size: 791 Bytes

Contents

class GeometricVector < Struct.new(:x, :y)

  def self.new_by_array(array)
    self.new(array[0], array[1])
  end

  def ==(vector)
    x === vector.x && y === vector.y
  end

  def +(vector)
    GeometricVector.new(x + vector.x, y + vector.y)
  end

  def -(vector)
    self + (-1) * vector
  end

  def *(scalar)
    GeometricVector.new(x * scalar, y * scalar)
  end

  def coerce(scalar)
    if scalar.is_a?(Numeric)
      [self, scalar]
    else
      raise ArgumentError, "GeometricVector: cannot coerce #{scalar.inspect}"
    end
  end

  def cross_product(vector)
    x * vector.y - y * vector.x
  end

  def scalar_product(vector)
    x * vector.x + y * vector.y
  end

  def collinear_with?(vector)
    cross_product(vector) === 0
  end

  def modulus
    Math.hypot(x ,y)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flash_math-0.0.1 lib/flash_math/modules/geometry/geometric_vector.rb