63 lines
1.5 KiB
C++
63 lines
1.5 KiB
C++
/**
|
|
* @file Rect.cpp
|
|
* @author apio (cloudapio.eu)
|
|
* @brief A simple 2D rectangle representation.
|
|
*
|
|
* @copyright Copyright (c) 2023, the Luna authors.
|
|
*
|
|
*/
|
|
|
|
#include <ui/Rect.h>
|
|
|
|
namespace ui
|
|
{
|
|
bool Rect::contains(Point point)
|
|
{
|
|
return (point.x >= pos.x) && (point.y >= pos.y) && (point.x <= (pos.x + width)) &&
|
|
(point.y <= (pos.y + height));
|
|
}
|
|
|
|
bool Rect::contains(Rect rect)
|
|
{
|
|
if (!contains(rect.pos)) return false;
|
|
Point rel = relative(rect.pos);
|
|
if ((rel.x + rect.width) > width) return false;
|
|
if ((rel.y + rect.height) > height) return false;
|
|
return true;
|
|
}
|
|
|
|
Point Rect::normalize(Point point)
|
|
{
|
|
if (point.x < pos.x) point.x = pos.x;
|
|
if (point.y < pos.y) point.y = pos.y;
|
|
if (point.x > pos.x + width) point.x = pos.x + width;
|
|
if (point.y > pos.y + height) point.y = pos.y + height;
|
|
return point;
|
|
}
|
|
|
|
Point Rect::relative(Point point)
|
|
{
|
|
point = normalize(point);
|
|
point.x -= pos.x;
|
|
point.y -= pos.y;
|
|
return point;
|
|
}
|
|
|
|
Point Rect::absolute(Point point)
|
|
{
|
|
point.x += pos.x;
|
|
point.y += pos.y;
|
|
return point;
|
|
}
|
|
|
|
Rect Rect::absolute(Rect rect)
|
|
{
|
|
return Rect { absolute(rect.pos), rect.width, rect.height };
|
|
}
|
|
|
|
Rect Rect::normalized()
|
|
{
|
|
return Rect { ui::Point { pos.x < 0 ? 0 : pos.x, pos.y < 0 ? 0 : pos.y }, width, height };
|
|
}
|
|
};
|