Added nearest_rect()

This commit is contained in:
Davis King 2016-08-28 12:09:02 -04:00
parent 0ac2197c74
commit bb60d061c4
2 changed files with 46 additions and 0 deletions

View File

@ -463,6 +463,36 @@ namespace dlib
return temp;
}
// ----------------------------------------------------------------------------------------
inline size_t nearest_rect (
const std::vector<rectangle>& rects,
const point& p
)
{
DLIB_ASSERT(rects.size() > 0);
size_t idx = 0;
double best_dist = std::numeric_limits<double>::infinity();
for (size_t i = 0; i < rects.size(); ++i)
{
if (rects[i].contains(p))
{
return i;
}
else
{
double dist = (nearest_point(rects[i],p)-p).length();
if (dist < best_dist)
{
best_dist = dist;
idx = i;
}
}
}
return idx;
}
// ----------------------------------------------------------------------------------------
template <typename T, typename U>

View File

@ -704,6 +704,22 @@ namespace dlib
- returns the point in rect that is closest to p
!*/
// ----------------------------------------------------------------------------------------
inline size_t nearest_rect (
const std::vector<rectangle>& rects,
const point& p
);
/*!
requires
- rects.size() > 0
ensures
- returns the index of the rectangle that is closest to the point p. In
particular, this function returns an IDX such that:
length(nearest_point(rects[IDX],p) - p)
is minimized.
!*/
// ----------------------------------------------------------------------------------------
inline long distance_to_rect_edge (