Sha256: 926b5f2e21fb4217c06007223679a3334154f7229365919a3a56b40c1138652d

Contents?: true

Size: 1.12 KB

Versions: 5

Compression:

Stored size: 1.12 KB

Contents

##
# Simple and fast 2 dimensional vector

class V
  # x coordinate accessors -- preferably float
  attr_accessor :x
  # y coordinate accessors -- preferably float
  attr_accessor :y

  class << self
    alias :[] :new # :nodoc:
  end

  ##
  # Create a new vector with x & y coordinates.

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

  # zero vector
  ZERO = V[0.0, 0.0]

  # one vector
  ONE  = V[1.0, 1.0]

  ##
  # Add two vectors, returning a new vector.

  def + v
    V[x+v.x, y+v.y]
  end

  ##
  # Subtract two vectors, returning a new vector.

  def - v
    V[x-v.x, y-v.y]
  end

  ##
  # Unary negation.

  def -@
    V[-x, -y]
  end

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

  def * s
    V[x*s, y*s]
  end

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

  def / s
    V[x/s, y/s]
  end

  def == other # :nodoc:
    x == other.x && y == other.y
  end

  ##
  # Return the length of the vector from the origin.

  def magnitude
    @magnitude ||= Math.sqrt(x*x + y*y)
  end

  def inspect # :nodoc:
    "#{self.class.name}[%.2f, %.2f]" % [x, y]
  end
  alias to_s inspect # :nodoc:
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
graphics-1.1.2 lib/graphics/v.rb
graphics-1.1.1 lib/graphics/v.rb
graphics-1.1.0 lib/graphics/v.rb
graphics-1.0.1 lib/graphics/v.rb
graphics-1.0.0 lib/graphics/v.rb