Module: Vector2d::Arithmetic

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

Overview

The operators. A scalar operand applies to both coordinates. Anything else is coerced into a vector and applied one axis at a time.

Instance Method Summary collapse

Instance Method Details

#*(other) ⇒ self

Multiplies vectors.

Vector2d(1, 2) * Vector2d(2, 3) # => Vector2d(2, 6)
Vector2d(1, 2) * 2              # => Vector2d(2, 4)

Parameters:

  • other (Vector2d, Array, String, Hash, Integer, Float, Rational, BigDecimal, ::Vector, ::Matrix)

    anything Vector2d.parse accepts

Returns:

  • (self)


15
16
17
# File 'lib/vector2d/arithmetic.rb', line 15

def *(other)
  calculate_each(:*, other)
end

#+(other) ⇒ self

Adds vectors.

Vector2d(1, 2) + Vector2d(2, 3) # => Vector2d(3, 5)
Vector2d(1, 2) + 2              # => Vector2d(3, 4)

Parameters:

  • other (Vector2d, Array, String, Hash, Integer, Float, Rational, BigDecimal, ::Vector, ::Matrix)

    anything Vector2d.parse accepts

Returns:

  • (self)


37
38
39
# File 'lib/vector2d/arithmetic.rb', line 37

def +(other)
  calculate_each(:+, other)
end

#+@self

Returns the vector unchanged.

+Vector2d(2, 3) # => Vector2d(2,3)

Returns:

  • (self)


66
67
68
# File 'lib/vector2d/arithmetic.rb', line 66

def +@
  self
end

#-(other) ⇒ self

Subtracts vectors.

Vector2d(2, 3) - Vector2d(2, 1) # => Vector2d(0, 2)
Vector2d(4, 3) - 1              # => Vector2d(3, 2)

Parameters:

  • other (Vector2d, Array, String, Hash, Integer, Float, Rational, BigDecimal, ::Vector, ::Matrix)

    anything Vector2d.parse accepts

Returns:

  • (self)


48
49
50
# File 'lib/vector2d/arithmetic.rb', line 48

def -(other)
  calculate_each(:-, other)
end

#-@self

Returns the vector reversed.

-Vector2d(2, 3) # => Vector2d(-2,-3)

Returns:

  • (self)


57
58
59
# File 'lib/vector2d/arithmetic.rb', line 57

def -@
  reverse
end

#/(other) ⇒ self

Divides vectors.

Vector2d(4, 2) / Vector2d(2, 1) # => Vector2d(2, 2)
Vector2d(4, 2) / 2              # => Vector2d(2, 1)

Parameters:

  • other (Vector2d, Array, String, Hash, Integer, Float, Rational, BigDecimal, ::Vector, ::Matrix)

    anything Vector2d.parse accepts

Returns:

  • (self)


26
27
28
# File 'lib/vector2d/arithmetic.rb', line 26

def /(other)
  calculate_each(:/, other)
end

#reverseself

Reverses the vector.

Vector2d(2, 3).reverse # => Vector2d(-2,-3)

Returns:

  • (self)


75
76
77
# File 'lib/vector2d/arithmetic.rb', line 75

def reverse
  build(-x, -y)
end