Reputation: 59
I am having a bit of an issue, I have a rectangle from contour detection but I want to re-size the rectangle whilst maintaining the centroid I noticed when I resize it, its obtaining a new centroid, this is because I want it to be at the center of my object not but when re-sized its moving to the sizes here is an image before resize, my centroid is marked by the red and blue markers
Before resize:
After resize:
Here is how I am resizing:
private static Rect rotateRect(Rect rect, int heightPercentage, int widthPercetange)
{
int originalX = 0;
int originalY = 0;
originalX = rect.x;
originalY = rect.y;
rect.height = resizeObject(rect.height, heightPercentage);
rect.width = resizeObject(rect.width, widthPercetange);
rect.x = originalX;
rect.y = originalY;
return rect;
}
private static int resizeObject(int resize, int percentage)
{
return (int)(resize *(percentage/100.0f));
}
Upvotes: 1
Views: 3770
Reputation: 41765
You simply need to add to x
and y
coordinates half the amount you remove from width
and height
.
private static Rect rotateRect(Rect rect, int heightPercentage, int widthPercetange)
{
int rwidth = rect.width;
int rheight = rect.height;
rect.width = Math.round((rect.width * widthPercetange) / 100.0f);
rect.height = Math.round((rect.height * heightPercentage) / 100.0f);
rect.x += (rwidth - rect.width) / 2;
rect.y += (rheight - rect.height) / 2;
return rect;
}
Upvotes: 1