I know Rectangle already has many methods, so I would understand if you don’t want to add the following ones, but I think they would be really useful and avoid quite some typing when writing resizable interfaces.
the removeFromXX methods are great, but I keep having to calculate the amountToRemove before calling them.
/** Removes a strip from the top of this rectangle, reducing this rectangle
by the specified proportion and returning the section that was removed.
E.g. if this rectangle is (100, 100, 200, 200) and proportionToRemove is 0.25, this will
return (100, 100, 200, 50) and leave this rectangle as (100, 150, 200, 150).
*/
template <typename FloatType>
Rectangle removeProportionFromTop (FloatType proportionToRemove) noexcept
{
const Rectangle r (pos.x, pos.y, w, ValueType (proportionToRemove * h));
pos.y += r.h; h -= r.h;
return r;
}
/** Removes a strip from the left-hand edge of this rectangle, reducing this rectangle
by the specified proportion and returning the section that was removed.
E.g. if this rectangle is (100, 100, 200, 200) and proportionToRemove is 0.25, this will
return (100, 100, 50, 200) and leave this rectangle as (150, 100, 150, 200).
*/
template <typename FloatType>
Rectangle removeProportionFromLeft (FloatType proportionToRemove) noexcept
{
const Rectangle r (pos.x, pos.y, ValueType (proportionToRemove * w), h);
pos.x += r.w; w -= r.w;
return r;
}
/** Removes a strip from the right-hand edge of this rectangle, reducing this rectangle
by the specified proportion and returning the section that was removed.
E.g. if this rectangle is (100, 100, 200, 200) and proportionToRemove is 0.25, this will
return (150, 100, 50, 200) and leave this rectangle as (100, 100, 50, 200).
*/
template <typename FloatType>
Rectangle removeProportionFromRight (FloatType proportionToRemove) noexcept
{
const ValueType amountToRemove = ValueType (proportionToRemove * w);
const Rectangle r (pos.x + w - amountToRemove, pos.y, amountToRemove, h);
w -= amountToRemove;
return r;
}
/** Removes a strip from the bottom of this rectangle, reducing this rectangle
by the specified proportion and returning the section that was removed.
E.g. if this rectangle is (100, 100, 200, 200) and proportionToRemove is 0.25, this will
return (100, 150, 200, 50) and leave this rectangle as (100, 100, 200, 150).
*/
template <typename FloatType>
Rectangle removeProportionFromBottom (FloatType proportionToRemove) noexcept
{
const ValueType amountToRemove = ValueType (proportionToRemove * h);
const Rectangle r (pos.x, pos.y + h - amountToRemove, w, amountToRemove);
h -= amountToRemove;
return r;
}



