triang.c
上传用户:gzelex
上传日期:2007-01-07
资源大小:707k
文件大小:2k
开发平台:

MultiPlatform

  1. #include <LEDA/plane.h>
  2. #include <LEDA/d_array.h>
  3. #include <LEDA/graph.h>
  4. #include <LEDA/window.h>
  5. window W;
  6. void TRIANG(list<point> L, GRAPH<point,int>& G)
  7.   if (L.length() < 3) return;
  8.   list<point> CH;
  9.   list_item last;
  10.   L.sort();  // sort points lexicographically
  11.   d_array<point,node> V(nil);
  12.   // initialize convex hull with first two points
  13.   point p = L.pop();
  14.   V[p] = G.new_node(p);
  15.   CH.append(p);
  16.   while (L.head() == p) L.pop();
  17.   point q = L.pop();
  18.   last = CH.append(q);
  19.   V[q] = G.new_node(q);
  20.   G.new_edge(V[p],V[q]);
  21.   // scan remaining points
  22.   forall(p,L)
  23.   {
  24.     if (p == CH[last]) continue;  // multiple point
  25.     node v = G.new_node(p);
  26.     V[p] = v;
  27.     G.new_edge(v,V[CH[last]]);
  28.     // compute upper tangent (p,up)
  29.     list_item up = last;
  30.     list_item it = CH.cyclic_succ(up);
  31.     while (left_turn(CH[it],CH[up],p))
  32.     { up = it;
  33.       it = CH.cyclic_succ(up);
  34.       G.new_edge(v,V[CH[up]]);
  35.      }
  36.     // compute lower tangent (p,low)
  37.     list_item low = last;
  38.     it = CH.cyclic_pred(low);
  39.     while (right_turn(CH[it],CH[low],p))
  40.     { low = it;
  41.       it = CH.cyclic_pred(low);
  42.       G.new_edge(v,V[CH[low]]);
  43.      }
  44.     // remove all points between up and low
  45.     if (up != low)
  46.     { it = CH.cyclic_succ(low);
  47.       while (it != up)
  48.       { CH.del(it);
  49.         it = CH.cyclic_succ(low);
  50.        }
  51.      }
  52.     // insert new point
  53.     last = CH.insert(p,low);
  54.    }
  55. }
  56.      
  57. main()
  58. {
  59.   //window W;
  60.   W.init(-100,100,-100);
  61.   W.set_node_width(5);
  62.   int N = 500;
  63.   panel P("triangulation");
  64.   P.int_item("# points",N,1,2000);
  65.   int b1 = P.button("mouse");
  66.   int b2 = P.button("random");
  67.   int b3 = P.button("quit");
  68.   for(;;)
  69.   { 
  70.     list<point> L;
  71.     point p,q;
  72.     int but = P.open();
  73.     W.clear();
  74.     if (but == b1)
  75.       while (W >> p)
  76.       { W.draw_point(p,blue);
  77.         L.append(p);
  78.        }
  79.     if (but == b2)
  80.       for(int i = 0; i<N; i++) 
  81.       { point p(rand_int(-90,90),rand_int(-90,90));
  82.         W.draw_point(p,blue);
  83.         L.append(p);
  84.        }
  85.     if (but == b3) break;
  86.   
  87.     GRAPH<point,int> G;
  88.     TRIANG(L,G);
  89.   
  90.     edge e;
  91.     forall_edges(e,G)
  92.        W.draw_segment(G[source(e)],G[target(e)],violet);
  93.   }
  94.    
  95.  return 0;
  96. }