Board 0.9.6
examples/traversal.cpp
#include <Board.h>
#include <ctime>
#include <iostream>
using namespace LibBoard;
const int NbColors = 4;
const Color Colors[NbColors] = {Color("#cc0000"), Color("#00cc00"), Color("#0000cc"), Color("#c0c0c0")};
{
if (!n) {
return ShapeList() << (Group() << LibBoard::rectangle(0, 0, 10, 10, Color("#404040"), Color::Null, 0.1, DashStyle));
}
if (n == 1) {
switch (Tools::boardRand() % 2) {
case 0:
return ShapeList() << circle(0, 0, 5, Colors[Tools::boardRand() % NbColors], Color::Null, 1.0);
case 1:
return ShapeList() << rectangle(0, 0, 10, 10, Colors[Tools::boardRand() % NbColors], Color::Null, 1.0);
}
}
int count[4] = {0, 0, 0, 0};
while (count[0] + count[1] + count[2] + count[3] != n) {
count[Tools::boardRand() % 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, Direction::Right, Alignment::Center, 0.0, UseLineWidth);
ShapeList bottom;
bottom << bottomLeft;
bottom.append(bottomRight, Direction::Right, Alignment::Center, 0.0, UseLineWidth);
top.append(bottom, Direction::Bottom, Alignment::Center, 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 {
while (true) {
result.append(list.topLevelFindLast<T>(n), Direction::Right, Alignment::Center);
++n;
}
} catch (Exception &) {
}
return result;
}
int main(int, char *[])
{
// srand(time(0));
Tools::initBoardRand(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 = Tools::boardRand() % 3;
switch (r) {
case 0:
board.append(circle(0, 0, 5.0), Direction::Right, Alignment::Center);
break;
case 1:
board.append(rectangle(0, 0, 10, 10), Direction::Right, Alignment::Center);
break;
case 2:
board.append(cross, Direction::Right, Alignment::Center);
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), Direction::Right, Alignment::Center, 10.0);
board.append(circles, Direction::Bottom, Alignment::Left);
board.append(rectangles, Direction::Bottom, Alignment::Left);
board.append(crosses, Direction::Bottom, Alignment::Left);
for (Shape & s : circles) {
s.scale(1, 2);
}
board.append(circles, Direction::Bottom, Alignment::Right);
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";
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";
while (dfi != dfe) {
std::cout << (*dfi).name() << ", ";
++dfi;
}
std::cout << "\n\n";
board.append(generateGroup(50), Direction::Bottom, Alignment::Center);
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");
}
Declaration of the Board class.
int main(int argc, char *argv[])
Definition arithmetic.cpp:16
Structure representing an RGB triple.
Definition Color.h:43
Color & setRGBf(float red, float green, float blue, float alpha=1.0)
Definition Color.cpp:71
Definition Exception.h:36
@ Helvetica
Definition PSFonts.h:68
Definition Board.h:55
Ellipse circle(double x, double y, double radius, Color penColor=Style::defaultPenColor(), Color fillColor=Style::defaultFillColor(), double lineWidth=Style::defaultLineWidth(), const LineStyle lineStyle=Style::defaultLineStyle())
Definition Ellipse.cpp:371
Polyline rectangle(double left, double top, double width, double height, Color penColor=Style::defaultPenColor(), Color fillColor=Style::defaultFillColor(), double lineWidth=Style::defaultLineWidth(), const LineStyle lineStyle=Style::defaultLineStyle(), const LineCap cap=Style::defaultLineCap(), const LineJoin join=Style::defaultLineJoin())
Definition Polyline.cpp:569
Group cross(Point p, const Style &style=Style::defaultStyle())
Definition Board.cpp:1024
Class for EPS, FIG or SVG drawings.
Definition Board.h:61
void saveSVG(const char *filename, PageSize size=PageSize::BoundingBox, double margin=0.0, Unit unit=Unit::Millimeter) const
Definition Board.cpp:765
An ellipse.
Definition Ellipse.h:38
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
A line between two points.
Definition Line.h:38
A path, according to Postscript and SVG definition.
Definition Path.h:45
A polygonal line described by a series of 2D points.
Definition Polyline.h:38
Struct representing a rectangle on the plane.
Definition Rect.h:40
The BreadthFirstIterator struct allows to traverse the shape tree using a breadth-first strategy.
Definition ShapeList.h:392
The DepthFirstIterator struct allows to traverse the shape tree using a depth-first strategy.
Definition ShapeList.h:370
Shape * pointer()
Definition ShapeList.h:693
A group of shapes.
Definition ShapeList.h:47
BreadthFirstIterator breadthFirstBegin()
breadthFirstBegin
Definition ShapeList.h:659
BreadthFirstIterator breadthFirstEnd()
breadthFirstEnd
Definition ShapeList.h:664
const std::string & name() const override
Definition ShapeList.cpp:49
DepthFirstIterator depthFirstBegin()
depthFirstBegin
Definition ShapeList.h:649
ShapeList & append(const Shape &shape, Direction direction=Direction::Right, Alignment alignment=Alignment::Center, double margin=0.0, LineWidthFlag lineWidthFlag=UseLineWidth)
Definition ShapeList.cpp:232
std::size_t size() const
The number of shapes in the list (at top level).
Definition ShapeList.h:669
std::size_t deepSize() const
Recursively counts the number of shapes in the list.
Definition ShapeList.cpp:501
DepthFirstIterator depthFirstEnd()
depthFirstEnd
Definition ShapeList.h:654
std::vector< Shape * >::size_type size_type
Definition ShapeList.h:49
ShapeList & scale(double sx, double sy) override
Definition ShapeList.cpp:330
T & topLevelFindLast(std::size_t position=0)
Definition ShapeList.h:522
T & last(const std::size_t position=0)
Definition ShapeList.h:505
Abstract structure for a 2D shape.
Definition Shape.h:64
virtual Point center(LineWidthFlag lineWidthFlag=IgnoreLineWidth) const
Definition Shape.cpp:59
A piece of text.
Definition Text.h:40
const Color Colors[NbColors]
Definition traversal.cpp:18
ShapeList generateGroup(int n)
Definition traversal.cpp:20
ShapeList findAll(ShapeList &list)
Definition traversal.cpp:67
const int NbColors
Definition traversal.cpp:17