Skip to content

Commit a0c8b61

Browse files
committed
Significant rewrite of most sample apps
1 parent 0a9f445 commit a0c8b61

File tree

30 files changed

+987
-1839
lines changed

30 files changed

+987
-1839
lines changed

CPP/Examples/Inflate/Inflate.cpp

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,44 @@
77
using namespace std;
88
using namespace Clipper2Lib;
99

10-
void DoRabbit();
11-
void DoSimpleShapes();
12-
void System(const std::string& filename);
13-
10+
void System(const std::string& filename)
11+
{
12+
#ifdef _WIN32
13+
system(filename.c_str());
14+
#else
15+
system(("firefox " + filename).c_str());
16+
#endif
17+
}
1418

15-
int main(int argc, char* argv[])
19+
static void DisplayAsSvgImage(const std::string& caption, FillRule fillrule,
20+
const PathsD* subject, const PathsD* clip, const PathsD* solution)
1621
{
22+
const std::string filename = caption + ".SVG";
23+
SvgWriter svg;
24+
if (subject)
25+
SvgAddSubject(svg, *subject, fillrule);
26+
if (clip)
27+
SvgAddClip(svg, *clip, fillrule);
28+
if (solution)
29+
SvgAddSolution(svg, *solution, fillrule, false);
30+
SvgSaveToFile(svg, filename, 400, 400, 10);
31+
System(filename);
32+
}
1733

18-
DoSimpleShapes();
19-
DoRabbit();
20-
//std::getchar();
34+
void DoRabbit()
35+
{
36+
SvgReader svg_reader("./rabbit.svg");
37+
PathsD p = svg_reader.paths;
38+
PathsD solution(p);
39+
while (p.size())
40+
{
41+
p = InflatePaths(p, -5, JoinType::Round, EndType::Polygon);
42+
// SimplifyPaths is not essential but is **highly recommended**
43+
// because it speeds up the loop by removing tiny artefacts
44+
p = SimplifyPaths(p, 0.25);
45+
copy(p.begin(), p.end(), back_inserter(solution));
46+
}
47+
DisplayAsSvgImage("rabbit_offset", FillRule::EvenOdd, nullptr, nullptr, &solution);
2148
}
2249

2350
void DoSimpleShapes()
@@ -99,39 +126,9 @@ void DoSimpleShapes()
99126
System(filename);
100127
}
101128

102-
void DoRabbit()
129+
int main(int argc, char* argv[])
103130
{
104-
SvgReader svg_reader;
105-
svg_reader.LoadFromFile("./rabbit.svg");
106-
PathsD p = svg_reader.GetPaths();
107-
108-
JoinType jt = JoinType::Round;
109-
PathsD solution(p);
110-
111-
while (p.size())
112-
{
113-
// nb: don't forget to scale the delta offset too!
114-
p = InflatePaths(p, -2.5, jt, EndType::Polygon);
115-
// SimplifyPaths (or RamerDouglasPeucker) is not
116-
// essential but is highly recommended because it
117-
// speeds up the loop and also tidies up the result
118-
p = SimplifyPaths(p, 0.25); // preferred over RDP()
119-
solution.reserve(solution.size() + p.size());
120-
copy(p.begin(), p.end(), back_inserter(solution));
121-
}
122-
123-
FillRule fr = FillRule::EvenOdd;
124-
SvgWriter svg;
125-
SvgAddSolution(svg, solution, fr, false);
126-
SvgSaveToFile(svg, "solution_off2.svg", 450, 720, 0);
127-
System("solution_off2.svg");
131+
DoSimpleShapes();
132+
DoRabbit();
128133
}
129134

130-
void System(const std::string& filename)
131-
{
132-
#ifdef _WIN32
133-
system(filename.c_str());
134-
#else
135-
system(("firefox " + filename).c_str());
136-
#endif
137-
}

CPP/Examples/Inflate/rabbit.svg

Lines changed: 3 additions & 7 deletions
Loading

CPP/Examples/SimpleClipping/SimpleClipping.cpp

Lines changed: 20 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,35 @@
22
#include <string>
33

44
#include "clipper2/clipper.h"
5+
#include "clipper2/clipper.core.h"
6+
#include "../../Utils/clipper.svg.h"
57
#include "../../Utils/clipper.svg.utils.h"
68

79
using namespace Clipper2Lib;
810

9-
void DoSimpleTest(bool show_solution_coords = false);
10-
Path64 MakeRandomPoly(int width, int height, unsigned vertCnt);
11-
void System(const std::string &filename);
12-
13-
int main()
14-
{
15-
DoSimpleTest();
16-
}
17-
18-
inline Path64 MakeStar(const Point64& center, int radius, int points)
11+
static void DisplayAsSvgImage(const std::string& caption, FillRule fillrule,
12+
const Paths64& subject, const Paths64& clip, const Paths64& solution)
1913
{
20-
if (!(points % 2)) --points;
21-
if (points < 5) points = 5;
22-
Path64 tmp = Ellipse<int64_t>(center, radius, radius, points);
23-
Path64 result;
24-
result.reserve(points);
25-
result.push_back(tmp[0]);
26-
for (int i = points - 1, j = i / 2; j;)
27-
{
28-
result.push_back(tmp[j--]);
29-
result.push_back(tmp[i--]);
30-
}
31-
return result;
32-
}
33-
34-
35-
void DoSimpleTest(bool show_solution_coords)
36-
{
37-
Paths64 tmp, solution;
38-
FillRule fr = FillRule::NonZero;
39-
40-
Paths64 subject, clip;
41-
subject.push_back(MakeStar(Point64(225, 225), 220, 9));
42-
clip.push_back(Ellipse<int64_t>(Point64(225,225), 150, 150));
43-
44-
//Intersect both shapes and then 'inflate' result -10 (ie deflate)
45-
solution = Intersect(subject, clip, fr);
46-
solution = InflatePaths(solution, -10, JoinType::Round, EndType::Polygon);
47-
14+
const std::string filename = caption + ".SVG";
4815
SvgWriter svg;
49-
SvgAddSubject(svg, subject, fr);
50-
SvgAddClip(svg, clip, fr);
51-
SvgAddSolution(svg, solution, fr, false);
52-
SvgSaveToFile(svg, "solution.svg", 450, 450, 10);
53-
System("solution.svg");
54-
}
55-
56-
void System(const std::string &filename)
57-
{
16+
SvgAddSubject(svg, subject, fillrule);
17+
SvgAddClip(svg, clip, fillrule);
18+
SvgAddSolution(svg, solution, fillrule, false);
19+
SvgSaveToFile(svg, filename, 400, 400, 10);
5820
#ifdef _WIN32
5921
system(filename.c_str());
6022
#else
6123
system(("firefox " + filename).c_str());
6224
#endif
6325
}
26+
27+
int main()
28+
{
29+
// intersect a star and another modestly rotated star
30+
Paths64 subject, clip, solution;
31+
subject.push_back({ MakePath({200,100, 20,158, 130,4, 130,196, 20,42}) });
32+
clip.push_back({ MakePath({196,126, 8,136, 154,16, 104,200, 38,24}) });
33+
solution = Intersect(subject, clip, FillRule::NonZero);
34+
DisplayAsSvgImage("Intersect Paths", FillRule::NonZero, subject, clip, solution);
35+
}
36+

CPP/Examples/Triangulation/TriSamples/coral3.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)