blPoint2d — A simple 2d point structure in c++

Introduction

When developing physics, engineering or any other type of technical applications, there are basic mathematical tools that I have found myself using time over time. In this article, I design and build a simple two-dimensional point/vector structure with some basic functionality, keeping in mind code readability.

Here’s a snippet of this point structure, showing how simple it is to use, and how easy it is to read the code:

int main(int argc, char *argv[])
{
    // Create a point P1
    blPoint2d<double> P1(2.1,3.4);

    // Create a copy of P1
    blPoint2d</double><double> P2 = P1;

    // Calculate the dot product
    double DotP1P2 = P1*P2;

    // Get a perpendicular point
    blPoint2d</double><double> P3 = P1.GetPerpendicularUnitPoint();

    // Calculate the dot product
    // between points P1 and P3 (should be zero)
    double DotP1P3 = P1*P3;

    // Create a scalar value
    double Scalar = 5;

    // Output some multiplications and divisions
    // to the console
    cout < < "P1 = " << P1 << "\n";
    cout << "P2 = " << P2 << "\n";
    cout << "P3 = " << P3 << "\n";
    cout << "DotP1P2 = " << DotP1P2 << "\n";
    cout << "DotP1P3 = " << DotP1P3 << "\n";
    cout << "P1*Scalar = " << P1*Scalar << "\n";
    cout << "Scalar*P1 = " << Scalar*P1 << "\n";
    cout << "P1/Scalar = " << P1/Scalar << "\n";
    cout << "Norm1 of P1 = " << P1.Norm1() << "\n";
    cout << "Norm2 of P1 = " << P1.Norm2() << "\n";
    cout << "Infinity Norm of P1 = " << P1.NormInf() << "\n";

    int waitforkey;
    cin >> waitforkey;
}

The resulting console output from this little snippet is:

Dependencies

  1. std::abs from the standard library
    1. Used to calculate the norms
  2. std::sqrt from the standard library
    1. Used to calculate the magnitude
  3. iostream library
    1. Used to output to console

The code

blPoint2d structure is simple, but provides overloaded operators to make the code very readable. The structure is saved in a file called blPoint2d.hpp, which uses inline documentation.

blPoint2d.hpp (Click to see code…)

About Vincenzo Barbato

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few.

Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes.

Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration.

His outlook on life:

"Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

About Enzo

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few. Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes. Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration. His outlook on life: "Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

2 Comments

  1. Pingback: blVector3d — A simple 3d vector in c++ | BarbatoLabs

  2. Pingback: blRectangle — A simple Rectangle class in c++ with useful functions | BarbatoLabs

Leave a Reply

Your email address will not be published. Required fields are marked *