Iros
 
Loading...
Searching...
No Matches
rect.h
Go to the documentation of this file.
1#pragma once
2
3#include "diusgfx/point.h"
4#include "diusgfx/size2d.h"
5
6namespace gfx {
7class Rect {
8public:
9 Rect() = default;
10 constexpr Rect(f32 x, f32 y, f32 width, f32 height) : m_top_left(x, y), m_size(width, height) {}
11 constexpr Rect(Point top_left, Size2d size) : m_top_left(top_left), m_size(size) {}
12
13 constexpr auto x() -> f32& { return m_top_left.x(); }
14 constexpr auto x() const -> f32 { return m_top_left.x(); }
15
16 constexpr auto y() -> f32& { return m_top_left.y(); }
17 constexpr auto y() const -> f32 { return m_top_left.y(); }
18
19 constexpr auto width() -> f32& { return m_size.width(); }
20 constexpr auto width() const -> f32 { return m_size.width(); }
21
22 constexpr auto height() -> f32& { return m_size.height(); }
23 constexpr auto height() const -> f32 { return m_size.height(); }
24
25 constexpr auto top_left() -> Point& { return m_top_left; }
26 constexpr auto top_left() const -> Point { return m_top_left; }
27
28 constexpr auto size() -> Size2d& { return m_size; }
29 constexpr auto size() const -> Size2d { return m_size; }
30
31 constexpr auto left() const -> f32 { return x(); }
32 constexpr auto right() const -> f32 { return x() + width(); }
33 constexpr auto top() const -> f32 { return y(); }
34 constexpr auto bottom() const -> f32 { return y() + height(); }
35
36 constexpr auto with_x(f32 x) const -> Rect { return { x, y(), width(), height() }; }
37 constexpr auto with_y(f32 y) const -> Rect { return { x(), y, width(), height() }; }
38 constexpr auto with_top_left(Point p) const -> Rect { return { p, size() }; }
39
40 constexpr auto with_width(f32 width) const -> Rect { return { x(), y(), width, height() }; }
41 constexpr auto with_height(f32 height) const -> Rect { return { x(), y(), width(), height }; }
42 constexpr auto with_size(Size2d size) const -> Rect { return { top_left(), size }; }
43
44 constexpr auto top_right() const -> Point { return top_left().with_x(right()); }
45 constexpr auto bottom_left() const -> Point { return top_left().with_y(bottom()); }
46 constexpr auto bottom_right() const -> Point { return top_right().with_y(bottom()); }
47
48 constexpr auto center() const -> Point {
49 auto result = top_left();
50 result.x() += size().width() / 2;
51 result.y() += size().height() / 2;
52 return result;
53 }
54
55 constexpr auto contains(Point p) const -> bool {
56 auto [x, y] = p;
57 return x >= top_left().x() && x <= bottom_right().x() && y >= top_left().y() && y <= bottom_right().y();
58 }
59 constexpr auto contains(Rect other) const -> bool { return contains(other.top_left()) && contains(bottom_right()); }
60
61 auto operator==(Rect const&) const -> bool = default;
62
63private:
64 Point m_top_left;
65 Size2d m_size;
66};
67}
constexpr auto y() -> f32 &
Definition rect.h:16
constexpr auto height() const -> f32
Definition rect.h:23
constexpr Rect(f32 x, f32 y, f32 width, f32 height)
Definition rect.h:10
constexpr Rect(Point top_left, Size2d size)
Definition rect.h:11
constexpr auto left() const -> f32
Definition rect.h:31
constexpr auto height() -> f32 &
Definition rect.h:22
auto operator==(Rect const &) const -> bool=default
constexpr auto top_right() const -> Point
Definition rect.h:44
constexpr auto y() const -> f32
Definition rect.h:17
constexpr auto center() const -> Point
Definition rect.h:48
constexpr auto top_left() const -> Point
Definition rect.h:26
constexpr auto with_y(f32 y) const -> Rect
Definition rect.h:37
constexpr auto width() -> f32 &
Definition rect.h:19
constexpr auto size() -> Size2d &
Definition rect.h:28
constexpr auto with_height(f32 height) const -> Rect
Definition rect.h:41
constexpr auto with_x(f32 x) const -> Rect
Definition rect.h:36
constexpr auto size() const -> Size2d
Definition rect.h:29
constexpr auto width() const -> f32
Definition rect.h:20
constexpr auto with_width(f32 width) const -> Rect
Definition rect.h:40
constexpr auto x() const -> f32
Definition rect.h:14
constexpr auto bottom_left() const -> Point
Definition rect.h:45
constexpr auto top_left() -> Point &
Definition rect.h:25
constexpr auto bottom() const -> f32
Definition rect.h:34
constexpr auto contains(Point p) const -> bool
Definition rect.h:55
constexpr auto with_size(Size2d size) const -> Rect
Definition rect.h:42
Rect()=default
constexpr auto with_top_left(Point p) const -> Rect
Definition rect.h:38
constexpr auto bottom_right() const -> Point
Definition rect.h:46
constexpr auto right() const -> f32
Definition rect.h:32
constexpr auto x() -> f32 &
Definition rect.h:13
constexpr auto top() const -> f32
Definition rect.h:33
constexpr auto contains(Rect other) const -> bool
Definition rect.h:59
float f32
Definition floats.h:4
Definition bitmap.h:14
di::math::linalg::Vec< PointTag > Point
Definition point.h:31
di::math::linalg::Vec< Size2dType > Size2d
Definition size2d.h:31