• 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

    Unit vectors and normalization

    In vector mathematics, a unit vector is a vector of any given direction with magnitude 1. Each vector on the following image is a unit vector:

    Unit vectors

    The above example showed four simple unit vectors pointing at all directions of the graph, the following is a different example, of a unit vector with direction (1, 1):

    Unit vectors

    A very important fact about unit vectors, is that a unit vector multiplied by any scalar will have the same direction. This property is essential for any algorithm dealing with geometrical calculations. A perfect real-life usage example of unit vectors is moving a character in video games along a given direction with some set speed.

    The following example demonstrates finding different vectors with the use of unti vectors:

    Unit vectors

    Vector normalization

    A normalization operation is used to obtain a unit vector from any given vector. To normalize a vector means to divide each component of a vector with it's magnitude:

    v1 = {
        'x': 3,
        'y': 5
    }
    
    magnitude = math.sqrt(v1.x * v1.x + v1.y * v1.y)
    v1norm = {
        'x': v1.x / magnitude,
        'y': v1.y / magnitude
    }
    print(v1norm)
    
    {'x': 0.514496, 'y': 0.857493}
    

    This operation is implemented by GLM as simply:

    v1 = glm.vec2(3, 5)
    v1norm = glm.normalize(v1)