Added a version of set_aspect_ratio() that works for drectangle.

This commit is contained in:
Davis King 2015-07-03 11:54:56 -04:00
parent ad9a9e6efa
commit 16efd4698c
2 changed files with 54 additions and 0 deletions

View File

@ -385,6 +385,15 @@ namespace dlib
return drectangle(p.x()-width/2, p.y()-height/2, p.x()+width/2, p.y()+height/2);
}
inline drectangle centered_drect (
const drectangle& rect,
double width,
double height
)
{
return centered_drect(dcenter(rect), width, height);
}
inline const drectangle shrink_rect (
const drectangle& rect,
double num
@ -419,6 +428,21 @@ namespace dlib
return shrink_rect(rect, -width, -height);
}
inline drectangle set_aspect_ratio (
const drectangle& rect,
double ratio
)
{
DLIB_ASSERT(ratio > 0,
"\t drectangle set_aspect_ratio()"
<< "\n\t ratio: " << ratio
);
const double h = std::sqrt(rect.area()/ratio);
const double w = h*ratio;
return centered_drect(rect, w, h);
}
// ----------------------------------------------------------------------------------------
}

View File

@ -503,6 +503,18 @@ namespace dlib
- R.height() == height
!*/
// ----------------------------------------------------------------------------------------
drectangle centered_drect (
const drectangle& rect,
double width,
double height
);
/*!
ensures
- returns centered_drect(center(rect), width, height)
!*/
// ----------------------------------------------------------------------------------------
const drectangle shrink_rect (
@ -554,6 +566,24 @@ namespace dlib
(i.e. grows the given drectangle by expanding its border)
!*/
// ----------------------------------------------------------------------------------------
drectangle set_aspect_ratio (
const drectangle& rect,
double ratio
);
/*!
requires
- ratio > 0
ensures
- This function reshapes the given rectangle so that it has the given aspect
ratio. In particular, this means we return a rectangle R such that the
following equations are true:
- R.width()/R.height() == ratio
- R.area() == rect.area()
- dcenter(rect) == dcenter(R)
!*/
// ----------------------------------------------------------------------------------------
}