@@ -94,10 +94,7 @@ struct TRect {
9494 {size.width - r.size .width , size.height - r.size .height });
9595 }
9696
97- constexpr TRect operator *(Type scale) const {
98- return TRect ({origin.x * scale, origin.y * scale},
99- {size.width * scale, size.height * scale});
100- }
97+ constexpr TRect operator *(Type scale) const { return Scale (scale); }
10198
10299 constexpr TRect operator *(const TRect& r) const {
103100 return TRect ({origin.x * r.origin .x , origin.y * r.origin .y },
@@ -108,6 +105,20 @@ struct TRect {
108105 return origin == r.origin && size == r.size ;
109106 }
110107
108+ constexpr TRect Scale (Type scale) const {
109+ return TRect ({origin.x * scale, origin.y * scale},
110+ {size.width * scale, size.height * scale});
111+ }
112+
113+ constexpr TRect Scale (TPoint<T> scale) const {
114+ return TRect ({origin.x * scale.x , origin.y * scale.y },
115+ {size.width * scale.x , size.height * scale.y });
116+ }
117+
118+ constexpr TRect Scale (TSize<T> scale) const {
119+ return Scale (TPoint<T>(scale));
120+ }
121+
111122 constexpr bool Contains (const TPoint<Type>& p) const {
112123 return p.x >= GetLeft () && p.x < GetRight () && p.y >= GetTop () &&
113124 p.y < GetBottom ();
@@ -255,7 +266,7 @@ struct TRect {
255266
256267 // / @brief Returns a rectangle with expanded edges. Negative expansion
257268 // / results in shrinking.
258- constexpr TRect<T> Expand (T left, T top, T right, T bottom) {
269+ constexpr TRect<T> Expand (T left, T top, T right, T bottom) const {
259270 return TRect (origin.x - left, //
260271 origin.y - top, //
261272 size.width + left + right, //
@@ -264,12 +275,22 @@ struct TRect {
264275
265276 // / @brief Returns a rectangle with expanded edges in all directions.
266277 // / Negative expansion results in shrinking.
267- constexpr TRect<T> Expand (T amount) {
278+ constexpr TRect<T> Expand (T amount) const {
268279 return TRect (origin.x - amount, //
269280 origin.y - amount, //
270281 size.width + amount * 2 , //
271282 size.height + amount * 2 );
272283 }
284+
285+ // / @brief Returns a new rectangle that represents the projection of the
286+ // / source rectangle onto this rectangle. In other words, the source
287+ // / rectangle is redefined in terms of the corrdinate space of this
288+ // / rectangle.
289+ constexpr TRect<T> Project (TRect<T> source) const {
290+ return source.Shift (-origin).Scale (
291+ TSize<T>(1.0 / static_cast <Scalar>(size.width ),
292+ 1.0 / static_cast <Scalar>(size.height )));
293+ }
273294};
274295
275296using Rect = TRect<Scalar>;
0 commit comments