Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
closest_pair_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
44# include <geom_algorithms.H>
45
46# include <cassert>
47# include <iostream>
48
49using namespace Aleph;
50using namespace std;
51
52static void print_banner(const char * title)
53{
54 cout << "[Aleph Geometry Example] " << title << "\n";
55 cout << "============================================================\n";
56}
57
60{
61 Array<Point> arr;
62 for (DynList<Point>::Iterator it(pts); it.has_curr(); it.next_ne())
63 arr.append(it.get_curr());
64
65 assert(arr.size() >= 2);
66 Geom_Number best = arr(0).distance_squared_to(arr(1));
67 out_a = arr(0);
68 out_b = arr(1);
69
70 for (size_t i = 0; i < arr.size(); ++i)
71 for (size_t j = i + 1; j < arr.size(); ++j)
72 if (const Geom_Number d2 = arr(i).distance_squared_to(arr(j)); d2 < best)
73 {
74 best = d2;
75 out_a = arr(i);
76 out_b = arr(j);
77 }
78
79 return best;
80}
81
82int main()
83{
84 print_banner("Closest Pair");
85
87 pts.append(Point(0, 0));
88 pts.append(Point(5, 2));
89 pts.append(Point(9, 8));
90 pts.append(Point(3, 4));
91 pts.append(Point(4, 4));
92 pts.append(Point(8, 1));
93 pts.append(Point(7, 7));
94 pts.append(Point(4, 5));
95
97 const auto r = cp(pts);
98
99 cout << "Closest pair: (" << r.first.get_x() << ", " << r.first.get_y() << ") and "
100 << "(" << r.second.get_x() << ", " << r.second.get_y() << ")" << endl;
101 cout << "distance^2 = " << r.distance_squared << endl;
102
103 Point ba, bb;
105 cout << "Brute-force distance^2 = " << brute_d2 << endl;
106
107 assert(r.distance_squared == brute_d2);
108 cout << "Validation OK: divide-and-conquer matches brute force." << endl;
109
110 DynList<Point> dup;
111 dup.append(Point(10, 10));
112 dup.append(Point(2, 3));
113 dup.append(Point(10, 10)); // duplicate point
114 const auto r_dup = cp(dup);
115 assert(r_dup.distance_squared == 0);
116 cout << "Duplicate-point scenario validated: distance^2 = 0." << endl;
117 cout << "STATUS: OK" << endl;
118 return 0;
119}
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:351
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:245
Closest pair of points via divide and conquer.
Iterator on the items of list.
Definition htlist.H:1420
Dynamic singly linked list with functional programming support.
Definition htlist.H:1155
T & append(const T &item)
Definition htlist.H:1271
bool has_curr() const noexcept
Definition htlist.H:930
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:229
static void print_banner(const char *title)
int main()
static Geom_Number brute_distance_squared(const DynList< Point > &pts, Point &out_a, Point &out_b)
Computational geometry algorithms.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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.
STL namespace.
gsl_rng * r