Closest Pairs
Running TimeThe running time is O(nlogn) where n is the number of input points. ExampleThe following program generates ten random points in a square and then
computes the closest pair. The result is displayed using a #include <LEDA/core/list.h>
#include <LEDA/geo/rat_point.h>
#include <LEDA/geo/random_rat_point.h>
#include <LEDA/graphics/window.h>
#include <LEDA/geo/geo_alg.h>
#include <LEDA/numbers/rational.h>
using namespace leda;
int main()
{
list<rat_point> L;
random_points_in_square(10,100,L);
rat_point r1,r2;
rational r=CLOSEST_PAIR(L,r1,r2);
window W;
W.init(-110,110,-110);
W.open(); W.display();
W.set_node_width(2);
rat_point p;
forall(p,L) W.draw_filled_node(p.to_point());
W.set_node_width(3);
W.draw_filled_node(r1.to_point(),red);
W.draw_filled_node(r2.to_point(),red);
W.draw_segment(r1.to_point(),r2.to_point(),red);
W.read_mouse();
W.screenshot("closest_pair");
return 0;
}
|
See also:Writing Kernel Independent Code Manual Entries |
||||