From 8f5c5a3c0c1a66f51e1200c6c116d8db34d9810a Mon Sep 17 00:00:00 2001 From: Davis King Date: Sat, 14 Feb 2015 19:11:09 -0500 Subject: [PATCH] Refined drectangle a little. --- dlib/geometry/drectangle.h | 6 ++++-- dlib/geometry/drectangle_abstract.h | 2 +- dlib/test/geometry.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/dlib/geometry/drectangle.h b/dlib/geometry/drectangle.h index c73086e2f..76f914b27 100644 --- a/dlib/geometry/drectangle.h +++ b/dlib/geometry/drectangle.h @@ -95,7 +95,8 @@ namespace dlib double width ( ) const { - if (is_empty()) + // if either the width or height would be 0. + if (t >= b || l >= r) return 0; else return r - l; @@ -104,7 +105,8 @@ namespace dlib double height ( ) const { - if (is_empty()) + // if either the width or height would be 0. + if (t >= b || l >= r) return 0; else return b - t; diff --git a/dlib/geometry/drectangle_abstract.h b/dlib/geometry/drectangle_abstract.h index 29fbd2a02..25a16e85a 100644 --- a/dlib/geometry/drectangle_abstract.h +++ b/dlib/geometry/drectangle_abstract.h @@ -95,7 +95,7 @@ namespace dlib - top() == rect.top()-0.5 - right() == rect.right()+0.5 - bottom() == rect.bottom()+0.5 - - center(*this) == center(rect) + - dcenter(*this) == dcenter(rect) - width() == rect.width() - height() == rect.height() !*/ diff --git a/dlib/test/geometry.cpp b/dlib/test/geometry.cpp index 2e6baf05b..a23df2d4f 100644 --- a/dlib/test/geometry.cpp +++ b/dlib/test/geometry.cpp @@ -772,6 +772,29 @@ namespace } } + +// ---------------------------------------------------------------------------------------- + + void test_rect_to_drect() + { + print_spinner(); + dlib::rand rnd; + for (int i = 0; i < 5000; ++i) + { + rectangle rect = centered_rect(rnd.get_random_32bit_number()%100, + rnd.get_random_32bit_number()%100, + rnd.get_random_32bit_number()%100, + rnd.get_random_32bit_number()%100); + + drectangle drect = rect; + rectangle rect2 = drect; + DLIB_TEST(rect2 == rect); + DLIB_TEST(rect.width() == drect.width()); + DLIB_TEST(rect.height() == drect.height()); + DLIB_TEST(dcenter(rect) == dcenter(drect)); + } + } + // ---------------------------------------------------------------------------------------- class geometry_tester : public tester @@ -786,6 +809,7 @@ namespace void perform_test ( ) { + test_rect_to_drect(); geometry_test(); test_border_enumerator(); test_find_affine_transform();