Standard Rectangle Class

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

Any ideas?

+4
source share
1 answer

If you are going to find him, he is likely to be part of another addiction. Therefore, it is best to try to write your own. Now is the time to practice creating a class template. :)

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, this is not the answer you expect.


As for your design, I hope you don’t take the GUI version, make a copy, and then modify it to the console version. It would be better to make a library; then the GUI and the console are just a matter of presentation.

0
source

Source: https://habr.com/ru/post/1306989/


All Articles