Module: Vector2d::Properties

Included in:
Vector2d
Defined in:
lib/vector2d/properties.rb

Instance Method Summary collapse

Instance Method Details

#angleObject

Angle of vector.

Vector2d(2, 3).angle # => 0.9827..


9
10
11
# File 'lib/vector2d/properties.rb', line 9

def angle
  Math.atan2(y, x)
end

#aspect_ratioObject

Aspect ratio of vector.

Vector2d(2, 3).aspect_ratio # => 0.6667..


17
18
19
# File 'lib/vector2d/properties.rb', line 17

def aspect_ratio
  (x.to_f / y).abs
end

#lengthObject

Length of vector.

Vector2d(2, 3).length # => 3.6055..


25
26
27
# File 'lib/vector2d/properties.rb', line 25

def length
  Math.sqrt(squared_length)
end

#normalized?Boolean

Is this a normalized vector?

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

Returns:

  • (Boolean)


42
43
44
# File 'lib/vector2d/properties.rb', line 42

def normalized?
  (length.to_f - 1.0).abs < Float::EPSILON
end

#squared_lengthObject

Squared length of vector.

Vector2d(2, 3).squared_length # => 13


33
34
35
# File 'lib/vector2d/properties.rb', line 33

def squared_length
  (x * x) + (y * y)
end