p_sweep.c
Upload User: gzelex
Upload Date: 2007-01-07
Package Size: 707k
Code Size: 3k
Development Platform:

MultiPlatform

  1. #include <LEDA/plane.h>
  2. #include <LEDA/window.h>
  3. #include <LEDA/prio.h>
  4. #include <LEDA/sortseq.h>
  5. #include <LEDA/p_dictionary.h>
  6. #include <math.h>
  7. static double X_POS;
  8. int compare(segment& s1,segment& s2)
  9. {
  10.   line l1(s1);
  11.   line l2(s2);
  12.   double diff = l1.y_proj(X_POS) - l2.y_proj(X_POS);
  13.   if (fabs(diff) < 1e-10)
  14.      return compare(l1.slope(),l2.slope());
  15.   else 
  16.      return compare(diff,0.0);
  17. }
  18. segment hor_seg(point p) { return segment(p,0,1); }
  19. typedef priority_queue<segment,point> X_structure;
  20. typedef p_dictionary<segment,int> Y_structure; 
  21. typedef sortseq<double,Y_structure>  HISTORY;
  22. void sweep(list<segment>& L, HISTORY& H)
  23. {
  24.   X_structure    X;
  25.   Y_structure    Y;           
  26.   segment s;
  27.   forall(s,L)                     // initialize the X_structure
  28.   { X.insert(s,s.start());
  29.     X.insert(s,s.end());
  30.    }
  31.   // start sweep
  32.   X_POS = -MAXDOUBLE;         
  33.   H.insert(X_POS,Y);              // insert empty Y_structure at -infinity
  34.   while( !X.empty() )
  35.   {
  36.     pq_item it = X.find_min();
  37.     point   p = X.inf(it);        // p is the next transitition point
  38.     segment l = X.key(it);        // l is a segment starting or ending in p
  39.     X.del_item(it);
  40.     X_POS = p.xcoord();           // move the sweep line to p
  41.     if (l.start()==p)             // update Y_structure Y
  42.         Y = Y.insert(l,0);        // left point
  43.     else
  44.         Y = Y.del(l);             // rigth point
  45.     H.insert(X_POS,Y);            // insert Y into history sequence 
  46.   }
  47.   H.insert(MAXDOUBLE,Y);          // insert empty Y_structure at +infinity
  48.   
  49. }
  50. segment locate(point p, HISTORY& H)
  51. {
  52.   X_POS = p.xcoord();
  53.   Y_structure Y = H.inf(H.locate_pred(X_POS));
  54.   p_dic_item pit = Y.succ(hor_seg(p));
  55.   if (pit != nil) 
  56.      return Y.key(pit);
  57.   else
  58.      return segment(0,0,0,0);
  59.   
  60. }
  61. window W;
  62. void draw_sweep_line()
  63. { int save = W.set_line_width(1);
  64.   W.draw_vline(X_POS,green);
  65.   W.set_line_width(save);
  66.  }
  67. void draw_segment(segment s)
  68. { W.draw_segment(s,blue);
  69.  }
  70. void show_segment(segment s)
  71. { draw_segment(s);
  72.   int save = W.set_line_width(5);
  73.   W.draw_segment(s,red);
  74.   W.set_line_width(save);
  75.  }
  76. void hide_segment(segment s)
  77. { int save = W.set_line_width(5);
  78.   W.draw_segment(s,red);
  79.   W.set_line_width(save);
  80.   draw_segment(s);
  81.  }
  82. main()
  83.   HISTORY H;
  84.   segment s;
  85.   point p;
  86.   list<segment> L;
  87.   W.set_mode(xor_mode);
  88.   W.set_line_width(1);
  89.   while (W >> s) 
  90.   { draw_segment(s);
  91.     L.append(s);
  92.    }
  93.  sweep(L,H);
  94.  int key;
  95.  double x,y;
  96.  X_POS = W.xmin();
  97.  L.clear();
  98.  while ((key = W.read_mouse(x,y))!= 3 )  
  99.  {
  100.    forall(s,L) hide_segment(s);           
  101.    draw_sweep_line();
  102.    L.clear();
  103.    switch(key) {
  104.    case 1: L.append(locate(point(x,y),H));
  105.            X_POS = W.xmin();
  106.            break;
  107.    case 2: { X_POS = x;
  108.              draw_sweep_line();
  109.              seq_item sit = H.locate_pred(x);
  110.              Y_structure Y = H.inf(sit);
  111.              p_dic_item it;
  112.              forall_items(it,Y) L.append(Y.key(it));
  113.              break;
  114.             }
  115.    }
  116.    forall(s,L) show_segment(s);           
  117.   }
  118.   
  119.   return 0;
  120. }