144using namespace Aleph;
153 cout <<
"+" << string(70,
'-') <<
"+" <<
endl;
154 cout <<
"| " << left << setw(68) << title <<
" |" <<
endl;
155 cout <<
"+" << string(70,
'-') <<
"+" <<
endl;
178 if (!label.empty()) cout << label <<
": ";
188 cout <<
"\n " << name <<
" (" << poly.
size() <<
" vertices):" <<
endl;
192 const Vertex& v = it.get_current_vertex();
193 cout <<
" V" << i++ <<
": (" << fixed << setprecision(2)
203 cout <<
" T" << index <<
": ";
207 cout <<
"(" << fixed << setprecision(1)
236 print_header(
"Example 1: Polygon Triangulation - Basic Shapes");
253 cout <<
"\n Triangulation result:" <<
endl;
256 for (
auto it = triangles.
get_it(); it.has_curr(); it.next_ne())
263 cout <<
"\n Total triangles: " << triangles.
size() <<
endl;
264 cout <<
" Total area: " << fixed << setprecision(2) <<
total_area
265 <<
" square units" <<
endl;
266 cout <<
" Expected area: 10000.00 square units" <<
endl;
273 for (
int j = 0; j < 5; ++j)
284 cout <<
"\n Triangulation result:" <<
endl;
287 for (
auto it =
pent_triangles.get_it(); it.has_curr(); it.next_ne())
295 cout <<
" Total area: " << fixed << setprecision(2) <<
total_area
296 <<
" square units" <<
endl;
305 print_header(
"Example 2: Complex Polygon Triangulation");
324 cout <<
"\n Triangulation result:" <<
endl;
327 for (
auto it = triangles.
get_it(); it.has_curr(); it.next_ne())
334 cout <<
"\n Total triangles: " << triangles.
size() <<
endl;
335 cout <<
" Total area: " << fixed << setprecision(2) <<
total_area
336 <<
" square units" <<
endl;
339 cout <<
" Expected area: 4000.00 square units (60x40 + 40x40)" <<
endl;
348 print_header(
"Example 3: Convex Hull - Colombian Cities");
356 struct CityCoord {
const char* name;
double x;
double y; };
361 city_data.append({
"Barranquilla", 74, 109});
362 city_data.append({
"Cartagena", 75, 104});
364 city_data.append({
"Bucaramanga", 73, 71});
366 city_data.append({
"Santa Marta", 74, 111});
370 city_data.append({
"Villavicencio", 73, 41});
375 cout <<
"\n Colombian cities included:" <<
endl;
376 for (
size_t i = 0; i <
city_data.size(); ++i)
379 cout <<
" " << left << setw(15) << c.name
380 <<
"(" << c.x <<
", " << c.y <<
")" <<
endl;
386 for (
auto it =
cities.get_it(); it.has_curr(); it.next_ne())
397 auto start = chrono::high_resolution_clock::now();
399 auto end = chrono::high_resolution_clock::now();
400 auto bf_time = chrono::duration_cast<chrono::microseconds>(end - start).count();
402 cout <<
" Hull vertices: " <<
hull_bf.size() <<
endl;
403 cout <<
" Time: " <<
bf_time <<
" microseconds" <<
endl;
408 start = chrono::high_resolution_clock::now();
410 end = chrono::high_resolution_clock::now();
411 auto gw_time = chrono::duration_cast<chrono::microseconds>(end - start).count();
413 cout <<
" Hull vertices: " <<
hull_gw.size() <<
endl;
414 cout <<
" Time: " <<
gw_time <<
" microseconds" <<
endl;
419 start = chrono::high_resolution_clock::now();
421 end = chrono::high_resolution_clock::now();
422 auto qh_time = chrono::duration_cast<chrono::microseconds>(end - start).count();
424 cout <<
" Hull vertices: " <<
hull_qh.size() <<
endl;
425 cout <<
" Time: " <<
qh_time <<
" microseconds" <<
endl;
430 cout <<
" The convex hull represents the outermost cities:" <<
endl;
433 const Vertex& v = it.get_current_vertex();
435 for (
size_t i = 0; i <
city_data.size(); ++i)
441 cout <<
" - " << c.name <<
endl;
454 print_header(
"Example 4: Convex Hull Algorithm Performance");
456 cout <<
"\n Comparing algorithms on random point sets:" <<
endl;
457 cout <<
" " << string(60,
'-') <<
endl;
465 cout <<
"\n " << setw(8) <<
"Points"
466 << setw(15) <<
"Brute Force"
467 << setw(15) <<
"Gift Wrap"
468 << setw(15) <<
"QuickHull"
469 << setw(10) <<
"Hull Size" <<
endl;
470 cout <<
" " << string(60,
'-') <<
endl;
472 for (
size_t s = 0; s < sizes.
size(); ++s)
480 for (
int i = 0; i < n; ++i)
482 double x = (
rand() % 1000) / 10.0;
483 double y = (
rand() % 1000) / 10.0;
494 auto start = chrono::high_resolution_clock::now();
496 auto end = chrono::high_resolution_clock::now();
497 auto bf_time = chrono::duration_cast<chrono::microseconds>(end - start).count();
499 start = chrono::high_resolution_clock::now();
501 end = chrono::high_resolution_clock::now();
502 auto gw_time = chrono::duration_cast<chrono::microseconds>(end - start).count();
504 start = chrono::high_resolution_clock::now();
506 end = chrono::high_resolution_clock::now();
507 auto qh_time = chrono::duration_cast<chrono::microseconds>(end - start).count();
509 cout <<
" " << setw(8) << n
510 << setw(12) <<
bf_time <<
" us"
511 << setw(12) <<
gw_time <<
" us"
512 << setw(12) <<
qh_time <<
" us"
516 cout <<
"\n Note: Times in microseconds (us)" <<
endl;
517 cout <<
" Brute Force grows as O(n^3)" <<
endl;
518 cout <<
" Gift Wrapping grows as O(nh) where h = hull size" <<
endl;
519 cout <<
" QuickHull grows as O(n log n) on average" <<
endl;
537 cout <<
" Three major Colombian cities:" <<
endl;
546 cout <<
"\n Route lengths (approximate):" <<
endl;
547 cout <<
" Bogota-Medellin: " << fixed << setprecision(2)
555 cout <<
" Test point: (73, 50)" <<
endl;
556 cout <<
" Relative to line Bogota-Medellin:" <<
endl;
559 cout <<
" Point is to the LEFT" <<
endl;
561 cout <<
" Point is to the RIGHT" <<
endl;
563 cout <<
" Point is ON the line" <<
endl;
569 cout <<
" Triangle formed by Bogota, Medellin, Cali:" <<
endl;
570 cout <<
" Area: " << fixed << setprecision(2)
575 Point outside(80, 80);
577 cout <<
"\n Point containment:" <<
endl;
578 cout <<
" Point (75, 45): ";
580 cout <<
"INSIDE the triangle" <<
endl;
582 cout <<
"OUTSIDE the triangle" <<
endl;
584 cout <<
" Point (80, 80): ";
586 cout <<
"INSIDE the triangle" <<
endl;
588 cout <<
"OUTSIDE the triangle" <<
endl;
599 cout <<
"\n Scenario: Calculate coverage area of cell towers" <<
endl;
600 cout <<
" " << string(50,
'-') <<
endl;
614 cout <<
"\n Tower locations:" <<
endl;
616 for (
auto it =
towers.get_it(); it.has_curr(); it.next_ne())
618 const Point& p = it.get_curr();
619 cout <<
" Tower " <<
tower_num++ <<
": ("
628 cout <<
"\n Coverage boundary (convex hull):" <<
endl;
629 cout <<
" Boundary towers: " <<
coverage.size() <<
endl;
630 cout <<
" Interior towers: " << (9 -
coverage.size()) <<
endl;
638 for (
auto it = triangles.
get_it(); it.has_curr(); it.next_ne())
641 cout <<
"\n Coverage statistics:" <<
endl;
642 cout <<
" Total coverage area: " << fixed << setprecision(2)
644 cout <<
" Triangles in mesh: " << triangles.
size() <<
endl;
647 double perimeter = 0;
650 const Segment& seg = it.get_current_segment();
654 cout <<
" Perimeter: " << perimeter <<
" km" <<
endl;
655 cout <<
" Compactness ratio: " << (4 *
M_PI *
total_area) / (perimeter * perimeter)
656 <<
" (1.0 = circle)" <<
endl;
665 print_header(
"Example 7: Advanced Geometry (Delaunay/Voronoi/PIP/HPI)");
678 const auto dt = delaunay(sites);
679 cout <<
" Delaunay: sites=" <<
dt.sites.size()
680 <<
" triangles=" <<
dt.triangles.size() <<
endl;
684 cout <<
" Voronoi: vertices=" <<
vor.vertices.size()
685 <<
" edges=" <<
vor.edges.size() <<
endl;
694 cout <<
" Voronoi clipped cells=" <<
clipped.size() <<
endl;
710 cout <<
" q_inside -> "
713 cout <<
" q_outside -> "
728 cout <<
" Feasible polygon vertices: " << feasible.
size() <<
endl;
737 cout <<
"Usage: " <<
prog
738 <<
" [-s <triangulation|convexhull|advanced|all>] [--help]\n";
739 cout <<
"\nIf no selector is given, all demos are executed.\n";
745 for (
int i = 1; i <
argc; ++i)
748 if (
arg ==
"--help" ||
arg ==
"-h")
764 cout <<
"Unknown argument: " <<
arg <<
"\n";
770 cout <<
"========================================================================" <<
endl;
771 cout <<
" ALEPH-W COMPUTATIONAL GEOMETRY EXAMPLE" <<
endl;
772 cout <<
" Core + Advanced Geometry Algorithms" <<
endl;
773 cout <<
"========================================================================" <<
endl;
799 cout <<
"Unknown section: " <<
section <<
"\n";
805 cout <<
"========================================================================" <<
endl;
806 cout <<
" Example completed successfully!" <<
endl;
807 cout <<
"========================================================================" <<
endl;
Simple dynamic array with automatic resizing and functional operations.
T & append(const T &data)
Append a copy of data
Brute force convex hull algorithm.
Polygon triangulation using the ear-cutting algorithm.
Exact Delaunay triangulation using the Bowyer-Watson incremental algorithm.
bool has_curr() const noexcept
Return true if the iterator has current item.
size_t size() const noexcept
Return the current dimension of array.
T & append()
Allocate a new entry to the end of array.
Dynamic singly linked list with functional programming support.
T & append(const T &item)
Gift wrapping (Jarvis march) convex hull algorithm.
size_t size() const noexcept
Count the number of elements of the list.
Exact bounded intersection of half-planes.
static bool contains(const Polygon &poly, const Point &p)
Return true if the point is inside or on the boundary.
Represents a point with rectangular coordinates in a 2D plane.
const Geom_Number & get_x() const noexcept
Gets the x-coordinate value.
const Geom_Number & get_y() const noexcept
Gets the y-coordinate value.
Iterator over the edges (segments) of a polygon.
bool has_curr() const
Check if there is a current segment.
A general (irregular) 2D polygon defined by a sequence of vertices.
void add_vertex(const Point &point)
Add a vertex to the polygon.
void close()
Close the polygon.
const size_t & size() const
Get the number of vertices.
QuickHull convex hull algorithm.
Represents a line segment between two points.
Geom_Number length() const
Returns the Euclidean length of the segment.
A non-degenerate triangle defined by three points.
const Point & get_p3() const
Gets the third vertex.
const Point & get_p2() const
Gets the second vertex.
bool contains(const Point &p) const
Checks if a point lies strictly inside this triangle.
const Point & get_p1() const
Gets the first vertex.
A vertex in a polygon's doubly linked vertex list.
Voronoi diagram derived as the dual of a Delaunay triangulation.
static Array< ClippedCell > clipped_cells_indexed(const Array< Point > &sites, const Polygon &clip)
Clip Voronoi cells and return explicit site-indexed records.
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
Computational geometry algorithms.
void print_polygon(const Polygon &poly, const string &name)
Print a polygon's vertices.
void demo_convex_hull_performance()
void demo_triangulation_complex()
void demo_coverage_area()
void print_point(const Point &p, const string &label="")
Print a point with optional label.
void demo_advanced_algorithms()
void demo_triangulation_basic()
void print_triangle(const Triangle &t, int index)
Print a triangle.
void print_subheader(const string &subtitle)
void demo_convex_hull_cities()
double triangle_area(const Triangle &t)
Calculate area of a triangle using cross product.
double to_double(const Geom_Number &n)
Convert Geom_Number to double.
void demo_geometric_primitives()
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_cos_function > > cos(const __gmp_expr< T, U > &expr)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sin_function > > sin(const __gmp_expr< T, U > &expr)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_abs_function > > abs(const __gmp_expr< T, U > &expr)
Main namespace for Aleph-w library functions.
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.
static void section(const string &title)
Iterator over the vertices of a polygon.
Lazy and scalable dynamic array implementation.