Board  0.9.5
examples/traversal.cpp
#include <cstdlib>
#include <ctime>
#include <iostream>
#include "Board.h"
using namespace LibBoard;
const int NbColors = 4;
const Color Colors[NbColors] = {Color("#cc0000"), Color("#00cc00"), Color("#0000cc"), Color("#c0c0c0")};
ShapeList generateGroup(int n)
{
if (!n) {
return ShapeList() << (Group() << LibBoard::rectangle(0, 0, 10, 10, Color("#404040"), Color::Null, 0.1, DashStyle));
}
if (n == 1) {
switch (rand() % 2) {
case 0:
return ShapeList() << LibBoard::circle(0, 0, 5, Colors[rand() % NbColors], Color::Null, 1.0);
case 1:
return ShapeList() << LibBoard::rectangle(0, 0, 10, 10, Colors[rand() % NbColors], Color::Null, 1.0);
}
}
int count[4] = {0, 0, 0, 0};
while (count[0] + count[1] + count[2] + count[3] != n) {
count[rand() % 4] += 1;
}
ShapeList topLeft = generateGroup(count[0]);
ShapeList topRight = generateGroup(count[1]);
ShapeList bottomLeft = generateGroup(count[2]);
ShapeList bottomRight = generateGroup(count[3]);
ShapeList top;
top << topLeft;
top.append(topRight, ShapeList::Right, ShapeList::AlignCenter, 0.0, UseLineWidth);
ShapeList bottom;
bottom << bottomLeft;
bottom.append(bottomRight, ShapeList::Right, ShapeList::AlignCenter, 0.0, UseLineWidth);
top.append(bottom, ShapeList::Bottom, ShapeList::AlignCenter, 0.0, UseLineWidth);
if (n >= 2) {
ShapeList list;
Group group;
group << top;
Rect r = top.bbox(UseLineWidth);
group << LibBoard::rectangle(r, Color::Black, Color::Null, 0.1);
list << group;
return list;
} else {
ShapeList list;
list << top;
return list;
}
}
template <typename T> ShapeList findAll(ShapeList & list)
{
ShapeList result;
try {
ShapeList::size_type n = 0;
while (true) {
result.append(list.topLevelFindLast<T>(n), Board::Right, Board::AlignCenter);
++n;
}
} catch (Exception &) {
}
return result;
}
int main(int, char *[])
{
// srand(time(0));
srand(1000);
Board board;
Style::setDefaultLineWidth(0.5);
Style::setDefaultPenColor(Color::Blue);
Style::setDefaultFillColor(Color::Null);
cross << Line(0, 0, 10, 10, Color::Green) << Line(0, 10, 10, 0, Color::Green);
// Build a random list of shapes
int n = 25;
while (n--) {
int r = rand() % 3;
switch (r) {
case 0:
board.append(LibBoard::circle(0, 0, 5.0), Board::Right, Board::AlignCenter);
break;
case 1:
board.append(LibBoard::rectangle(0, 0, 10, 10), Board::Right, Board::AlignCenter);
break;
case 2:
board.append(cross, Board::Right, Board::AlignCenter);
break;
}
}
// Extract circles, rectangles, and crosses in distinct lists
ShapeList circles = findAll<Ellipse>(board);
ShapeList rectangles = findAll<Polyline>(board);
ShapeList crosses = findAll<Group>(board);
board.append(Text(0, 0, "Everything", LibBoard::Fonts::Helvetica, 7), Board::Right, Board::AlignCenter, 10.0);
board.append(circles, Board::Bottom, Board::AlignLeft);
board.append(rectangles, Board::Bottom, Board::AlignLeft);
board.append(crosses, Board::Bottom, Board::AlignLeft);
for (Shape & s : circles) {
s.scale(1, 2);
}
board.append(circles, Board::Bottom, Board::AlignRight);
std::cout << "List of shapes at to level\n";
std::cout << "==========================\n";
for (const Shape & s : board) {
std::cout << s.name() << ", ";
}
std::cout << "\n\n";
std::cout << "Beadth-First traversal of the shapes tree\n";
std::cout << "=========================================\n";
ShapeList::BreadthFirstIterator bfi = board.breadthFirstBegin();
ShapeList::BreadthFirstIterator bfe = board.breadthFirstEnd();
while (bfi != bfe) {
std::cout << (*bfi).name() << ", ";
++bfi;
}
std::cout << "\n\n";
std::cout << "Depth-First traversal of the shapes tree\n";
std::cout << "========================================\n";
ShapeList::DepthFirstIterator dfi = board.depthFirstBegin();
ShapeList::DepthFirstIterator dfe = board.depthFirstEnd();
while (dfi != dfe) {
std::cout << (*dfi).name() << ", ";
++dfi;
}
std::cout << "\n\n";
board.append(generateGroup(50), ShapeList::Bottom, ShapeList::AlignCenter);
std::cout << "Size is " << board.size() << std::endl;
std::cout << "Board deep size is " << board.deepSize() << std::endl;
ShapeList & tree = board.last<ShapeList>();
std::size_t deepSize = tree.deepSize();
std::cout << "Tree deep size is " << deepSize << std::endl;
dfi = tree.depthFirstBegin();
dfe = tree.depthFirstEnd();
std::size_t counter = 0;
ShapeList points;
Color shade;
Path path;
while (dfi != dfe) {
Shape * r = dynamic_cast<Polyline *>(dfi.pointer());
if (!r) {
r = dynamic_cast<Ellipse *>(dfi.pointer());
}
if (r) {
shade.setRGBf(0.2f + 0.8f * (counter / (float)deepSize), 0, 0, 1.0);
points << LibBoard::circle(r->center(), 1, Color::Null, shade, 0.0);
path << r->center();
}
++dfi;
++counter;
}
std::cout << counter << " shapes visited.\n";
board << points;
board << Polyline(path, Color("#00c000"), Color::Null, 0.5, SolidStyle, RoundCap, RoundJoin);
board.saveSVG("traversal.svg");
}
LibBoard::ShapeList::depthFirstBegin
DepthFirstIterator depthFirstBegin()
depthFirstBegin
Definition: ShapeList.h:645
LibBoard::ShapeList::last
T & last(const std::size_t position=0)
Definition: ShapeList.h:501
LibBoard::ShapeList::topLevelFindLast
T & topLevelFindLast(std::size_t position=0)
Definition: ShapeList.h:518
LibBoard::Exception
Definition: Exception.h:36
LibBoard::Polyline
A polygonal line described by a series of 2D points.
Definition: Polyline.h:38
LibBoard::ShapeList::deepSize
std::size_t deepSize() const
Recursively counts the number of shapes in the list.
Definition: ShapeList.cpp:515
findAll
ShapeList findAll(ShapeList &list)
Definition: traversal.cpp:68
LibBoard::Rect
Struct representing a rectangle on the plane.
Definition: Rect.h:39
LibBoard::ShapeList::depthFirstEnd
DepthFirstIterator depthFirstEnd()
depthFirstEnd
Definition: ShapeList.h:650
LibBoard::Text
A piece of text.
Definition: Text.h:40
LibBoard::ShapeList::BreadthFirstIterator
The BreadthFirstIterator struct allows to traverse the shape tree using a breadth-first strategy.
Definition: ShapeList.h:388
LibBoard::Line
A line between two points.
Definition: Line.h:38
LibBoard::Shape
Abstract structure for a 2D shape.
Definition: Shape.h:63
LibBoard::cross
Group cross(Point p, const Style &style=Style::defaultStyle())
Definition: Board.cpp:968
LibBoard::Path
A path, according to Postscript and SVG definition.
Definition: Path.h:45
LibBoard::Ellipse
An ellipse.
Definition: Ellipse.h:38
LibBoard::Group
A group of shapes. A group is basically a ShapeList except that when rendered in either an SVG of a F...
Definition: Group.h:40
LibBoard::ShapeList
A group of shapes.
Definition: ShapeList.h:46
LibBoard::ShapeList::scale
ShapeList & scale(double sx, double sy) override
Definition: ShapeList.cpp:344
LibBoard::Shape::center
virtual Point center(LineWidthFlag lineWidthFlag=IgnoreLineWidth) const
Definition: Shape.cpp:59
Board.h
Declaration of the Board class.
LibBoard::ShapeList::DepthFirstIterator
The DepthFirstIterator struct allows to traverse the shape tree using a depth-first strategy.
Definition: ShapeList.h:366
LibBoard::Board
Class for EPS, FIG or SVG drawings.
Definition: Board.h:62
LibBoard::ShapeList::name
const std::string & name() const override
Definition: ShapeList.cpp:49
LibBoard::ShapeList::append
ShapeList & append(const Shape &shape, Direction direction=ShapeList::Right, Alignment alignment=ShapeList::AlignCenter, double margin=0.0, LineWidthFlag lineWidthFlag=UseLineWidth)
Definition: ShapeList.cpp:246
LibBoard::Color
Structure representing an RGB triple.
Definition: Color.h:43