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

  1.  
  2. // This programs is using dictionaries with keys of type "point" with two 
  3. // different linear orders, the lexicographic ordering on the cartesian 
  4. // coordinates (default ordering) and the lexicographic ordering on the
  5. // polar coordinates.
  6.  
  7. // must be linked with   libP.a libG.a libL.a -lm   !!!
  8.  
  9.  
  10. #include <LEDA/dictionary.h>
  11. #include <LEDA/plane.h>
  12.  
  13.  
  14. int pol_cmp(point& x, point& y) 
  15. {
  16.   // defines lexicographic order of the polar coordinates
  17.   
  18.   point origin(0,0);
  19.   segment sx(origin,x), sy(origin,y);
  20.   int c = compare(sx.angle(), sy.angle());   // predefined compare(real,real)
  21.   if (c) return c;
  22.   return compare(sx.length(), sy.length());  // predefined compare(int,int) 
  23.   
  24.  }
  25.  
  26.  
  27. declare(POINT,pol_cmp);
  28.  
  29. // Now "POINT(pol_cmp)" is equivalent to the data type  point
  30. // with the linear order defined by "pol_cmp".
  31.  
  32.  
  33. declare2(dictionary,point,int)           // default ordering (by compare)
  34. declare2(dictionary,POINT(pol_cmp),int)  // ordering by pol_cmp
  35.  
  36.  
  37.  
  38. main()
  39. {
  40.   dictionary(point,int)          D;
  41.   dictionary(POINT(pol_cmp),int) D_pol;
  42.  
  43.   point x;
  44.  
  45.   while (cin >> x) 
  46.   { D.insert(x,0);
  47.     D_pol.insert(x,0);
  48.    }
  49.  
  50.   dic_item it;
  51.  
  52.   forall_dic_items(it,D) cout << D.key(it)  << "\n";
  53.   newline;
  54.  
  55.   forall_dic_items(it,D_pol) cout << D_pol.key(it)  << "\n";
  56.   newline;
  57.  
  58. }
  59.