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

MultiPlatform

  1. #include <LEDA/window.h>
  2. window W;
  3. double x, y, dx, dy;
  4. void A(int);
  5. void B(int);
  6. void C(int);
  7. void D(int);
  8. void plot(double new_x, double new_y)
  9. { W.draw_segment(x,y,new_x,new_y);
  10.   x = new_x;
  11.   y = new_y;
  12.  }
  13. void A(int i)
  14.   if (i > 0)
  15.   { D(i-1); plot(x-dx,y);
  16.     A(i-1); plot(x,y-dy);
  17.     A(i-1); plot(x+dx,y); 
  18.     B(i-1);
  19.    }
  20.  }
  21. void B(int i)
  22.   if (i > 0)
  23.   { C(i-1); plot(x,y+dy);
  24.     B(i-1); plot(x+dx,y);
  25.     B(i-1); plot(x,y-dy); 
  26.     A(i-1);
  27.    }
  28.  }
  29. void C(int i)
  30.   if (i > 0)
  31.   { B(i-1); plot(x+dx,y);
  32.     C(i-1); plot(x,y+dy);
  33.     C(i-1); plot(x-dx,y); 
  34.     D(i-1);
  35.    }
  36.  }
  37. void D(int i)
  38.   if (i > 0)
  39.   { A(i-1); plot(x,y-dy);
  40.     D(i-1); plot(x-dx,y);
  41.     D(i-1); plot(x,y+dy); 
  42.     C(i-1);
  43.    }
  44.  }
  45. int n = 5;
  46. void hilbert()
  47. {
  48.    double lx = W.xmax() - W.xmin();
  49.    double ly = W.ymax() - W.ymin();
  50.    double x0 = W.xmin() + 0.98*lx;
  51.    double y0 = W.ymin() + 0.98*ly;
  52.    dx = 0.96 * lx/(1 << n);
  53.    dy = 0.96 * ly/(1 << n);
  54.    x = x0;
  55.    y = y0;
  56.    A(n);
  57.    W.draw_segment(x0,y0,x0+dx,y0);
  58.    W.draw_segment(x0+dx,y0,x0+dx,y);
  59.    W.draw_segment(x0+dx,y,x,y);
  60.   }
  61. main()
  62. {   
  63.  panel P("hilbert curve");
  64.  P.int_item("n = ",n,1,10);
  65.  W.set_redraw(hilbert);
  66.  for(;;)
  67.  { P.open();
  68.    W.clear();
  69.    hilbert();
  70.    W.read_mouse();
  71.   }
  72.  
  73.   return 0;
  74. }