Am i the only one who miss a Rectangle::setCenter?


#1


I know the rectangle class already got a lot of methods, but what would you think of adding such ones? :

/** Returns a rectangle with the same size as this one, but a new center position. */
Rectangle withCenter (const ValueType newCenterX, const ValueType newCenterY) const noexcept   { return Rectangle (pos.x + newCenterX - getCentreX(), pos.y + newCenterY - getCentreY(), w, h); }
        
/** Returns a rectangle with the same size as this one, but a new center position. */
Rectangle withCenter (const Point<ValueType> newCenter) const noexcept   { return Rectangle (pos.x + newCenter.x - getCentreX(), pos.y + newCenter.y - getCentreY(), w, h); }

/** Changes the position of the rectangle's center (leaving its size unchanged). */
inline void setCenter (const Point<ValueType> newCenter) noexcept                       { pos += newCenter - getCentre(); }

/** Changes the position of the rectangle's center (leaving its size unchanged). */
inline void setCenter (const ValueType newCenterX, const ValueType newCenterY) noexcept       { pos.addXY (newCenterX - getCentreX(), newCenterY - getCentreY()); }

 

This is really usefull when setting bounds of widgets and labels (particularly when the text is centered!), as I'm almost always more interested about their center positions rather than their top-left..

I guess I'm not the only one in that case ?
 


#2

I actually needed this myself last week, but was in a hurry and didn't have time to add it to the Rectangle class.. Thanks for the reminder, I'll do that now!


#3

great! :)