• Home
  • About
  • Download
  • Changelog
  • User manual
  • Math
  • Math articles

    Math - 1 - BasicsMath - 2 - VectorsMath - 3 - Vector arithmeticsMath - 4 - Unit vectorsMath - 5 - Dot productMath - 6 - Cross product

    Vector arithmetics

    Just like with scalar numbers, you may perform addition and multiplication on vectors with another vectors, and vectors with scalar numbers. This note will present some examples of such calculations.

    Vector magnitude

    As mentioned in earlier notes, a vector has a direction and a magnitude (or length). A vector magnitude can be calculated by taking a square root of sum of vector's squared components:

    v1 = {
        'x': 3,
        'y': 5
    }
    
    magnitude = math.sqrt(v1.x * v1.x + v1.y * v1.y)
    

    Vector addition

    Two vectors a and b may be added together to receive a third vector c which could be defined as a vector a translated (or simply moved) by vector c.

    Vector addition

    In the above example you may see vector v1 (0, 3) and v2 (3, 0) added together resulting in vector v3 (3, 3).

    The addition operation of two vectors could be expressed as adding all components of one vector to corresponding components of another vector. An important thing to remember is that only the same-dimensional vectors may be added together, meaning that it's valid to add 2D vector to another 2D vector, or a 3D and 3D vector, but it is not valid to add 2D and 3D vectors.

    For example:

    v1 = {
        'x': 0,
        'y': 3
    }
    
    v2 = {
        'x': 3,
        'y': 0
    }
    
    v3 = {
        'x': v1.x + v2.x,
        'y': v1.y + v2.y
    }
    

    This operation is of course implemented in GLM out of the box, so everything you have to do, is to use addition operator on two (or more) vectors:

    v1 = glm.vec3(5, 0, 0)
    v2 = glm.vec3(5, 7, 9)
    v3 = v1 + v2
    
    vec3( 10, 7, 9 )
    

    Vector-scalar addition

    It is possible to perform addition on vectors and scalars. In this case, the result of such operation will be a vector, which components have been summed with a given scalar.

    Vector-scalar addition

    A GLM example:

    v1 = glm.vec2(4, 2)
    v2 = v1 + 10
    
    vec2( 14, 12 )
    

    Vector multiplication

    Similar to vector addition, two (or more) vectors may be multiplied together to receive a third vector which components have been multiplied together.

    Vector multiplication

    A GLM example:

    v1 = glm.vec3(1, 2, 3)
    v2 = glm.vec3(2, 3, 4)
    v3 = v1 * v2
    
    vec3( 2, 6, 12 )
    

    Vector-scalar multiplication

    Similar to vector-scalar addition, it is possible to multiply a vector by a scalar. This operation is known as scaling a vector, and is used much more often than vector-scalar addition.

    Vector-scalar multiplication

    A GLM example:

    v1 = glm.vec2(1, 2)
    v2 = v1 * 10
    
    vec3( 10, 20 )
    

    This operation is often used in conjunction with vector normalization to calculate displacement vectors which may be used for example in video games to move your character around. You may read more about normalization and unit vectors at this chapter of this module.