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

  1. #include <LEDA/point_set.h>
  2. #include <LEDA/window.h>
  3.  
  4. declare(point_set,string)
  5.  
  6. void draw_polygon(window& W, point_set(string)& S, list(ps_item)& P)
  7. { list_item it;
  8.   forall_items(it,P)
  9.   { point p =  S.key(P[it]);
  10.     point q =  S.key(P[P.cyclic_succ(it)]);
  11.     W.draw_segment(p,q);
  12.    }
  13.  }
  14.  
  15. main()
  16. {
  17.  
  18.   window W;
  19.  
  20.  
  21.   W.set_node_width(5);
  22.   W.set_line_width(2);
  23.   W.set_mode(xor_mode);
  24.  
  25.   panel P("POINT SET");
  26.  
  27.   P.text_item("Use the left mouse button to insert points. If a shift ");
  28.   P.text_item("key is pressed simultaneously the nearest neighbor of  ");
  29.   P.text_item("the current mouse position is computed and displayed.  ");
  30.   P.text_item("An iso-oriented query rectangle can by defined using   ");
  31.   P.text_item("the middle button twice. An orthogonal range query is  ");
  32.   P.text_item("performed and all points lying inside the rectangle    ");
  33.   P.text_item("are deleted. Click the right button to quit.           ");
  34.  
  35.   P.button("continue");
  36.  
  37.   P.open();
  38.  
  39.  
  40.   W.clear();
  41.  
  42.   point_set(string) S;
  43.  
  44.   real          x,y,x1,y1;
  45.   int           button=0;
  46.   ps_item       nearest_it=nil;
  47.   point         p; 
  48.   list(ps_item) Pol;
  49.  
  50.  
  51.  
  52.   while (button !=3 )
  53.   {  
  54.      button =  W.read_mouse(x,y);
  55.  
  56.      // delete arrow to nearest neighbor or polygon
  57.  
  58.      if (nearest_it!=nil) 
  59.       { W.draw_edge_arrow(p,S.key(nearest_it));
  60.         nearest_it = nil;
  61.        }
  62.  
  63.      if (!Pol.empty()) 
  64.      { draw_polygon(W,S,Pol); 
  65.        Pol.clear();
  66.       }
  67.  
  68.      p = point(x,y);
  69.  
  70.      switch(button) {
  71.  
  72.      case 1: { string s = form("point (%f,%f)",p.xcoord(),p.ycoord());
  73.                S.insert(p,s);
  74.                W.draw_filled_node(p);
  75.                break;
  76.               }
  77.  
  78.  
  79.      case 2: { W.read_mouse_rect(x,y,x1,y1);
  80.                list(ps_item) L = S.range_search(x,x1,y,y1);
  81.                ps_item it;
  82.                forall(it,L) 
  83.                { cout << "delete " << S.inf(it) << "\n";
  84.                  W.draw_filled_node(S.key(it));
  85.                  S.del_item(it);
  86.                 }
  87.                cout.flush();
  88.                break;
  89.               }
  90.  
  91.      case -1:  { nearest_it = S.nearest_neighbor(p);
  92.                  if (nearest_it!=nil) 
  93.                   { W.draw_edge_arrow(p,S.key(nearest_it));
  94.                     cout << "Nearest " << S.inf(nearest_it) << "\n";
  95.                    }
  96.                  else cout << "Empty point set.\n";    
  97.                  cout.flush();
  98.                  break;
  99.                 }
  100.  
  101.      case -2:  { Pol = S.convex_hull();
  102.                  draw_polygon(W,S,Pol);
  103.                  break;
  104.                 }
  105.  
  106.      } //switch
  107.  
  108.  
  109. } //for
  110.  
  111. }
  112.