Files
pdf/engine/include/pdfengine/geometry/rect.hpp
T
2026-08-03 11:26:44 +05:30

84 lines
2.7 KiB
C++

#pragma once
#include <algorithm>
#include <cmath>
namespace pdfengine {
namespace geometry {
struct Point {
float x = 0.0f;
float y = 0.0f;
constexpr Point() noexcept = default;
constexpr Point(float xVal, float yVal) noexcept : x(xVal), y(yVal) {}
};
struct Size {
float width = 0.0f;
float height = 0.0f;
constexpr Size() noexcept = default;
constexpr Size(float w, float h) noexcept : width(w), height(h) {}
};
struct Rect {
float x = 0.0f;
float y = 0.0f;
float width = 0.0f;
float height = 0.0f;
constexpr Rect() noexcept = default;
constexpr Rect(float xVal, float yVal, float w, float h) noexcept
: x(xVal), y(yVal), width(w), height(h) {}
[[nodiscard]] constexpr float left() const noexcept { return x; }
[[nodiscard]] constexpr float top() const noexcept { return y; }
[[nodiscard]] constexpr float right() const noexcept { return x + width; }
[[nodiscard]] constexpr float bottom() const noexcept { return y + height; }
[[nodiscard]] constexpr float centerX() const noexcept { return x + width * 0.5f; }
[[nodiscard]] constexpr float centerY() const noexcept { return y + height * 0.5f; }
[[nodiscard]] constexpr float area() const noexcept { return width * height; }
[[nodiscard]] constexpr bool isEmpty() const noexcept { return width <= 0.0f || height <= 0.0f; }
[[nodiscard]] constexpr bool contains(float px, float py) const noexcept {
return px >= x && px <= right() && py >= y && py <= bottom();
}
[[nodiscard]] constexpr bool contains(const Point& pt) const noexcept {
return contains(pt.x, pt.y);
}
[[nodiscard]] constexpr bool intersects(const Rect& other) const noexcept {
return left() < other.right() && right() > other.left() &&
top() < other.bottom() && bottom() > other.top();
}
[[nodiscard]] constexpr Rect intersectWith(const Rect& other) const noexcept {
float l = std::max(left(), other.left());
float r = std::min(right(), other.right());
float t = std::max(top(), other.top());
float b = std::min(bottom(), other.bottom());
if (l >= r || t >= b) {
return Rect(0.0f, 0.0f, 0.0f, 0.0f);
}
return Rect(l, t, r - l, b - t);
}
[[nodiscard]] constexpr Rect combineWith(const Rect& other) const noexcept {
if (isEmpty()) return other;
if (other.isEmpty()) return *this;
float l = std::min(left(), other.left());
float r = std::max(right(), other.right());
float t = std::min(top(), other.top());
float b = std::max(bottom(), other.bottom());
return Rect(l, t, r - l, b - t);
}
};
} // namespace geometry
} // namespace pdfengine