Board  0.9.5
examples/stroke_path.cpp
#include <vector>
#include "Board.h"
using namespace LibBoard;
ShapeList strikeOut(const Path & path, double strokeWidth, LineCap lineCap, LineJoin lineJoin, double miterLimit = 4.0)
{
const double s = 1.0;
ShapeList list;
std::vector<Point> e = Tools::pathBoundaryPoints(path, strokeWidth, lineCap, lineJoin, miterLimit);
size_t limit = (path.isClosed() ? path.size() : (path.size() - 1));
for (size_t i = 0; i < limit; ++i) {
Point a = path[i];
Point b = path[(i + 1) % path.size()];
Point v = b - a;
Point shift = 0.5 * strokeWidth * v.rotatedPI2().normalise();
list << Line(a + shift, b + shift, Color::Black, strokeWidth * 0.01).scale(s);
list << Line(a - shift, b - shift, Color::Black, strokeWidth * 0.01).scale(s);
list << Line(a, b, Color::Red, strokeWidth * 0.01);
if ((lineCap == RoundCap) || (lineJoin == RoundJoin)) {
list << LibBoard::circle(a, strokeWidth * 0.5, Color::Green, Color::Null, strokeWidth * 0.01);
}
}
if ((!path.isClosed() && lineCap == RoundCap) || (lineJoin == RoundJoin)) {
Point a = path[path.size() - 1];
list << LibBoard::circle(a, strokeWidth * 0.5, Color::Green, Color::Null, strokeWidth * 0.01);
}
for (size_t i = 0; i < e.size(); ++i) {
Point c = e[i];
list << LibBoard::circle(c, 10.0, Color::Blue, Color::Null, strokeWidth * 0.01);
list << Line(c - Point(10.0, 0.0), c + Point(10.0, 0), Color::Blue, strokeWidth * 0.01);
list << Line(c - Point(0.0, 10.0), c + Point(0, 10.0), Color::Blue, strokeWidth * 0.01);
}
return list;
}
int main(int, char *[])
{
Board board;
board.clear(Color::White);
// http://www.w3.org/TR/SVG/painting.html#StrokeProperties
Point p1(30, 366);
Point p2(200, 400);
Point p3(0, 0);
Point p4(-100, 20);
Point p5(-50, -100);
Path pA(Path::ClosedPath);
pA << p1 << p2 << p3 << p4 << p5;
Path pB(Path::ClosedPath);
pB << Point(0, 0) << Point(180, 0) << Point(180, 300) << Point(0, 300);
Path pC(Path::ClosedPath);
pC << Point(0, 0) << Point(200, 0) << Point(500, 5) << Point(150, 200);
Path losange;
losange << Point(0, 0) << Point(300, -500) << Point(600, 0) << Point(300, 500);
l.append(strikeOut(pA, 50.0, RoundCap, MiterJoin, 3.0), ShapeList::Right, ShapeList::AlignCenter, 30.0);
l.append(strikeOut(pB, 50.0, RoundCap, BevelJoin, 3.0), ShapeList::Right, ShapeList::AlignCenter, 30.0);
l.append(strikeOut(pC, 50.0, RoundCap, MiterJoin, 3.0), ShapeList::Right, ShapeList::AlignCenter, 30.0);
l.append(strikeOut(pA, 50.0, RoundCap, RoundJoin, 3.0), ShapeList::Right, ShapeList::AlignCenter, 30.0);
l.append(Text(0, 0, "Round cap, miter/bevel/miter/round join", Fonts::Courier, 14.0), ShapeList::Right, ShapeList::AlignCenter);
board.append(l, ShapeList::Bottom, ShapeList::AlignLeft);
l.clear();
l.append(strikeOut(pA, 50.0, ButtCap, MiterJoin, 3.0), ShapeList::Right, ShapeList::AlignCenter, 30.0);
l.append(strikeOut(pB, 50.0, ButtCap, BevelJoin, 3.0), ShapeList::Right, ShapeList::AlignCenter, 30.0);
l.append(strikeOut(pC, 50.0, ButtCap, MiterJoin, 3.0), ShapeList::Right, ShapeList::AlignCenter, 30.0);
l.append(strikeOut(pA, 50.0, ButtCap, RoundJoin, 3.0), ShapeList::Right, ShapeList::AlignCenter, 30.0);
l.append(Text(0, 0, "Butt cap, miter/bevel/miter/round join", Fonts::Courier, 14.0), ShapeList::Right, ShapeList::AlignCenter);
board.append(l, ShapeList::Bottom, ShapeList::AlignLeft);
l.clear();
l.append(strikeOut(pA, 50.0, SquareCap, MiterJoin, 3.0), ShapeList::Right, ShapeList::AlignCenter, 30.0);
l.append(strikeOut(pB, 50.0, SquareCap, BevelJoin, 3.0), ShapeList::Right, ShapeList::AlignCenter, 30.0);
l.append(strikeOut(pC, 50.0, SquareCap, MiterJoin, 3.0), ShapeList::Right, ShapeList::AlignCenter, 30.0);
l.append(strikeOut(pA, 50.0, SquareCap, RoundJoin, 3.0), ShapeList::Right, ShapeList::AlignCenter, 30.0);
l.append(Text(0, 0, "Square cap, miter/bevel/miter/round join", Fonts::Courier, 14.0), ShapeList::Right, ShapeList::AlignCenter);
board.append(l, ShapeList::Bottom, ShapeList::AlignLeft);
ShapeList list;
Polyline poly(Path::ClosedPath, Color::Blue, Color::Null, 1.0, SolidStyle, ButtCap, MiterJoin);
poly << Point(0, 0) << Point(30, -50) << Point(60, 0) << Point(30, 50);
list.append(poly, ShapeList::Right, ShapeList::AlignCenter);
list.append(poly, ShapeList::Right, ShapeList::AlignCenter);
board.append(list, ShapeList::Right, ShapeList::AlignCenter);
board.append(LibBoard::rectangle(0, 0, 100, 30).rotateDeg(40).scale(1.5, 1.0), ShapeList::Right, ShapeList::AlignCenter);
board.saveEPS("stroke_path.eps", Board::A4);
board.scaleToWidth(25, UseLineWidth);
board.saveSVG("stroke_path.svg", Board::BoundingBox, 0.0, Board::UCentimeter);
}
LibBoard::Board::saveEPS
void saveEPS(std::ostream &out, PageSize size=Board::BoundingBox, double margin=0.0, Unit unit=UMillimeter, const std::string &title=std::string()) const
Definition: Board.cpp:532
LibBoard::Polyline
A polygonal line described by a series of 2D points.
Definition: Polyline.h:38
LibBoard::Board::saveSVG
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=0.0, Unit unit=UMillimeter) const
Definition: Board.cpp:758
LibBoard::Shape::scaleToWidth
Shape & scaleToWidth(double w, LineWidthFlag lineWidthFlag)
Definition: Shape.cpp:85
LibBoard::Line::scale
Line & scale(double sx, double sy) override
Definition: Line.cpp:99
PathBoundaries.h
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
LibBoard::Text
A piece of text.
Definition: Text.h:40
LibBoard::Point
Struct representing a 2D point.
Definition: Point.h:42
LibBoard::Line
A line between two points.
Definition: Line.h:38
LibBoard::Point::rotatedPI2
Point rotatedPI2() const
Definition: Point.h:369
LibBoard::Board::enableLineWidthScaling
static void enableLineWidthScaling()
Definition: Board.cpp:134
LibBoard::Point::normalise
Point & normalise()
Definition: Point.h:408
LibBoard::Path
A path, according to Postscript and SVG definition.
Definition: Path.h:45
LibBoard::ShapeList::clear
ShapeList & clear()
Definition: ShapeList.cpp:86
LibBoard::ShapeList
A group of shapes.
Definition: ShapeList.h:46
Board.h
Declaration of the Board class.
LibBoard::Board::clear
void clear(const Color &color=Color::Null)
Definition: Board.cpp:149
LibBoard::Board
Class for EPS, FIG or SVG drawings.
Definition: Board.h:62
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