Skip to content

Commit

Permalink
Polygon RCP, added return distance
Browse files Browse the repository at this point in the history
  • Loading branch information
skramm committed Dec 14, 2023
1 parent 450537c commit b29d9aa
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/homog2d_history.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 25 additions & 5 deletions homog2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5495,18 +5495,20 @@ this should work !!! (but doesn't...)

private:
template<typename FPT2>
void
HOMOG2D_INUMTYPE
impl_constr_RCP( FPT2 rad, size_t n, const detail::PlHelper<type::IsClosed>& );

template<typename FPT2>
constexpr void
constexpr HOMOG2D_INUMTYPE
impl_constr_RCP( FPT2 rad, size_t n, const detail::PlHelper<type::IsOpen>& )
{
static_assert( detail::AlwaysFalse<PLT>::value, "cannot build an regular convex polygon for a OPolyline object");
return 0.; // to avoid a compiler warning
}

template<typename FPT2>
void imp_constrFRect( const FRect_<FPT2>& rect, const detail::PlHelper<type::IsClosed>& )
void
imp_constrFRect( const FRect_<FPT2>& rect, const detail::PlHelper<type::IsClosed>& )
{
for( const auto& pt: rect.get4Pts() )
_plinevec.push_back( pt );
Expand Down Expand Up @@ -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<typename FPT1,typename FPT2,typename FPT3>
void setParallelogram(
const Point2d_<FPT1>& pt1,
Expand All @@ -5789,6 +5791,13 @@ at 180° of the previous one.
impl_setFromFRect( rec, detail::PlHelper<PLT>() );
}

/// Build RCP (Regular Convex Polygon), and return distance between consecutive points
template<typename FPT2>
HOMOG2D_INUMTYPE set( FPT2 rad, size_t n )
{
return impl_constr_RCP( rad, n, detail::PlHelper<PLT>() );
}

///@}

private:
Expand Down Expand Up @@ -6064,7 +6073,7 @@ Two tasks:
/// \todo handle sin() and cos() to support ttmath
template<typename PLT,typename FPT>
template<typename FPT2>
void
HOMOG2D_INUMTYPE
PolylineBase<PLT,FPT>::impl_constr_RCP( FPT2 rad, size_t n, const detail::PlHelper<type::IsClosed>& )
{
if( n < 3 )
Expand All @@ -6075,15 +6084,26 @@ PolylineBase<PLT,FPT>::impl_constr_RCP( FPT2 rad, size_t n, const detail::PlHelp
std::vector<Point2d_<HOMOG2D_INUMTYPE>> v_pts(n);
auto it = std::begin( v_pts );
auto angle0 = (HOMOG2D_INUMTYPE)2. * M_PI / n;
// Point2d_<HOMOG2D_INUMTYPE> pt( rad, 0. ); // initial point

for( size_t i=0; i<n; i++ )
{
auto angle = angle0 * i;
auto x = std::cos( angle );
auto y = std::sin( angle );
#if 0
if( i == 1 )
{
auto pt2 = Point2d_<HOMOG2D_INUMTYPE>( x * rad, y * rad );
std::cout << pt.distTo( pt2 ) << "\n";
pt = pt2;
}
#endif
*it = Point2d_<HOMOG2D_INUMTYPE>( x * rad, y * rad );
it++;
}
*this = PolylineBase<PLT,FPT>( v_pts );
return getPoint(0).distTo( getPoint(1) );
}

} // namespace base
Expand Down
1 change: 1 addition & 0 deletions misc/homog2d.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
<Unit filename="no_build/no_build_15.cxx" />
<Unit filename="no_build/no_build_16.cxx" />
<Unit filename="no_build/no_build_17.cxx" />
<Unit filename="no_build/no_build_18.cxx" />
<Unit filename="no_build/readme.txt" />
<Unit filename="other/test_svg_import_1.svg" />
<Unit filename="other/test_svg_import_2.svg" />
Expand Down
20 changes: 20 additions & 0 deletions misc/homog2d_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]" )
{
{
Expand Down

0 comments on commit b29d9aa

Please sign in to comment.