144using namespace Aleph;
153 cout <<
"+" << string(70,
'-') <<
"+" << endl;
154 cout <<
"| " << left << setw(68) << title <<
" |" << endl;
155 cout <<
"+" << string(70,
'-') <<
"+" << endl;
160 cout <<
"\n " << subtitle << endl;
161 cout <<
" " << string(subtitle.length(),
'-') << 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)
227 return 0.5 *
abs(ax * by - ay * bx);
236 print_header(
"Example 1: Polygon Triangulation - Basic Shapes");
253 cout <<
"\n Triangulation result:" << endl;
255 double total_area = 0;
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)
275 double angle = 2 * M_PI * j / 5 - M_PI / 2;
284 cout <<
"\n Triangulation result:" << endl;
287 for (
auto it = pent_triangles.
get_it(); it.has_curr(); it.next_ne())
294 cout <<
"\n Total triangles: " << pent_triangles.
size() << endl;
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;
326 double total_area = 0;
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; };
358 city_data.
append({
"Bogota", 74, 44});
359 city_data.
append({
"Medellin", 75, 61});
360 city_data.
append({
"Cali", 76, 34});
361 city_data.
append({
"Barranquilla", 74, 109});
362 city_data.
append({
"Cartagena", 75, 104});
363 city_data.
append({
"Cucuta", 72, 77});
364 city_data.
append({
"Bucaramanga", 73, 71});
365 city_data.
append({
"Pereira", 75, 47});
366 city_data.
append({
"Santa Marta", 74, 111});
367 city_data.
append({
"Ibague", 75, 44});
368 city_data.
append({
"Pasto", 77, 12});
369 city_data.
append({
"Manizales", 75, 51});
370 city_data.
append({
"Villavicencio", 73, 41});
371 city_data.
append({
"Armenia", 75, 44});
372 city_data.
append({
"Leticia", 70, 0});
373 city_data.
append({
"Riohacha", 72, 116});
375 cout <<
"\n Colombian cities included:" << endl;
376 for (
size_t i = 0; i < city_data.
size(); ++i)
378 const auto& c = city_data(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())
388 cities_bf.
append(it.get_curr());
389 cities_gw.
append(it.get_curr());
390 cities_qh.
append(it.get_curr());
397 auto start = chrono::high_resolution_clock::now();
398 Polygon hull_bf = bf_hull(cities_bf);
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();
409 Polygon hull_gw = gw_hull(cities_gw);
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();
420 Polygon hull_qh = qh_hull(cities_qh);
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)
437 const auto& c = city_data(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();
495 Polygon hull_bf = bf(points_bf);
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();
500 Polygon hull_gw = gw(points_gw);
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();
505 Polygon hull_qh = qh(points_qh);
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"
513 << setw(10) << hull_qh.
size() << endl;
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;
533 Point bogota(74, 44);
534 Point medellin(75, 61);
537 cout <<
" Three major Colombian cities:" << endl;
543 Segment bogota_medellin(bogota, medellin);
544 Segment bogota_cali(bogota, cali);
546 cout <<
"\n Route lengths (approximate):" << endl;
547 cout <<
" Bogota-Medellin: " << fixed << setprecision(2)
554 Point test_point(73, 50);
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;
567 Triangle triangle(bogota, medellin, cali);
569 cout <<
" Triangle formed by Bogota, Medellin, Cali:" << endl;
570 cout <<
" Area: " << fixed << setprecision(2)
574 Point inside(75, 45);
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;
633 Polygon coverage_copy = coverage;
637 double total_area = 0;
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)
643 << total_area <<
" square km" << endl;
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;
683 const auto vor = voronoi(dt);
684 cout <<
" Voronoi: vertices=" << vor.vertices.size()
685 <<
" edges=" << vor.edges.size() << endl;
694 cout <<
" Voronoi clipped cells=" << clipped.size() << endl;
708 const Point q_inside(1, 1);
709 const Point q_outside(6, 4);
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";
742int main(
int argc,
char* argv[])
745 for (
int i = 1; i < argc; ++i)
747 const string arg = argv[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.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
size_t size() const noexcept
Return the number of items of the list, in constant time.
Gift wrapping (Jarvis march) convex hull algorithm.
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.
bool is_to_right_from(const Point &p1, const Point &p2) const
Checks if this point is to the right of the directed line from p1 to p2.
const Geom_Number & get_x() const noexcept
Gets the x-coordinate value.
const Geom_Number & get_y() const noexcept
Gets the y-coordinate value.
bool is_left_of(const Point &p1, const Point &p2) const
Checks if this point is to the left of the directed line from p1 to p2.
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()
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.
static void section(const string &title)
Iterator over the vertices of a polygon.
Lazy and scalable dynamic array implementation.