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"
22 #include "maths/Size2D.h"
23 #include "maths/Vector2D.h"
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
)
69 bool CRect::operator==(const CRect
&a
) const
71 return (left
== a
.left
&&
77 bool CRect::operator!=(const CRect
& a
) const
82 CRect
CRect::operator-() const
84 return CRect(-left
, -top
, -right
, -bottom
);
87 CRect
CRect::operator+() const
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
)
130 void CRect::operator+=(const CVector2D
& a
)
138 void CRect::operator+=(const CSize2D
& a
)
146 void CRect::operator-=(const CRect
& a
)
154 void CRect::operator-=(const CVector2D
& a
)
162 void CRect::operator-=(const CSize2D
& a
)
170 float CRect::GetWidth() const
175 float CRect::GetHeight() const
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
&&
218 CRect
CRect::Scale(float x
, float y
) const
220 return CRect(left
* x
, top
* y
, right
* x
, bottom
* y
);