Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class cObject
- {
- private:
- int x, y;
- int width, height;
- public:
- cObject() { x = y = 0; width = height = 1; }
- cObject(int x, int y, int width, int height)
- {
- this->x = x;
- this->y = y;
- this->width = width;
- this->height = height;
- }
- int getLeft() { return x; }
- int getRight() { return x + width; }
- int getTop() { return y; }
- int getBottom() { return y + height; }
- bool CollidesWith(cObject & obj)
- {
- if (this->getBottom() <= obj.getTop())
- return false;
- if (this->getTop() >= obj.getBottom())
- return false;
- if (this->getRight() <= obj.getLeft())
- return false;
- if (this->getLeft() >= obj.getRight())
- return false;
- return true;
- }
- };
- int main()
- {
- cObject o1(0, 0, 10, 10);
- cObject o2(10, 8, 1, 1);
- if (o1.CollidesWith(o2))
- cout << "Yes" << endl;
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement