142using namespace Aleph;
151 cout <<
"+" << string(70,
'-') <<
"+" <<
endl;
153 cout <<
"+" << string(70,
'-') <<
"+" <<
endl;
176 if (!label.empty())
cout << label <<
": ";
186 cout <<
"\n " << name <<
" (" << poly.
size() <<
" vertices):" <<
endl;
190 const Vertex& v = it.get_current_vertex();
201 cout <<
" T" << index <<
": ";
234 print_header(
"Example 1: Polygon Triangulation - Basic Shapes");
251 cout <<
"\n Triangulation result:" <<
endl;
263 <<
" square units" <<
endl;
264 cout <<
" Expected area: 10000.00 square units" <<
endl;
271 for (
int j = 0; j < 5; ++j)
273 double angle = 2 *
M_PI * j / 5 -
M_PI / 2;
282 cout <<
"\n Triangulation result:" <<
endl;
294 <<
" square units" <<
endl;
303 print_header(
"Example 2: Complex Polygon Triangulation");
322 cout <<
"\n Triangulation result:" <<
endl;
334 <<
" square units" <<
endl;
337 cout <<
" Expected area: 4000.00 square units (60x40 + 40x40)" <<
endl;
346 print_header(
"Example 3: Convex Hull - Colombian Cities");
354 struct CityCoord {
const char* name;
double x;
double y; };
373 cout <<
"\n Colombian cities included:" <<
endl;
377 cout <<
" " << left <<
setw(15) << c.name
378 <<
"(" << c.x <<
", " << c.y <<
")" <<
endl;
384 for (
auto it =
cities.
get_it(); it.has_curr(); it.next_ne())
395 auto start = chrono::high_resolution_clock::now();
397 auto end = chrono::high_resolution_clock::now();
398 auto bf_time = chrono::duration_cast<chrono::microseconds>(end - start).count();
406 start = chrono::high_resolution_clock::now();
408 end = chrono::high_resolution_clock::now();
409 auto gw_time = chrono::duration_cast<chrono::microseconds>(end - start).count();
417 start = chrono::high_resolution_clock::now();
419 end = chrono::high_resolution_clock::now();
420 auto qh_time = chrono::duration_cast<chrono::microseconds>(end - start).count();
428 cout <<
" The convex hull represents the outermost cities:" <<
endl;
431 const Vertex& v = it.get_current_vertex();
452 print_header(
"Example 4: Convex Hull Algorithm Performance");
454 cout <<
"\n Comparing algorithms on random point sets:" <<
endl;
455 cout <<
" " << string(60,
'-') <<
endl;
463 cout <<
"\n " <<
setw(8) <<
"Points"
464 <<
setw(15) <<
"Brute Force"
465 <<
setw(15) <<
"Gift Wrap"
466 <<
setw(15) <<
"QuickHull"
468 cout <<
" " << string(60,
'-') <<
endl;
470 for (
size_t s = 0; s <
sizes.
size(); ++s)
478 for (
int i = 0; i < n; ++i)
480 double x = (
rand() % 1000) / 10.0;
481 double y = (
rand() % 1000) / 10.0;
492 auto start = chrono::high_resolution_clock::now();
494 auto end = chrono::high_resolution_clock::now();
495 auto bf_time = chrono::duration_cast<chrono::microseconds>(end - start).count();
497 start = chrono::high_resolution_clock::now();
499 end = chrono::high_resolution_clock::now();
500 auto gw_time = chrono::duration_cast<chrono::microseconds>(end - start).count();
502 start = chrono::high_resolution_clock::now();
504 end = chrono::high_resolution_clock::now();
505 auto qh_time = chrono::duration_cast<chrono::microseconds>(end - start).count();
514 cout <<
"\n Note: Times in microseconds (us)" <<
endl;
515 cout <<
" Brute Force grows as O(n^3)" <<
endl;
516 cout <<
" Gift Wrapping grows as O(nh) where h = hull size" <<
endl;
517 cout <<
" QuickHull grows as O(n log n) on average" <<
endl;
535 cout <<
" Three major Colombian cities:" <<
endl;
544 cout <<
"\n Route lengths (approximate):" <<
endl;
553 cout <<
" Test point: (73, 50)" <<
endl;
554 cout <<
" Relative to line Bogota-Medellin:" <<
endl;
557 cout <<
" Point is to the LEFT" <<
endl;
559 cout <<
" Point is to the RIGHT" <<
endl;
561 cout <<
" Point is ON the line" <<
endl;
567 cout <<
" Triangle formed by Bogota, Medellin, Cali:" <<
endl;
575 cout <<
"\n Point containment:" <<
endl;
576 cout <<
" Point (75, 45): ";
578 cout <<
"INSIDE the triangle" <<
endl;
580 cout <<
"OUTSIDE the triangle" <<
endl;
582 cout <<
" Point (80, 80): ";
584 cout <<
"INSIDE the triangle" <<
endl;
586 cout <<
"OUTSIDE the triangle" <<
endl;
597 cout <<
"\n Scenario: Calculate coverage area of cell towers" <<
endl;
598 cout <<
" " << string(50,
'-') <<
endl;
612 cout <<
"\n Tower locations:" <<
endl;
614 for (
auto it =
towers.
get_it(); it.has_curr(); it.next_ne())
616 const Point& p = it.get_curr();
626 cout <<
"\n Coverage boundary (convex hull):" <<
endl;
639 cout <<
"\n Coverage statistics:" <<
endl;
648 const Segment&
seg = it.get_current_segment();
654 <<
" (1.0 = circle)" <<
endl;
663 cout <<
"Usage: " <<
prog <<
" [-s <triangulation|convexhull|all>] [--help]\n";
664 cout <<
"\nIf no selector is given, all demos are executed.\n";
670 for (
int i = 1; i <
argc; ++i)
673 if (
arg ==
"--help" ||
arg ==
"-h")
689 cout <<
"Unknown argument: " <<
arg <<
"\n";
695 cout <<
"========================================================================" <<
endl;
696 cout <<
" ALEPH-W COMPUTATIONAL GEOMETRY EXAMPLE" <<
endl;
697 cout <<
" Triangulation and Convex Hull Algorithms" <<
endl;
698 cout <<
"========================================================================" <<
endl;
726 cout <<
"========================================================================" <<
endl;
727 cout <<
" Example completed successfully!" <<
endl;
728 cout <<
"========================================================================" <<
endl;
Brute force convex hull algorithm.
Polygon triangulation using the ear-cutting algorithm.
Dynamic singly linked list with functional programming support.
T & append(const T &item)
Append a new item by copy.
Gift wrapping (Jarvis march) convex hull algorithm.
size_t size() const noexcept
Count the number of elements of the list.
QuickHull convex hull algorithm.
size_t length() const noexcept
Count the number of elements of a container.
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
Rectangular point in the plane.
const Geom_Number & get_y() const
Returns y value.
const Geom_Number & get_x() const
Returns x 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 close()
Close the polygon.
const size_t & size() const
Get the number of vertices.
void add_vertex(const Point &point)
Add a vertex to the polygon.
Fundamental segment defined by two points.
const Point & get_p1() const
const Point & get_p3() const
bool contains_to(const Point &p) const
Returns true if point p lies inside this triangle.
const Point & get_p2() const
A vertex in a polygon's doubly-linked vertex list.
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.
static void usage(const char *prog)
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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Iterator over the vertices of a polygon.
Lazy and scalable dynamic array implementation.