Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
polygon_offset_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
33
34# include <iostream>
35# include <cmath>
36# include <tikzgeom_algorithms.H>
37
38using namespace std;
39using namespace Aleph;
40
41static Geom_Number poly_area(const Polygon & p)
42{
44 if (a < 0) a = -a;
45 return a;
46}
47
48int main()
49{
50 // ----- L-shaped polygon -----
51 Polygon L;
52 L.add_vertex(Point(0, 0));
53 L.add_vertex(Point(4, 0));
54 L.add_vertex(Point(4, 2));
55 L.add_vertex(Point(2, 2));
56 L.add_vertex(Point(2, 4));
57 L.add_vertex(Point(0, 4));
58 L.close();
59
60 // ----- Star polygon -----
62 const double R = 5.0, r = 2.0;
63 for (int i = 0; i < 5; ++i)
64 {
65 double angle_out = M_PI / 2.0 + i * 2.0 * M_PI / 5.0;
66 double angle_in = angle_out + M_PI / 5.0;
69 star.add_vertex(Point(Geom_Number(r * cos(angle_in)),
71 }
72 star.close();
73
75
76 // -- L-shape offsets --
77 cout << "=== L-shape ===" << endl;
78 cout << "Original: " << L.size() << " vertices, area = "
79 << poly_area(L).get_d() << endl;
80
81 auto L_out1 = off(L, Geom_Number(1));
82 if (!L_out1.is_empty())
83 cout << "Outward d=1: " << L_out1.polygons(0).size() << " vertices, area = "
84 << poly_area(L_out1.polygons(0)).get_d() << endl;
85
86 auto L_out2 = off(L, Geom_Number(2));
87 if (!L_out2.is_empty())
88 cout << "Outward d=2: " << L_out2.polygons(0).size() << " vertices, area = "
89 << poly_area(L_out2.polygons(0)).get_d() << endl;
90
91 auto L_in = off(L, Geom_Number(Geom_Number(-1) / 2));
92 if (!L_in.is_empty())
93 cout << "Inward d=-0.5: " << L_in.polygons(0).size() << " vertices, area = "
94 << poly_area(L_in.polygons(0)).get_d() << endl;
95
96 // -- Star offsets --
97 cout << "\n=== Star ===" << endl;
98 cout << "Original: " << star.size() << " vertices, area = "
99 << poly_area(star).get_d() << endl;
100
101 auto S_out = off(star, Geom_Number(Geom_Number(1) / 2));
102 if (!S_out.is_empty())
103 {
104 Geom_Number total = 0;
105 for (size_t i = 0; i < S_out.size(); ++i)
106 total += poly_area(S_out.polygons(i));
107 cout << "Outward d=0.5: " << S_out.size() << " polygon(s), total area = "
108 << total.get_d() << endl;
109 }
110
111 // -- Miter vs Bevel comparison --
112 cout << "\n=== Miter vs Bevel (L-shape, d=1) ===" << endl;
113 auto miter = off(L, Geom_Number(1), PolygonOffset::JoinType::Miter);
114 auto bevel = off(L, Geom_Number(1), PolygonOffset::JoinType::Bevel);
115 if (!miter.is_empty())
116 cout << "Miter: " << miter.polygons(0).size() << " vertices, area = "
117 << poly_area(miter.polygons(0)).get_d() << endl;
118 if (!bevel.is_empty())
119 cout << "Bevel: " << bevel.polygons(0).size() << " vertices, area = "
120 << poly_area(bevel.polygons(0)).get_d() << endl;
121
122 // ----- TikZ output -----
123 const string filename = "polygon_offset_output.tex";
124 ofstream out(filename);
125 if (!out.is_open())
126 {
127 cerr << "Cannot open " << filename << endl;
128 return 1;
129 }
130
131 out << "\\documentclass[border=5mm]{standalone}\n"
132 << "\\usepackage{tikz}\n"
133 << "\\begin{document}\n";
134
135 // Sub-figure 1: Original L
136 {
137 Tikz_Plane plane(8, 8);
138 put_in_plane(plane, L, tikz_wire_style("black", 1.0));
139 put_polygon_vertices(plane, L, tikz_points_style("black"));
140 out << "% Original L-shape\n";
141 plane.draw(out);
142 out << "\n\\hspace{1cm}\n";
143 }
144
145 // Sub-figure 2: Outward d=1
146 if (!L_out1.is_empty())
147 {
148 Tikz_Plane plane(10, 10);
149 put_in_plane(plane, L, tikz_wire_style("gray!50"));
150 put_in_plane(plane, L_out1.polygons(0), tikz_wire_style("blue!80", 1.2));
151 put_polygon_vertices(plane, L_out1.polygons(0), tikz_points_style("red"));
152 out << "% Outward d=1\n";
153 plane.draw(out);
154 out << "\n\\hspace{1cm}\n";
155 }
156
157 // Sub-figure 3: Outward d=2
158 if (!L_out2.is_empty())
159 {
160 Tikz_Plane plane(12, 12);
161 put_in_plane(plane, L, tikz_wire_style("gray!50"));
162 put_in_plane(plane, L_out2.polygons(0), tikz_wire_style("green!60!black", 1.2));
163 put_polygon_vertices(plane, L_out2.polygons(0), tikz_points_style("red"));
164 out << "% Outward d=2\n";
165 plane.draw(out);
166 out << "\n\\hspace{1cm}\n";
167 }
168
169 // Sub-figure 4: Inward d=-0.5
170 if (!L_in.is_empty())
171 {
172 Tikz_Plane plane(8, 8);
173 put_in_plane(plane, L, tikz_wire_style("gray!50"));
174 put_in_plane(plane, L_in.polygons(0), tikz_wire_style("red!80", 1.2));
175 put_polygon_vertices(plane, L_in.polygons(0), tikz_points_style("blue"));
176 out << "% Inward d=-0.5\n";
177 plane.draw(out);
178 out << "\n";
179 }
180
181 out << "\\end{document}\n";
182 out.close();
183 cout << "\nTikZ output written to " << filename << endl;
184
185 return 0;
186}
static Geom_Number signed_double_area(const Array< Point > &verts)
Compute twice the signed area (shoelace formula without division).
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:229
Offset (inflate/deflate) an arbitrary simple polygon.
A general (irregular) 2D polygon defined by a sequence of vertices.
Definition polygon.H:246
void add_vertex(const Point &point)
Add a vertex to the polygon.
Definition polygon.H:677
2D TikZ canvas storing geometry objects and emitting LaTeX output.
Definition tikzgeom.H:184
void draw(std::ostream &output, const bool squarize=true) const
Emit a complete tikzpicture with all inserted objects.
Definition tikzgeom.H:1383
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_cos_function > > cos(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4069
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sin_function > > sin(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4070
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void put_in_plane(Tikz_Plane &plane, const Geom &geom_obj)
Insert any supported geometry type in a Tikz_Plane.
Definition tikzgeom.H:1456
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
void put_polygon_vertices(Tikz_Plane &plane, const Polygon &poly, const Tikz_Style &style=tikz_points_style(), const int layer=Tikz_Plane::Layer_Overlay)
Inserts the vertices of a polygon as styled points.
mpq_class Geom_Number
Numeric type used by the geometry module.
Definition point.H:115
Tikz_Style tikz_wire_style(const std::string &color="black", const bool dashed=false, const bool with_arrow=false)
Creates a style optimized for wireframe segments and polygons.
Tikz_Style tikz_points_style(const std::string &color="black", const double opacity=-1.0)
Creates a style optimized for point clouds.
STL namespace.
static Geom_Number poly_area(const Polygon &p)
gsl_rng * r
Helpers to visualize computational-geometry algorithm results in TikZ.