homog2d library
Typedefs | Functions | Variables
showcase10.cpp File Reference

Sierpinski triangle, recursively. More...

#include "../../homog2d.hpp"
Include dependency graph for showcase10.cpp:

Typedefs

using DrawType = std::pair< CPolyline, img::Color >
 

Functions

int main ()
 
template<typename IM >
void recurseTriangles (std::vector< CPolyline > &v_pl, std::vector< DrawType > &v_all, int max_depth, Homogr &H, img::Image< IM > &im)
 Each polyline in the input vector v_pl will be replaced by 3. More...
 

Variables

auto max_color = 200.
 

Detailed Description

Sierpinski triangle, recursively.

Typedef Documentation

◆ DrawType

using DrawType = std::pair<CPolyline,img::Color>

Function Documentation

◆ main()

int main ( )
64 {
65  Homogr H;
66  std::srand( std::time(nullptr) ); // to get random colors
67  auto x0 = 10;
68  auto y0 = 10;
69  auto l = 500;
70  auto max_depth = 10;
71  std::vector<Point2d> vpts{
72  {x0,y0}, {x0+l,y0}, {x0+l/2.,y0+l/2.*sqrt(3.)}
73  };
74  CPolyline pl(vpts); // build initial equilateral triangle
75  std::vector<CPolyline> v_pl{pl};
76  std::vector<DrawType> v_draw(1);
77  v_draw[0] = std::make_pair( pl, img::Color(250,0,20) );
78 
79  img::Image<img::SvgImage> im( 600, 600 );
80  pl.draw( im, img::DrawParams().setColor(250,0,20) );
81  im.write( "showcase10_00.svg" );
82 
83  recurseTriangles( v_pl, v_draw, max_depth, H, im );
84 }
85 
A 2D homography, defining a planar transformation.
Definition: homog2d.hpp:369
Draw parameters, independent of back-end library.
Definition: homog2d.hpp:514
Color type , see DrawParams.
Definition: homog2d.hpp:432
img::Image< img::SvgImage > im(300, 400)
void recurseTriangles(std::vector< CPolyline > &v_pl, std::vector< DrawType > &v_all, int max_depth, Homogr &H, img::Image< IM > &im)
Each polyline in the input vector v_pl will be replaced by 3.
Definition: showcase10.cpp:16
Opaque data structure, will hold the image type, depending on back-end library. This type is the one ...
Definition: homog2d.hpp:712
Polyline, will be instanciated either as OPolyline_ (open polyline) or CPolyline_.
Definition: homog2d.hpp:364
Here is the call graph for this function:

◆ recurseTriangles()

template<typename IM >
void recurseTriangles ( std::vector< CPolyline > &  v_pl,
std::vector< DrawType > &  v_all,
int  max_depth,
Homogr H,
img::Image< IM > &  im 
)

Each polyline in the input vector v_pl will be replaced by 3.

Parameters
v_plthe set of triangles that will be split
v_allwhat we actually draw
imoutput image
23 {
24  static int depth;
25  depth++;
26  H.addTranslation(-50,-50).addRotation( 5.*M_PI/180. ).addTranslation(50,50).addScale(1.1,1.1);
27  std::cout << "depth=" << depth << " nb pl input=" << v_pl.size() << '\n';
28  if( depth == max_depth )
29  return;
30  std::vector<CPolyline> v_pl_new;
31  int r = std::rand()*max_color/RAND_MAX;
32  int g = std::rand()*max_color/RAND_MAX;
33  int b = std::rand()*max_color/RAND_MAX;
34  for( const auto& pl: v_pl ) {
35  auto v_segs = pl.getSegs();
36  auto v_mid = getCenters( v_segs );
37  auto v_pts = pl.getPts();
38  auto pl1 = CPolyline( std::vector<Point2d>{ v_pts[0], v_mid[0], v_mid[2] } );
39  auto pl2 = CPolyline( std::vector<Point2d>{ v_pts[1], v_mid[1], v_mid[0] } );
40  auto pl3 = CPolyline( std::vector<Point2d>{ v_pts[2], v_mid[2], v_mid[1] } );
41 
42  auto pld = CPolyline( v_mid );
43  v_all.push_back( std::make_pair(pld,img::Color(r,g,b) ) );
44 
45  v_pl_new.push_back(pl1);
46  v_pl_new.push_back(pl2);
47  v_pl_new.push_back(pl3);
48  }
49  im.clear();
50  for( const auto& e: v_all )
51  {
52  auto e2 = H*e.first;
53  draw( im, e2, img::DrawParams().setColor( e.second ) );
54  }
55 
56  std::ostringstream oss;
57  oss << "showcase10_" << std::setfill('0') << std::setw(2) << depth << ".svg";
58  im.write( oss.str() );
59 
60  recurseTriangles( v_pl_new, v_all, max_depth, H, im );
61 }
62 
void draw(Data &data)
Definition: precision_test_opencv.cpp:184
auto getCenters(const T &vsegs)
Free function, returns middle points of set of segments/circles.
Definition: homog2d.hpp:10743
#define M_PI
Definition: homog2d.hpp:235
auto max_color
Definition: showcase10.cpp:12
Draw parameters, independent of back-end library.
Definition: homog2d.hpp:514
void write(std::string) const
Definition: homog2d.hpp:752
Color type , see DrawParams.
Definition: homog2d.hpp:432
Hmatrix_ & addScale(T k)
Adds the same scale factor to the matrix.
Definition: homog2d.hpp:1878
CPolyline_< HOMOG2D_INUMTYPE > CPolyline
Default polyline type.
Definition: homog2d.hpp:12398
Hmatrix_ & addTranslation(T tx, T ty)
Adds a translation tx,ty to the matrix.
Definition: homog2d.hpp:1830
void clear(Color c=Color(255, 255, 255))
Definition: homog2d.hpp:759
Hmatrix_ & addRotation(T theta)
Adds a rotation with an angle theta (radians) to the matrix.
Definition: homog2d.hpp:1854
void recurseTriangles(std::vector< CPolyline > &v_pl, std::vector< DrawType > &v_all, int max_depth, Homogr &H, img::Image< IM > &im)
Each polyline in the input vector v_pl will be replaced by 3.
Definition: showcase10.cpp:16
size_t size() const
Definition: homog2d.hpp:1375
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ max_color

auto max_color = 200.