Class: Vector2d

Inherits:
Object
  • Object
show all
Extended by:
Calculations::ClassMethods
Includes:
Calculations, Coercions, Fitting, Properties, Transformations
Defined in:
lib/vector2d.rb,
lib/vector2d/fitting.rb,
lib/vector2d/version.rb,
lib/vector2d/coercions.rb,
lib/vector2d/properties.rb,
lib/vector2d/calculations.rb,
lib/vector2d/transformations.rb

Defined Under Namespace

Modules: Calculations, Coercions, Fitting, Properties, Transformations

Constant Summary collapse

VERSION =
"2.0.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Calculations::ClassMethods

angle_between, cross_product, dot_product

Methods included from Transformations

#ceil, #floor, #normalize, #perpendicular, #resize, #reverse, #round, #truncate

Methods included from Properties

#angle, #aspect_ratio, #length, #normalized?, #squared_length

Methods included from Fitting

#contain, #fit, #fit_either

Methods included from Coercions

#coerce, #inspect, #to_a, #to_f_vector, #to_hash, #to_i_vector, #to_s

Methods included from Calculations

#*, #+, #-, #/, #angle_between, #cross_product, #distance, #dot_product, #squared_distance

Constructor Details

#initialize(x, y) ⇒ Vector2d

Returns a new instance of Vector2d.



72
73
74
# File 'lib/vector2d.rb', line 72

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

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



70
71
72
# File 'lib/vector2d.rb', line 70

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



70
71
72
# File 'lib/vector2d.rb', line 70

def y
  @y
end

Class Method Details

.parse(arg, second_arg = nil) ⇒ Object

Creates a new vector. The following examples are all valid:

Vector2d.parse(150, 100)
Vector2d.parse(150.0, 100.0)
Vector2d.parse("150x100")
Vector2d.parse("150.0x100.0")
Vector2d.parse([150,100})
Vector2d.parse({x: 150, y: 100})
Vector2d.parse({"x" => 150.0, "y" => 100.0})
Vector2d.parse(Vector2d(150, 100))


30
31
32
33
34
35
36
# File 'lib/vector2d.rb', line 30

def parse(arg, second_arg=nil)
  if second_arg.nil?
    parse_single_arg(arg)
  else
    self.new(arg, second_arg)
  end
end

Instance Method Details

#==(comp) ⇒ Object

Compares two vectors

Vector2d(2, 3) == Vector2d(2, 3) # => true
Vector2d(2, 3) == Vector2d(1, 0) # => false


81
82
83
# File 'lib/vector2d.rb', line 81

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