bezier: add region query

This commit is contained in:
Danny Robson 2015-02-03 12:53:41 +11:00
parent e5b85b559b
commit 3d53ee5eb1
3 changed files with 77 additions and 0 deletions

View File

@ -359,6 +359,36 @@ namespace util {
}
//-----------------------------------------------------------------------------
template <size_t S>
util::region2f
util::bezier<S>::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 <size_t S>
util::point2f&

View File

@ -21,6 +21,7 @@
#define __UTIL_BEZIER_HPP
#include "point.hpp"
#include "region.hpp"
#include <iostream>
@ -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;

View File

@ -8,6 +8,7 @@
//-----------------------------------------------------------------------------
template <size_t> void test_eval (void);
template <size_t> void test_intersect (void);
template <size_t> 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;
}