tags:

views:

64

answers:

1

I have a project that has a GUI (written in QT) and a command-line version. I made use of the rectangle class included in QT: QRect. I would like to break the command-line version's dependency on QT, so I need a drop-in rectangle class that supports intersection and union. I could write one, but I'd prefer including one if possible.

Any ideas?

A: 

If you're going to find one to include, it's probably part of another dependency. So your best bet is to try to write your own. Now is a good time to practice making a template class. :)

template <typename T>
struct point
{
    // or maybe you'd prefer to make these private
    T x;
    T y;
};

template <typename T>
struct rectangle
{
public:
    typedef point<T> point_type;

    bool contains(const point_type& pPoint)
    {
        return !(pPoint.x < topleft.x) && (pPoint.x < bottomright.x) && 
                !(pPoint.y < topleft.y) && (pPoint.y < bottomright.y);
    }

    T width(void) const
    {
        return bottomright.x - topleft.x;
    }

    // and more stuff

    // or maybe you'd prefer to make these private, nor
    // is this the only way to represent a rectangle.
    point_type topleft;
    point_type bottomright;
};

Sorry it's not the answer you're expecting.


Just about your design, I hope you're not taking your GUI version, performing a copy, then modifying it into a console version. Better would be to make a library; then GUI versus console is merely a matter of presentation.

GMan
That's fine, thanks for starting it off (nice attention to boundary cases by the way.)Anyway, it was worth a shot asking. Smaller things have made it into boost. Hopefully this gets put in too....
Neil G