Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
geometry.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3
4#pragma once
5
6#include <cstdint>
7#include <expected>
8
9namespace portals {
10
16
24struct CellPoint {
26 std::int32_t x{};
28 std::int32_t y{};
29
34 bool operator==(const CellPoint &other) const noexcept = default;
35};
36
44struct CellSize {
46 std::int32_t width{};
48 std::int32_t height{};
49
53 [[nodiscard]] CellSize normalized() const noexcept;
54
59 bool operator==(const CellSize &other) const noexcept = default;
60};
61
69[[nodiscard]] std::expected<CellPoint, GeometryError> translated(CellPoint point, CellPoint offset) noexcept;
70
81class CellRect {
82public:
84 CellRect() noexcept = default;
85
93 [[nodiscard]] static std::expected<CellRect, GeometryError> from(CellPoint origin, CellSize available) noexcept;
94
98 [[nodiscard]] CellPoint origin() const noexcept;
102 [[nodiscard]] CellSize size() const noexcept;
106 [[nodiscard]] std::int32_t right() const noexcept;
110 [[nodiscard]] std::int32_t bottom() const noexcept;
114 [[nodiscard]] bool empty() const noexcept;
115
120 [[nodiscard]] bool contains(CellPoint point) const noexcept;
125 [[nodiscard]] bool contains(CellRect rect) const noexcept;
126
136 [[nodiscard]] CellRect intersection(CellRect clip) const noexcept;
137
144 [[nodiscard]] std::expected<CellRect, GeometryError> translated(CellPoint offset) const noexcept;
145
150 bool operator==(const CellRect &other) const noexcept = default;
151
152private:
154
155 CellPoint origin_{};
156 CellSize size_{};
157};
158
159} // namespace portals
CellPoint origin() const noexcept
Return the cell origin by value; no errors.
bool contains(CellPoint point) const noexcept
Test half-open point membership; an empty rectangle contains no point.
CellRect() noexcept=default
Create the empty rectangle at (0, 0); no errors.
bool empty() const noexcept
Whether either extent is zero; no errors.
std::expected< CellRect, GeometryError > translated(CellPoint offset) const noexcept
Return an owning translated rectangle, preserving its extents.
CellSize size() const noexcept
Return the normalized cell extents by value; no errors.
CellRect intersection(CellRect clip) const noexcept
Intersect with a clipping rectangle; no errors or invalidation.
std::int32_t right() const noexcept
Return the representable exclusive horizontal end; no errors.
std::int32_t bottom() const noexcept
Return the representable exclusive vertical end; no errors.
static std::expected< CellRect, GeometryError > from(CellPoint origin, CellSize available) noexcept
Create a rectangle after normalizing negative available extents to zero.
Public Portals API.
Definition action.hpp:8
std::expected< CellPoint, GeometryError > translated(CellPoint point, CellPoint offset) noexcept
Translate a cell point by an explicit columns/rows offset.
GeometryError
Failure of a checked cell-coordinate operation.
Definition geometry.hpp:12
@ COORDINATE_OVERFLOW
An origin or exclusive end would be outside the int32_t range.
Definition geometry.hpp:14
An owning value in terminal columns/rows, x right and y down.
Definition geometry.hpp:24
std::int32_t y
Vertical coordinate in terminal rows.
Definition geometry.hpp:28
std::int32_t x
Horizontal coordinate in terminal columns.
Definition geometry.hpp:26
bool operator==(const CellPoint &other) const noexcept=default
Compare both coordinates; no errors or invalidation.
An owning available extent in terminal columns/rows.
Definition geometry.hpp:44
CellSize normalized() const noexcept
Return an owning nonnegative extent; no errors or invalidation.
std::int32_t height
Available number of rows, possibly negative before normalization.
Definition geometry.hpp:48
std::int32_t width
Available number of columns, possibly negative before normalization.
Definition geometry.hpp:46