FifeGUI 0.3.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// Corresponding header include
6#include "fifechan/rectangle.hpp"
7
8// Standard library includes
9#include <cassert>
10#include <ostream>
11
12namespace fcn
13{
14 Rectangle::Rectangle() : x(0), y(0), width(0), height(0)
15 {
16 }
17
18 Rectangle::Rectangle(int x_, int y_, int width_, int height_) : x(x_), y(y_), width(width_), height(height_)
19 {
20 assert("Width must be non-negative" && width_ >= 0);
21 assert("Height must be non-negative" && height_ >= 0);
22 }
23
24 void Rectangle::setAll(int x_, int y_, int width_, int height_)
25 {
26 assert("Width must be non-negative" && width_ >= 0);
27 assert("Height must be non-negative" && height_ >= 0);
28 x = x_;
29 y = y_;
30 width = width_;
31 height = height_;
32 }
33
34 bool Rectangle::isIntersecting(Rectangle const & rectangle) const
35 {
36 int x_ = x;
37 int y_ = y;
38 int width_ = width;
39 int height_ = height;
40
41 x_ -= rectangle.x;
42 y_ -= rectangle.y;
43
44 if (x_ < 0) {
45 width_ += x_;
46 } else if (x_ + width_ > rectangle.width) {
47 width_ = rectangle.width - x_;
48 }
49
50 if (y_ < 0) {
51 height_ += y_;
52 } else if (y_ + height_ > rectangle.height) {
53 height_ = rectangle.height - y_;
54 }
55
56 if (width_ <= 0 || height_ <= 0) {
57 return false;
58 }
59
60 return true;
61 }
62
63 bool Rectangle::isContaining(int x_, int y_) const
64 {
65 if (isEmpty()) {
66 return false;
67 }
68
69 return x_ >= x && y_ >= y && x_ < x + width && y_ < y + height;
70 }
71
72 bool Rectangle::isContaining(Rectangle const & other) const
73 {
74 if (other.isEmpty()) {
75 return false;
76 }
77
78 return other.x >= x && other.y >= y && other.x + other.width <= x + width &&
79 other.y + other.height <= y + height;
80 }
81
82 bool Rectangle::isEmpty() const
83 {
84 return width <= 0 || height <= 0;
85 }
86
88 {
89 if (rh.isEmpty()) {
90 return {x, y, width, height};
91 }
92
93 int const nx = x < rh.x ? x : rh.x;
94 int const ny = y < rh.y ? y : rh.y;
95 int const nx2 = x + width > rh.x + rh.width ? x + width : rh.x + rh.width;
96 int const ny2 = y + height > rh.y + rh.height ? y + height : rh.y + rh.height;
97 return {nx, ny, nx2 - nx, ny2 - ny};
98 }
99
101 {
102 if (rh.isEmpty()) {
103 return *(this);
104 }
105
106 if (isEmpty()) {
107 *this = rh; // avoid use-after-free, assign instead of returning rh
108 return *this;
109 }
110
111 x = x < rh.x ? x : rh.x;
112 y = y < rh.y ? y : rh.y;
113 int const x2 = x + width > rh.x + rh.width ? x + width : rh.x + rh.width;
114 int const y2 = y + height > rh.y + rh.height ? y + height : rh.y + rh.height;
115 width = x2 - x;
116 height = y2 - y;
117 return *(this);
118 }
119
121 {
122 int const nx = x > rh.x ? x : rh.x;
123 int const ny = y > rh.y ? y : rh.y;
124
125 if (rh.isEmpty() || isEmpty()) {
126 return {nx, ny, 0, 0};
127 }
128
129 int const nx2 = x + width < rh.x + rh.width ? x + width : rh.x + rh.width;
130 int const ny2 = y + height < rh.y + rh.height ? y + height : rh.y + rh.height;
131
132 int const w = nx2 - nx;
133 int const h = ny2 - ny;
134
135 if (w <= 0 || h <= 0) {
136 return {nx, ny, 0, 0};
137 }
138
139 return {nx, ny, w, h};
140 }
141
142 std::ostream& operator<<(std::ostream& out, Rectangle const & rectangle)
143 {
144 out << "Rectangle [x = " << rectangle.x << ", y = " << rectangle.y << ", width = " << rectangle.width
145 << ", height = " << rectangle.height << "]";
146
147 return out;
148 }
149} // namespace fcn
Rectangle()
Constructor.
Definition rectangle.cpp:14
bool isContaining(int x, int y) const
Checks the rectangle contains a point.
Definition rectangle.cpp:63
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:24
Rectangle operator+(Rectangle const &rh) const
Adds a rectangle to this rectangle.
Definition rectangle.cpp:87
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.
bool isIntersecting(Rectangle const &rectangle) const
Checks if another rectangle intersects with the rectangle.
Definition rectangle.cpp:34
bool isEmpty() const
Checks whether the rectangle is empty or not.
Definition rectangle.cpp:82
Used replacement tokens by configure_file():
FIFEGUI_API std::ostream & operator<<(std::ostream &out, Color const &color)
Definition color.cpp:312