Hello people! I hope you are well!

In this article I would like to present you a simple C++ 3D vector library. This is just a prototype and I am sure you can find other better and more complete alternatives. So, we are just dealing here with a “Yet Another 3D Vector Library”.

I created this library because I wanted to reuse some 3D vector operations in various OpenGL computer graphics exercise programs (from my M.Sc. studies). Of course, I could use an already existing library. However, I wanted also to understand how some basic 3D vector operations work.

So, these two reasons led me to create the library.

The operations the library contains are the following:

  • add two vectors
  • subtract two vectors
  • multiple a vector with a scalar value
  • divide a vector with a scalar value
  • normalize a vector
  • calculate the cross product of two vectors
  • calculate the inner product of two vectors
  • calculate the length of a vector
  • calculate the opposite of a vector

These were the vector operations that I needed for my programs.

Now, let me show you the header of the library:

#ifndef _VECTOR_3F_H_
#define _VECTOR_3F_H_

class Vector3f
{
    private:
        float x, y, z;

    public:
        Vector3f();

        Vector3f(const float x, const float y, const float z);

        float getX() const;

        float getY() const;

        float getZ() const;

        void setX(const float value);

        void setY(const float value);

        void setZ(const float value);

        void setValues(const float x, const float y, const float z);

        Vector3f operator + (const Vector3f & vector) const;

        Vector3f operator - (const Vector3f & vector) const;

        Vector3f operator * (const float scale) const;

        Vector3f operator / (const float scale) const;

        Vector3f crossProduct(const Vector3f & vector) const;

        float innerProduct(const Vector3f & vector) const;

        float length() const;

        Vector3f operator - () const;

        void opposite ();

        Vector3f normalized() const;

        void normalize();
};

#endif // _VECTOR_3F_H_

Here is the implementation of the library:

#include <cmath>

#include "Vector3f.h"

Vector3f::Vector3f() : Vector3f(0, 0, 0)
{
}

Vector3f::Vector3f(const float x, const float y, const float z)
{
    setValues(x, y, z);
}

float
Vector3f::getX() const
{
    return x;
}

float
Vector3f::getY() const
{
    return y;
}

float
Vector3f::getZ() const
{
    return z;
}

void
Vector3f::setX(const float value)
{
    x = value;
}

void
Vector3f::setY(const float value)
{
    y = value;
}

void
Vector3f::setZ(const float value)
{
    z = value;
}

void
Vector3f::setValues(const float x, const float y, const float z)
{
    setX(x);
    setY(y);
    setZ(z);
}

Vector3f
Vector3f::operator + (const Vector3f & vector) const
{
    return Vector3f(x + vector.getX(), y + vector.getY(), z + vector.getZ());
}

Vector3f
Vector3f::operator - (const Vector3f & vector) const
{
    return Vector3f(x - vector.getX(), y - vector.getY(), z - vector.getZ());
}

Vector3f
Vector3f::operator * (const float scale) const
{
    return Vector3f(x * scale, y * scale, z * scale);
}

Vector3f
Vector3f::operator / (const float scale) const
{
    return Vector3f(x / scale, y / scale, z / scale);
}

Vector3f
Vector3f::crossProduct(const Vector3f & vector) const
{
    return Vector3f(y * vector.getZ() - z * vector.getY(),
                    z * vector.getX() - x * vector.getZ(),
                    x * vector.getY() - y * vector.getX());
}

float
Vector3f::innerProduct(const Vector3f & vector) const
{
    return (x * vector.getX() + y * vector.getY() + z * vector.getZ());
}

float
Vector3f::length() const
{
    return sqrt(x*x + y*y + z*z);
}

Vector3f
Vector3f::operator - () const
{
    return Vector3f(-x, -y, -z);
}

void
Vector3f::opposite()
{
    setValues(-x, -y, -z);
}

Vector3f
Vector3f::normalized() const
{
    const float distance = length();

    return Vector3f(x / distance, y / distance, z / distance);
}

void
Vector3f::normalize()
{
    const float distance = length();

    setValues(x / distance, y / distance, z / distance);
}

Have a nice weekend!