diff --git a/docs/homog2d_history.md b/docs/homog2d_history.md index 20f5878..d0268f2 100644 --- a/docs/homog2d_history.md +++ b/docs/homog2d_history.md @@ -15,6 +15,7 @@ See [Release page](https://github.com/skramm/homog2d/releases). - add `Vector` type (defined by dx,dy) - current master branch + - added building a CPolyline as a Regular Convex Polygon (RCP), either through constructor or through `set()` function - added helper function `genRandomColors()` - bufixes, fixed enclosing of Polyline objects (https://github.com/skramm/homog2d/issues/10) - added `dsize()` member and free function, [see here](homog2d_manual.md#numtype) diff --git a/homog2d.hpp b/homog2d.hpp index 43172b3..b958f79 100644 --- a/homog2d.hpp +++ b/homog2d.hpp @@ -5495,18 +5495,20 @@ this should work !!! (but doesn't...) private: template - void + HOMOG2D_INUMTYPE impl_constr_RCP( FPT2 rad, size_t n, const detail::PlHelper& ); template - constexpr void + constexpr HOMOG2D_INUMTYPE impl_constr_RCP( FPT2 rad, size_t n, const detail::PlHelper& ) { static_assert( detail::AlwaysFalse::value, "cannot build an regular convex polygon for a OPolyline object"); + return 0.; // to avoid a compiler warning } template - void imp_constrFRect( const FRect_& rect, const detail::PlHelper& ) + void + imp_constrFRect( const FRect_& rect, const detail::PlHelper& ) { for( const auto& pt: rect.get4Pts() ) _plinevec.push_back( pt ); @@ -5771,7 +5773,7 @@ at 180° of the previous one. *it++ = pt; // allow type conversions (std::copy implies same type) } -/// Build a parallelogram from 3 points +/// Build a parallelogram (4 points) from 3 points template void setParallelogram( const Point2d_& pt1, @@ -5789,6 +5791,13 @@ at 180° of the previous one. impl_setFromFRect( rec, detail::PlHelper() ); } +/// Build RCP (Regular Convex Polygon), and return distance between consecutive points + template + HOMOG2D_INUMTYPE set( FPT2 rad, size_t n ) + { + return impl_constr_RCP( rad, n, detail::PlHelper() ); + } + ///@} private: @@ -6064,7 +6073,7 @@ Two tasks: /// \todo handle sin() and cos() to support ttmath template template -void +HOMOG2D_INUMTYPE PolylineBase::impl_constr_RCP( FPT2 rad, size_t n, const detail::PlHelper& ) { if( n < 3 ) @@ -6075,15 +6084,26 @@ PolylineBase::impl_constr_RCP( FPT2 rad, size_t n, const detail::PlHelp std::vector> v_pts(n); auto it = std::begin( v_pts ); auto angle0 = (HOMOG2D_INUMTYPE)2. * M_PI / n; +// Point2d_ pt( rad, 0. ); // initial point + for( size_t i=0; i( x * rad, y * rad ); + std::cout << pt.distTo( pt2 ) << "\n"; + pt = pt2; + } +#endif *it = Point2d_( x * rad, y * rad ); it++; } *this = PolylineBase( v_pts ); + return getPoint(0).distTo( getPoint(1) ); } } // namespace base diff --git a/misc/homog2d.cbp b/misc/homog2d.cbp index f819dca..aa2dfd8 100644 --- a/misc/homog2d.cbp +++ b/misc/homog2d.cbp @@ -165,6 +165,7 @@ + diff --git a/misc/homog2d_test.cpp b/misc/homog2d_test.cpp index f67419e..f99ae53 100644 --- a/misc/homog2d_test.cpp +++ b/misc/homog2d_test.cpp @@ -3159,6 +3159,26 @@ TEST_CASE( "Polyline", "[polyline]" ) } } +TEST_CASE( "Polyline RCP (Regular Convex Polygon)", "[polyline-RCP]" ) +{ + CHECK_THROWS( CPolyline( 5, -5 ) ); + CHECK_THROWS( CPolyline( -5, 5 ) ); + CHECK_THROWS( CPolyline( 1, 2 ) ); + CHECK_THROWS( CPolyline( 0, 5 ) ); + + CPolyline pol( 5, 5 ); + CHECK( pol.size() == 5 ); + CHECK( pol.nbSegs() == 5 ); + pol.set( 8, 4 ); + CHECK( pol.size() == 4 ); + CHECK( pol.nbSegs() == 4 ); + + CHECK_THROWS( pol.set( 1, 2 ) ); + CHECK_THROWS( pol.set( 0, 5 ) ); + CHECK_THROWS( pol.set( -5, 5 ) ); + CHECK_THROWS( pol.set( 5, -5 ) ); +} + TEST_CASE( "Polyline setParallelogram", "[polyline-setParallelogram]" ) { {