Module: Vector2d::Projection::ClassMethods

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

Overview

The products taking both vectors as arguments. Extended into Vector2d, so they are called on the class.

Vector2d.dot_product(Vector2d(2, 1), Vector2d(2, 3)) # => 7

Instance Method Summary collapse

Instance Method Details

#cross_product(vector1, vector2) ⇒ Integer, ...

Calculates cross product of two vectors.

v1 = Vector2d(2, 1)
v2 = Vector2d(2, 3)
Vector2d.cross_product(v1, v2) # => 4

Parameters:

  • vector1 (Vector2d)

    the vector the product is measured from

  • vector2 (Vector2d)

    the vector the product is measured to

Returns:

  • (Integer, Float, Rational, BigDecimal)

    a scalar



35
36
37
# File 'lib/vector2d/projection.rb', line 35

def cross_product(vector1, vector2)
  (vector1.x * vector2.y) - (vector1.y * vector2.x)
end

#dot_product(vector1, vector2) ⇒ Integer, ...

Calculates dot product of two vectors.

v1 = Vector2d(2, 1)
v2 = Vector2d(2, 3)
Vector2d.dot_product(v1, v2) # => 7

Parameters:

  • vector1 (Vector2d)

    one of the vectors

  • vector2 (Vector2d)

    the other vector

Returns:

  • (Integer, Float, Rational, BigDecimal)

    a scalar



22
23
24
# File 'lib/vector2d/projection.rb', line 22

def dot_product(vector1, vector2)
  (vector1.x * vector2.x) + (vector1.y * vector2.y)
end