home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / graphtal / examples / mountain.lsy < prev    next >
Text File  |  1992-11-02  |  2KB  |  69 lines

  1. /*
  2.  * An L-system to generate "mountains". The algorithm
  3.  * follows the subdivision method. We start with a triangle,
  4.  * which we divide up into 6 triangles. 
  5.  *
  6.  * 1992, Christoph Streit
  7.  */
  8.  
  9. lsystem mountain;
  10.  
  11. const subdivisions = 4;
  12.  
  13. table generate {
  14.  
  15.  /*
  16.   * Compute a point in the triangle using barycentric coordinates.
  17.   * The point will be used to subdivide the triangle. A random
  18.   * zOffset is computed also.
  19.   */
  20.  tri(x1,y1,z1,x2,y2,z2,x3,y3,z3, n) : n > 0 -> 
  21.     tri(x1,y1,z1,x2,y2,z2,x3,y3,z3, 
  22.       10*(0.5^(subdivisions-n+1)/2)*gauss(), // zOffset
  23.           rand(), rand(), // random point
  24.           n);
  25.  
  26.  /*
  27.   * Correct the barycentric coordinates of the choosen point.
  28.   */
  29.  tri(x1,y1,z1,x2,y2,z2,x3,y3,z3, offset, a, b, n) : a+b > 1 ->
  30.          tri(x1,y1,z1,x2,y2,z2,x3,y3,z3, offset, 1-a, 1-b, n);
  31.  
  32.  /*
  33.   * Subdivide...
  34.   */
  35.  tri(x1,y1,z1,x2,y2,z2,x3,y3,z3, offset, a, b, n) : n > 0 && !(a+b>1) ->
  36.     tri(x1, y1, z1, 
  37.       (x1+x2)/2, (y1+y2)/2, (z1+z2)/2,
  38.       a*x1+b*x2+(1-a-b)*x3, a*y1+b*y2+(1-a-b)*y3, a*z1+b*z2+(1-a-b)*z3+offset, n-1)
  39.     tri((x1+x2)/2, (y1+y2)/2, (z1+z2)/2,
  40.       x2,y2,z2,
  41.       a*x1+b*x2+(1-a-b)*x3, a*y1+b*y2+(1-a-b)*y3, a*z1+b*z2+(1-a-b)*z3+offset, n-1)
  42.  
  43.     tri(x2,y2,z2,
  44.       (x2+x3)/2, (y2+y3)/2, (z2+z3)/2,
  45.       a*x1+b*x2+(1-a-b)*x3, a*y1+b*y2+(1-a-b)*y3, a*z1+b*z2+(1-a-b)*z3+offset, n-1) 
  46.     tri((x2+x3)/2, (y2+y3)/2, (z2+z3)/2,
  47.       x3,y3,z3,
  48.       a*x1+b*x2+(1-a-b)*x3, a*y1+b*y2+(1-a-b)*y3, a*z1+b*z2+(1-a-b)*z3+offset, n-1)
  49.  
  50.     tri(x3,y3,z3,
  51.       (x3+x1)/2, (y3+y1)/2, (z3+z1)/2,
  52.       a*x1+b*x2+(1-a-b)*x3, a*y1+b*y2+(1-a-b)*y3, a*z1+b*z2+(1-a-b)*z3+offset, n-1) 
  53.     tri((x3+x1)/2, (y3+y1)/2, (z3+z1)/2,
  54.       x1,y1,z1,
  55.       a*x1+b*x2+(1-a-b)*x3, a*y1+b*y2+(1-a-b)*y3, a*z1+b*z2+(1-a-b)*z3+offset, n-1);
  56. };
  57.  
  58. attributes {
  59.  axiom co("SlateGrey") tri(0,0,0, 0,20,0, -10,10,0, subdivisions)
  60.                        tri(0,20,0, -20,20,0, -10,10,0, subdivisions)
  61.                        tri(-20,20,0, -20,0,0, -10,10,0, subdivisions)
  62.                tri(-20,0,0, 0,0,0, -10,10,0, subdivisions);
  63.  derivation generate(infinity);
  64.  
  65.  eye    24, 10, 10;
  66.  lookat  0, 10,  1;
  67.  randomize 20;
  68. };
  69.