The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
rect.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2014 - 2025
3 by Mark de Wever <koraq@xs4all.nl>
4 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY.
12
13 See the COPYING file for more details.
14*/
15
16#include "sdl/point.hpp"
17#include "sdl/rect.hpp"
18
19#include <cmath>
20#include <algorithm>
21#include <ostream>
22
23bool operator==(const SDL_Rect& a, const SDL_Rect& b)
24{
25 return SDL_RectsEqual(&a, &b) != false;
26}
27
28bool operator!=(const SDL_Rect& a, const SDL_Rect& b)
29{
30 return !operator==(a,b);
31}
32
33std::ostream& operator<<(std::ostream& s, const SDL_Rect& r)
34{
35 s << '[' << r.x << ',' << r.y << '|' << r.w << ',' << r.h << ']';
36 return s;
37}
38
39bool rect::operator==(const rect& r) const
40{
41 return SDL_RectsEqual(this, &r) != false;
42}
43
44bool rect::operator==(const SDL_Rect& r) const
45{
46 return SDL_RectsEqual(this, &r) != false;
47}
48
49bool rect::empty() const
50{
51 return SDL_RectEmpty(this);
52}
53
54bool rect::contains(int x, int y) const
55{
56 SDL_Point p{x, y};
57 return SDL_PointInRect(&p, this) != false;
58}
59
60bool rect::contains(const point& point) const
61{
62 return SDL_PointInRect(&point, this) != false;
63}
64
65bool rect::contains(const rect& r) const
66{
67 if(this->x > r.x) return false;
68 if(this->y > r.y) return false;
69 if(this->x + this->w < r.x + r.w) return false;
70 if(this->y + this->h < r.y + r.h) return false;
71 return true;
72}
73
74bool rect::overlaps(const rect& r) const
75{
76 return SDL_HasRectIntersection(this, &r);
77}
78
79rect rect::minimal_cover(const rect& other) const
80{
81 rect result;
82 SDL_GetRectUnion(this, &other, &result);
83 return result;
84}
85
87{
88 SDL_GetRectUnion(this, &other, this);
89 return *this;
90}
91
92rect rect::intersect(const rect& other) const
93{
94 rect result;
95 if(!SDL_GetRectIntersection(this, &other, &result)) {
96 return rect();
97 }
98 return result;
99}
100
101void rect::clip(const rect& other)
102{
103 *this = this->intersect(other);
104}
105
106void rect::shift(const point& other)
107{
108 this->x += other.x;
109 this->y += other.y;
110}
111
112rect rect::shifted_by(int x, int y) const
113{
114 rect res = *this;
115 res.x += x;
116 res.y += y;
117 return res;
118}
119
120rect rect::shifted_by(const point& other) const
121{
122 return shifted_by(other.x, other.y);
123}
124
125point rect::point_at(double x, double y) const
126{
127 return {
128 static_cast<int>(this->x + std::round(this->w * std::clamp(x, 0.0, 1.0))),
129 static_cast<int>(this->y + std::round(this->h * std::clamp(y, 0.0, 1.0)))
130 };
131}
132
133std::ostream& operator<<(std::ostream& s, const rect& r)
134{
135 s << '[' << r.x << ',' << r.y << '|' << r.w << ',' << r.h << ']';
136 return s;
137}
int w
Definition pathfind.cpp:188
bool operator==(const SDL_Rect &a, const SDL_Rect &b)
Definition rect.cpp:23
std::ostream & operator<<(std::ostream &s, const SDL_Rect &r)
Definition rect.cpp:33
bool operator!=(const SDL_Rect &a, const SDL_Rect &b)
Definition rect.cpp:28
Contains the SDL_Rect helper code.
Holds a 2D point.
Definition point.hpp:25
An abstract description of a rectangle with integer coordinates.
Definition rect.hpp:49
void clip(const rect &r)
Clip this rectangle by the given rectangle.
Definition rect.cpp:101
rect minimal_cover(const rect &r) const
Calculates the minimal rectangle that completely contains both this rectangle and the given rectangle...
Definition rect.cpp:79
bool empty() const
False if both w and h are > 0, true otherwise.
Definition rect.cpp:49
rect shifted_by(int x, int y) const
Returns a new rectangle shifted by the given relative position.
Definition rect.cpp:112
point point_at(double x, double y) const
Returns the proper point that corresponds to the given [0.0, 1.0] coordinates.
Definition rect.cpp:125
constexpr rect()
Explicitly initialize rects to 0.
Definition rect.hpp:52
bool contains(int x, int y) const
Whether the given point lies within the rectangle.
Definition rect.cpp:54
bool operator==(const rect &r) const
Definition rect.cpp:39
rect intersect(const rect &r) const
Calculates the intersection of this rectangle and another; that is, the maximal rectangle that is con...
Definition rect.cpp:92
void shift(const point &p)
Shift the rectangle by the given relative position.
Definition rect.cpp:106
rect & expand_to_cover(const rect &r)
Minimally expand this rect to fully contain another.
Definition rect.cpp:86
bool overlaps(const rect &r) const
Whether the given rectangle and this rectangle overlap.
Definition rect.cpp:74
mock_party p
static map_location::direction s
#define h
#define b