blRectangle — A simple Rectangle class in c++ with useful functions

Introduction

This is part of my on going effort to publish all the mathematical tools that I have developed and used over the years. In this article, I present a simple rectangle class, which is very useful when dealing with images or graphics in general. As with the rest of my work, simplicity and code readability are my top priorities, because the key to code reuse, is to make the code understandable.

Here’s a little snippet showing how easy this rectangle class is to use:

int main(int argc, char *argv[])
{
    // Create a rectangle
    blRectangle<double> Rect1(0,0,10,10);

    // Create a second rectangle
    blRectangle<double> Rect2(blPoint2d<double>(-1.5,-3),blPoint2d<double>(10,100));

    cout << "Rect1 = " << Rect1 << "\n\n";
    cout << "Rect2 = " << Rect2 << "\n\n";

    cout << "Rect1 width = " << Rect1.Width() << "\n";
    cout << "Rect1 height = " << Rect1.Height() << "\n";
    cout << "Rect1 center = " << Rect1.Center() << "\n";
    cout << "Rect1 area = " << Rect1.Area() << "\n\n";

    cout << "Rect2 width = " << Rect2.Width() << "\n";
    cout << "Rect2 height = " << Rect2.Height() << "\n";
    cout << "Rect2 center = " << Rect2.Center() << "\n";
    cout << "Rect2 area = " << Rect2.Area() << "\n\n";

    int waitforkey;
    cin >> waitforkey;
}

The resulting console output is:

Dependencies

  1. blPoint2d
    1. Used to store coordinate pairs
    2. Used to return the center of the rectangle
  2. std::abs
    1. Used to calculate width and height of rectangle
  3. std::swap
    1. Used to sort the rectangle coordinates (p1 < p2)
  4. iostream library
    1. Used to output the rectangle to the console

The code

blRectangle structure is simple, but provides functions which are very useful when dealing with images or graphics. The structure is saved in a file called blRectangle.hpp, which uses inline documentation.

blRectangle.hpp (Click to see code…)

Usage

This rectangle structure can be used for anything from graphics to images, and the code is very readable. Here’s a little snippet showing just how easy it is to use:

int main(int argc, char *argv[])
{
    // Create a rectangle
    blRectangle<double> Rect1(0,0,10,10);

    // Create a second rectangle
    blRectangle<double> Rect2(blPoint2d<double>(-1.5,-3),blPoint2d<double>(10,100));

    cout << "Rect1 = " << Rect1 << "\n\n";
    cout << "Rect2 = " << Rect2 << "\n\n";

    // Translate Rect1 10 units left 5 units up
    Rect1.TranslateRectangle(-10,5);
    cout << "Translated Rect1 = " << Rect1 << "\n\n";

    // Move first pair of Rect1 to (-5.6,6.4)
    Rect1.MoveRectangleByPlacingP1At(-5.6,6.4);
    cout << "Moved Rect1 = " << Rect1 << "\n\n";

    // MOve second pair of Rect1 to (-10,10)
    Rect1.MoveRectangleByPlacingP2At(-10,10);
    cout << "Moved Rect1 = " << Rect1 << "\n\n";

    int waitforkey;
    cin >> waitforkey;
}

The resulting console output is:

Downloads

I have put all the files into a zip file which can be downloaded here. All you have to do is extract it somewhere, let’s say in a directory called blMathAPI, and then include the blMathAPI.hpp file as follows:

#include "blMathAPI.hpp"
using namespace blMathAPI;

Note: Everything is declared in the namespace blMathAPI, and such you would use it as: blMathAPI::blRectangle
[download#2]

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"

Leave a Reply

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