home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / leda / prog / graphics / voronoi.c < prev   
Encoding:
C/C++ Source or Header  |  1991-11-15  |  2.3 KB  |  113 lines

  1. #include <LEDA/plane_alg.h>
  2. #include <LEDA/subdivision.h>
  3. #include <LEDA/window.h>
  4.  
  5.  
  6. declare(subdivision,point)
  7.  
  8.  
  9. main()
  10. {
  11.  
  12.   window W;
  13.  
  14.   W.init(0,1000,0);
  15.  
  16.   int node_width = 4;
  17.   int line_width = 1;
  18.   int input = 1;
  19.   int grid_width = 0;
  20.   int N = 100;
  21.  
  22.   panel P("VORONOI");
  23.  
  24.   P.text_item("This program computes the Voronoi diagram for a set  "); 
  25.   P.text_item("of point sites S in the plane. There are two ways of ");
  26.   P.text_item("defining set S. Mouse input: Use the left button to  ");
  27.   P.text_item("insert a sequence of points and terminate the input  ");
  28.   P.text_item("by clicking the right button. Random input: Give the ");
  29.   P.text_item("number of random points to be created by the program.");
  30.   P.text_item("The Voronoi diagram Vor(S) is computed and shown. Now");
  31.   P.text_item("you can define query points with the left button, for");
  32.   P.text_item("each of which a point location in Vor(S) is performed.");
  33.   P.text_item("Terminate the program by clicking the right button.  "); 
  34.   P.text_item("                                                     ");
  35.  
  36.   P.choice_item("INPUT",input,"RANDOM","MOUSE");
  37.  
  38.   P.int_item("GRID",grid_width,0,40,10);
  39.   P.int_item("SITES",N);
  40.   P.int_item("node width",node_width,1,10);
  41.   P.int_item("line width",line_width,1,5);
  42.  
  43.   P.open();
  44.  
  45.  
  46.   W.set_node_width(node_width);
  47.   W.set_line_width(line_width);
  48.  
  49.   real x, y;
  50.   point c;
  51.  
  52.   real R = 1000;  // length of the "infinite rays"
  53.  
  54.   list(point) sites;
  55.  
  56.  
  57.   W.clear();
  58.  
  59.   if (input)
  60.   { W.init(0,1000,0,grid_width);
  61.     while ( W >> c ) 
  62.     { W << c;
  63.       sites.append(c);
  64.      }
  65.    }
  66.   else 
  67.    { init_random();
  68.      while (N--)
  69.      { c = point(random(100,900),random(100,900));
  70.        W << c;
  71.        sites.append(c);
  72.      }
  73.    }
  74.  
  75.   cout << "Computing Voronoi diagram\n";
  76.   newline;
  77.  
  78.   GRAPH(point,point) G;
  79.   edge e;
  80.  
  81.   VORONOI(sites,R,G);
  82.  
  83.  
  84.   // Draw Graph
  85.  
  86.   forall_edges(e,G) W.draw_segment(G[source(e)], G[target(e)]);
  87.  
  88.   cout << "Computing subdivision\n";
  89.   newline;
  90.   
  91.   subdivision(point) S(G);
  92.  
  93.  
  94.   // locate points
  95.  
  96.   W.message("Give qeury points!");
  97.  
  98.  
  99.   W.set_mode(xor_mode);
  100.   face f = nil;
  101.  
  102.   while (W.read_mouse(x,y)!=3)
  103.   { if (f!=nil)                      // delete previously located site
  104.     W.draw_filled_node(S.inf(f));
  105.   
  106.     f = S.locate_point(point(x,y));
  107.   
  108.     W.draw_filled_node(S.inf(f));
  109.     cout << S.inf(f);
  110.     newline;
  111.    }
  112. }
  113.