Fix the removal of implicit conversions in libfmt 10 by using explicit casts.
[0ad.git] / source / maths / Rect.cpp
blob394a03226281489e444ebd7ce4cead8479d4aed5
1 /* Copyright (C) 2021 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #include "precompiled.h"
20 #include "Rect.h"
22 #include "maths/Size2D.h"
23 #include "maths/Vector2D.h"
25 CRect::CRect() :
26 left(0.f), top(0.f), right(0.f), bottom(0.f)
30 CRect::CRect(const CRect& rect) :
31 left(rect.left), top(rect.top), right(rect.right), bottom(rect.bottom)
35 CRect::CRect(const CVector2D& pos) :
36 left(pos.X), top(pos.Y), right(pos.X), bottom(pos.Y)
40 CRect::CRect(const CSize2D& size) :
41 left(0.f), top(0.f), right(size.Width), bottom(size.Height)
45 CRect::CRect(const CVector2D& upperleft, const CVector2D& bottomright) :
46 left(upperleft.X), top(upperleft.Y), right(bottomright.X), bottom(bottomright.Y)
50 CRect::CRect(const CVector2D& pos, const CSize2D& size) :
51 left(pos.X), top(pos.Y), right(pos.X + size.Width), bottom(pos.Y + size.Height)
55 CRect::CRect(const float l, const float t, const float r, const float b) :
56 left(l), top(t), right(r), bottom(b)
60 CRect& CRect::operator=(const CRect& a)
62 left = a.left;
63 top = a.top;
64 right = a.right;
65 bottom = a.bottom;
66 return *this;
69 bool CRect::operator==(const CRect &a) const
71 return (left == a.left &&
72 top == a.top &&
73 right == a.right &&
74 bottom == a.bottom);
77 bool CRect::operator!=(const CRect& a) const
79 return !(*this == a);
82 CRect CRect::operator-() const
84 return CRect(-left, -top, -right, -bottom);
87 CRect CRect::operator+() const
89 return *this;
92 CRect CRect::operator+(const CRect& a) const
94 return CRect(left + a.left, top + a.top, right + a.right, bottom + a.bottom);
97 CRect CRect::operator+(const CVector2D& a) const
99 return CRect(left + a.X, top + a.Y, right + a.X, bottom + a.Y);
102 CRect CRect::operator+(const CSize2D& a) const
104 return CRect(left + a.Width, top + a.Height, right + a.Width, bottom + a.Height);
107 CRect CRect::operator-(const CRect& a) const
109 return CRect(left - a.left, top - a.top, right - a.right, bottom - a.bottom);
112 CRect CRect::operator-(const CVector2D& a) const
114 return CRect(left - a.X, top - a.Y, right - a.X, bottom - a.Y);
117 CRect CRect::operator-(const CSize2D& a) const
119 return CRect(left - a.Width, top - a.Height, right - a.Width, bottom - a.Height);
122 void CRect::operator+=(const CRect& a)
124 left += a.left;
125 top += a.top;
126 right += a.right;
127 bottom += a.bottom;
130 void CRect::operator+=(const CVector2D& a)
132 left += a.X;
133 top += a.Y;
134 right += a.X;
135 bottom += a.Y;
138 void CRect::operator+=(const CSize2D& a)
140 left += a.Width;
141 top += a.Height;
142 right += a.Width;
143 bottom += a.Height;
146 void CRect::operator-=(const CRect& a)
148 left -= a.left;
149 top -= a.top;
150 right -= a.right;
151 bottom -= a.bottom;
154 void CRect::operator-=(const CVector2D& a)
156 left -= a.X;
157 top -= a.Y;
158 right -= a.X;
159 bottom -= a.Y;
162 void CRect::operator-=(const CSize2D& a)
164 left -= a.Width;
165 top -= a.Height;
166 right -= a.Width;
167 bottom -= a.Height;
170 float CRect::GetWidth() const
172 return right-left;
175 float CRect::GetHeight() const
177 return bottom-top;
180 CSize2D CRect::GetSize() const
182 return CSize2D(right - left, bottom - top);
185 CVector2D CRect::TopLeft() const
187 return CVector2D(left, top);
190 CVector2D CRect::TopRight() const
192 return CVector2D(right, top);
195 CVector2D CRect::BottomLeft() const
197 return CVector2D(left, bottom);
200 CVector2D CRect::BottomRight() const
202 return CVector2D(right, bottom);
205 CVector2D CRect::CenterPoint() const
207 return CVector2D((left + right) / 2.f, (top + bottom) / 2.f);
210 bool CRect::PointInside(const CVector2D& point) const
212 return (point.X >= left &&
213 point.X <= right &&
214 point.Y >= top &&
215 point.Y <= bottom);
218 CRect CRect::Scale(float x, float y) const
220 return CRect(left * x, top * y, right * x, bottom * y);