Commit 3ee460da authored by Davis King's avatar Davis King

Added set_rect_area()

parent 5bc1792d
......@@ -441,6 +441,30 @@ namespace dlib
return shrink_rect(rect, -width, -height);
}
inline drectangle set_rect_area (
const drectangle& rect,
double area
)
{
DLIB_ASSERT(area >= 0, "drectangle can't have a negative area.");
if (area == 0)
return drectangle(dcenter(rect));
if (rect.area() == 0)
{
// In this case we will make the output rectangle a square with the requested
// area.
double scale = std::sqrt(area);
return centered_drect(rect, scale, scale);
}
else
{
double scale = std::sqrt(area/rect.area());
return centered_drect(rect, rect.width()*scale, rect.height()*scale);
}
}
inline drectangle set_aspect_ratio (
const drectangle& rect,
double ratio
......
......@@ -585,6 +585,23 @@ namespace dlib
(i.e. grows the given drectangle by expanding its border)
!*/
// ----------------------------------------------------------------------------------------
drectangle set_rect_area (
const drectangle& rect,
double area
);
/*!
requires
- area >= 0
ensures
- Returns a rectangle R such that:
- center(R) == center(rect)
- R has the same aspect ratio as rect. If rect.area() == 0 then the
returned rect has a 1:1 aspect ratio.
- R.area() == area
!*/
// ----------------------------------------------------------------------------------------
drectangle set_aspect_ratio (
......
......@@ -726,6 +726,29 @@ namespace dlib
return rectangle(x, y, x+rect.width()-1, y+rect.height()-1);
}
// ----------------------------------------------------------------------------------------
inline rectangle set_rect_area (
const rectangle& rect,
unsigned long area
)
{
DLIB_ASSERT(area > 0);
if (rect.area() == 0)
{
// In this case we will make the output rectangle a square with the requested
// area.
unsigned long scale = std::round(std::sqrt(area));
return centered_rect(rect, scale, scale);
}
else
{
double scale = std::sqrt(area/(double)rect.area());
return centered_rect(rect, (long)std::round(rect.width()*scale), (long)std::round(rect.height()*scale));
}
}
// ----------------------------------------------------------------------------------------
inline rectangle set_aspect_ratio (
......
......@@ -487,6 +487,23 @@ namespace dlib
and height)
!*/
// ----------------------------------------------------------------------------------------
inline rectangle set_rect_area (
const rectangle& rect,
unsigned long area
);
/*!
requires
- area > 0
ensures
- Returns a rectangle R such that:
- center(R) == center(rect)
- R has the same aspect ratio as rect. If rect.area() == 0 then the
returned rect has a 1:1 aspect ratio.
- R.area() == area
!*/
// ----------------------------------------------------------------------------------------
inline rectangle set_aspect_ratio (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment