FifeGUI 0.2.0
A C++ GUI library designed for games.
rectangle.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2004 - 2008 Olof Naessén and Per Larsson
3// SPDX-FileCopyrightText: 2013 - 2026 Fifengine contributors
4
5#include "fifechan/rectangle.hpp"
6
7#include <ostream>
8
9namespace fcn
10{
11 Rectangle::Rectangle() : x(0), y(0), width(0), height(0) { }
12
13 Rectangle::Rectangle(int x_, int y_, int width_, int height_) : x(x_), y(y_), width(width_), height(height_) { }
14
15 void Rectangle::setAll(int x_, int y_, int width_, int height_)
16 {
17 x = x_;
18 y = y_;
19 width = width_;
20 height = height_;
21 }
22
23 bool Rectangle::isIntersecting(Rectangle const & rectangle) const
24 {
25 int x_ = x;
26 int y_ = y;
27 int width_ = width;
28 int height_ = height;
29
30 x_ -= rectangle.x;
31 y_ -= rectangle.y;
32
33 if (x_ < 0) {
34 width_ += x_;
35 } else if (x_ + width_ > rectangle.width) {
36 width_ = rectangle.width - x_;
37 }
38
39 if (y_ < 0) {
40 height_ += y_;
41 } else if (y_ + height_ > rectangle.height) {
42 height_ = rectangle.height - y_;
43 }
44
45 if (width_ <= 0 || height_ <= 0) {
46 return false;
47 }
48
49 return true;
50 }
51
52 bool Rectangle::isContaining(int x_, int y_) const
53 {
54 if (isEmpty()) {
55 return false;
56 }
57
58 return x_ >= x && y_ >= y && x_ < x + width && y_ < y + height;
59 }
60
61 bool Rectangle::isContaining(Rectangle const & other) const
62 {
63 if (other.isEmpty()) {
64 return false;
65 }
66
67 return other.x >= x && other.y >= y && other.x + other.width <= x + width &&
68 other.y + other.height <= y + height;
69 }
70
71 bool Rectangle::isEmpty() const
72 {
73 return width <= 0 || height <= 0;
74 }
75
77 {
78 if (rh.isEmpty()) {
79 return {x, y, width, height};
80 }
81
82 int const nx = x < rh.x ? x : rh.x;
83 int const ny = y < rh.y ? y : rh.y;
84 int const nx2 = x + width > rh.x + rh.width ? x + width : rh.x + rh.width;
85 int const ny2 = y + height > rh.y + rh.height ? y + height : rh.y + rh.height;
86 return {nx, ny, nx2 - nx, ny2 - ny};
87 }
88
90 {
91 if (rh.isEmpty()) {
92 return *(this);
93 }
94
95 if (isEmpty()) {
96 *this = rh; // avoid use-after-free, assign instead of returning rh
97 return *this;
98 }
99
100 x = x < rh.x ? x : rh.x;
101 y = y < rh.y ? y : rh.y;
102 int const x2 = x + width > rh.x + rh.width ? x + width : rh.x + rh.width;
103 int const y2 = y + height > rh.y + rh.height ? y + height : rh.y + rh.height;
104 width = x2 - x;
105 height = y2 - y;
106 return *(this);
107 }
108
110 {
111 int const nx = x > rh.x ? x : rh.x;
112 int const ny = y > rh.y ? y : rh.y;
113
114 if (rh.isEmpty() || isEmpty()) {
115 return {nx, ny, 0, 0};
116 }
117
118 int const nx2 = x + width < rh.x + rh.width ? x + width : rh.x + rh.width;
119 int const ny2 = y + height < rh.y + rh.height ? y + height : rh.y + rh.height;
120 Rectangle result(nx, ny, nx2 - nx, ny2 - ny);
121
122 if (result.isEmpty()) {
123 return {nx, ny, 0, 0};
124 }
125
126 return result;
127 }
128
129 std::ostream& operator<<(std::ostream& out, Rectangle const & rectangle)
130 {
131 out << "Rectangle [x = " << rectangle.x << ", y = " << rectangle.y << ", width = " << rectangle.width
132 << ", height = " << rectangle.height << "]";
133
134 return out;
135 }
136} // namespace fcn
friend std::ostream & operator<<(std::ostream &out, Rectangle const &rectangle)
Output operator for output.
Rectangle()
Constructor.
Definition rectangle.cpp:11
bool isContaining(int x, int y) const
Checks the rectangle contains a point.
Definition rectangle.cpp:52
int width
Holds the width of the rectangle.
Rectangle intersection(Rectangle const &rh) const
Gets the intersection between two rectangles.
int y
Holds the x coordinate of the rectangle.
void setAll(int x, int y, int width, int height)
Sets the dimension of a rectangle.
Definition rectangle.cpp:15
Rectangle operator+(Rectangle const &rh) const
Adds a rectangle to this rectangle.
Definition rectangle.cpp:76
int x
Holds the x coordinate of the rectangle.
int height
Holds the height of the rectangle.
Rectangle const & operator+=(Rectangle const &rh)
Adds a rectangle to this rectangle.
Definition rectangle.cpp:89
bool isIntersecting(Rectangle const &rectangle) const
Checks if another rectangle intersects with the rectangle.
Definition rectangle.cpp:23
bool isEmpty() const
Checks whether the rectangle is empty or not.
Definition rectangle.cpp:71