Commit 0e1d8756 authored by Davis King's avatar Davis King

Added a function for computing the center of a rectangle.

parent 20a79481
......@@ -353,6 +353,24 @@ namespace dlib
return result;
}
// ----------------------------------------------------------------------------------------
point center (
const dlib::rectangle& rect
)
{
point temp(rect.left() + rect.right() + 1,
rect.top() + rect.bottom() + 1);
if (temp.x() < 0)
temp.x() -= 1;
if (temp.y() < 0)
temp.y() -= 1;
return temp/2;
}
// ----------------------------------------------------------------------------------------
inline long distance_to_rect_edge (
......
......@@ -402,6 +402,16 @@ namespace dlib
The data in the input stream should be of the form [(left, top) (right, bottom)]
!*/
// ----------------------------------------------------------------------------------------
point center (
const dlib::rectangle& rect
);
/*!
ensures
- returns the center of the given rectangle
!*/
// ----------------------------------------------------------------------------------------
inline const rectangle centered_rect (
......@@ -412,6 +422,7 @@ namespace dlib
/*!
ensures
- returns a rectangle R such that:
- center(R) == p
- if (width == 0 || height == 0)
- R.width() == 0
- R.height() == 0
......@@ -419,7 +430,6 @@ namespace dlib
- R.width() == width
- R.height() == height
- R.tl_corner() == point(p.x()-width/2, p.y()-height/2)
- The center of R is a the point p
!*/
// ----------------------------------------------------------------------------------------
......@@ -433,6 +443,7 @@ namespace dlib
/*!
ensures
- returns a rectangle R such that:
- center(R) == p
- if (width == 0 || height == 0)
- R.width() == 0
- R.height() == 0
......@@ -440,7 +451,6 @@ namespace dlib
- R.width() == width
- R.height() == height
- R.tl_corner() == point(x-width/2, y-height/2)
- The center of R is a the point (x,y)
!*/
// ----------------------------------------------------------------------------------------
......
......@@ -349,6 +349,21 @@ namespace
DLIB_TEST(rectangle() + point(5,4) + point(10,10) == rectangle(5,4,10,10));
// make sure the center of a centered rectangle is always right
for (long x = -10; x <= 10; ++x)
{
for (long y = -10; y <= 10; ++y)
{
for (long w = 0; w < 10; ++w)
{
for (long h = 0; h < 10; ++h)
{
DLIB_TEST(center(centered_rect(x,y,w,h)) == point(x,y));
}
}
}
}
}
......
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