Commit bb60d061 authored by Davis King's avatar Davis King

Added nearest_rect()

parent 0ac2197c
......@@ -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>
......
......@@ -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 (
......
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