From 3d53ee5eb1cc5ed1dfb25de660c4ce7490c97823 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 3 Feb 2015 12:53:41 +1100 Subject: [PATCH] bezier: add region query --- bezier.cpp | 30 ++++++++++++++++++++++++++++++ bezier.hpp | 3 +++ test/bezier.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/bezier.cpp b/bezier.cpp index 5e85f344..510ce872 100644 --- a/bezier.cpp +++ b/bezier.cpp @@ -359,6 +359,36 @@ namespace util { } +//----------------------------------------------------------------------------- +template +util::region2f +util::bezier::region (void) const +{ + util::region2f r; + + float x0 = m_points[0].x; + float y0 = m_points[0].y; + + float x1 = x0; + float y1 = y0; + + for (size_t i = 1; i < S+1; ++i) { + x0 = min (x0, m_points[i].x); + y0 = min (y0, m_points[i].y); + + x1 = max (x1, m_points[i].x); + y1 = max (y1, m_points[i].y); + } + + return { + x0, + y0, + x1 - x0, + y1 - y0 + }; +} + + //----------------------------------------------------------------------------- template util::point2f& diff --git a/bezier.hpp b/bezier.hpp index 287a9212..24f073d6 100644 --- a/bezier.hpp +++ b/bezier.hpp @@ -21,6 +21,7 @@ #define __UTIL_BEZIER_HPP #include "point.hpp" +#include "region.hpp" #include @@ -40,6 +41,8 @@ namespace util { float distance (point2f) const; + region2f region (void) const; + point2f& operator[] (size_t idx); const point2f& operator[] (size_t idx) const; diff --git a/test/bezier.cpp b/test/bezier.cpp index d38af386..0d304c6a 100644 --- a/test/bezier.cpp +++ b/test/bezier.cpp @@ -8,6 +8,7 @@ //----------------------------------------------------------------------------- template void test_eval (void); template void test_intersect (void); +template void test_region (void); //----------------------------------------------------------------------------- @@ -161,6 +162,45 @@ test_intersect<3> (void) } +//----------------------------------------------------------------------------- +template <> +void +test_region<1> (void) +{ + static const util::bezier<1> upright ({{0.f, 0.f}, {100.f, 100.f}}); + static const util::bezier<1> downleft ({{100.f, 100.f}, {0.f, 0.f}}); + static const util::bezier<1> vertical ({{0.f, 0.f}, {0.f, 100.f}}); + static const util::bezier<1> horizontal ({{0.f, 0.f}, {100.f, 0.f}}); + + CHECK_EQ (upright.region (), util::region2f (0.f, 0.f, 100.f, 100.f)); + CHECK_EQ (downleft.region (), util::region2f (0.f, 0.f, 100.f, 100.f)); + CHECK_EQ (vertical.region (), util::region2f (0.f, 0.f, 0.f, 100.f)); + CHECK_EQ (horizontal.region (), util::region2f (0.f, 0.f, 100.f, 0.f)); +} + + +//----------------------------------------------------------------------------- +template <> +void +test_region<2> (void) +{ + static const util::bezier<2> upright({{0.f, 0.f}, {50.f, 50.f}, {100.f, 100.f}}); + + CHECK_EQ (upright.region (), util::region2f (0.0f, 0.f, 100.f, 100.0f)); +} + + +//----------------------------------------------------------------------------- +template <> +void +test_region<3> (void) +{ + static const util::bezier<3> upright({{0.f, 0.f}, {33.f, 33.f}, {67.f, 67.f}, {100.f, 100.f}}); + + CHECK_EQ (upright.region (), util::region2f (0.0f, 0.f, 100.f, 100.0f)); +} + + //----------------------------------------------------------------------------- int main (int, char**) @@ -173,5 +213,9 @@ main (int, char**) test_intersect<2> (); test_intersect<3> (); + test_region<1> (); + test_region<2> (); + test_region<3> (); + return EXIT_SUCCESS; }