T
Das ist eine geometrische Frage, also erst einmal ein Blatt Papier her.
o---o
| |
o---o
o---o
| |
o---o
o---o
| |
o---o---o
| |
o---o
o---o
| o+--o
o--+o |
o---o
o-----o
| o+--o
| || |
| o+--o
o-----o
o-----o
|o---o|
|| ||
|o---o|
o-----o
o----oo--o
| || |
| || |
| o+--o
o-----o
o---o
| o---o
o---o |
o---o
Wenn mindestens eine Ecke in dem anderen Rechteck liegt, gibt es eine Überschneidung. War das die Frage?
struct Vector
{
int x, y;
Vector();
Vector(int x, int y);
};
struct Rect
{
Vector topLeft, bottomRight;
};
bool isInside(const Rect &rect, const Vector &point)
{
return
(point.x >= rect.topLeft.x) &&
(point.y >= rect.topLeft.y) &&
(point.x <= rect.bottomRight.x) &&
(point.y <= rect.bottomRight.y);
}
Vector topRight(const Rect &rect)
{
return Vector(rect.bottomRight.x, rect.topLeft.y);
}
Vector bottomLeft(const Rect &rect)
{
return Vector(rect.topLeft.x, rect.bottomRight.y);
}
size_t countVerticesInside(const Rect &area, const Rect &vertices)
{
size_t count = 0;
count += isInside(area, vertices.topLeft);
count += isInside(area, vertices.bottomRight);
count += isInside(area, topRight(vertices));
count += isInside(area, bottomLeft(vertices));
return count;
}
bool intersect(const Rect &first, const Rect &second)
{
const size_t verticesInFirst = countVerticesInside(first, second);
const size_t verticesInSecond = countVerticesInside(second, first);
return (verticesInFirst > 0) || (verticesInSecond > 0);
}
EDIT: Das soll erst einmal reichen.