home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Photo CD Demo 1
/
Demo.bin
/
graphtal
/
examples
/
mountain.lsy
< prev
next >
Wrap
Text File
|
1992-11-02
|
2KB
|
69 lines
/*
* An L-system to generate "mountains". The algorithm
* follows the subdivision method. We start with a triangle,
* which we divide up into 6 triangles.
*
* 1992, Christoph Streit
*/
lsystem mountain;
const subdivisions = 4;
table generate {
/*
* Compute a point in the triangle using barycentric coordinates.
* The point will be used to subdivide the triangle. A random
* zOffset is computed also.
*/
tri(x1,y1,z1,x2,y2,z2,x3,y3,z3, n) : n > 0 ->
tri(x1,y1,z1,x2,y2,z2,x3,y3,z3,
10*(0.5^(subdivisions-n+1)/2)*gauss(), // zOffset
rand(), rand(), // random point
n);
/*
* Correct the barycentric coordinates of the choosen point.
*/
tri(x1,y1,z1,x2,y2,z2,x3,y3,z3, offset, a, b, n) : a+b > 1 ->
tri(x1,y1,z1,x2,y2,z2,x3,y3,z3, offset, 1-a, 1-b, n);
/*
* Subdivide...
*/
tri(x1,y1,z1,x2,y2,z2,x3,y3,z3, offset, a, b, n) : n > 0 && !(a+b>1) ->
tri(x1, y1, z1,
(x1+x2)/2, (y1+y2)/2, (z1+z2)/2,
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)
tri((x1+x2)/2, (y1+y2)/2, (z1+z2)/2,
x2,y2,z2,
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)
tri(x2,y2,z2,
(x2+x3)/2, (y2+y3)/2, (z2+z3)/2,
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)
tri((x2+x3)/2, (y2+y3)/2, (z2+z3)/2,
x3,y3,z3,
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)
tri(x3,y3,z3,
(x3+x1)/2, (y3+y1)/2, (z3+z1)/2,
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)
tri((x3+x1)/2, (y3+y1)/2, (z3+z1)/2,
x1,y1,z1,
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);
};
attributes {
axiom co("SlateGrey") tri(0,0,0, 0,20,0, -10,10,0, subdivisions)
tri(0,20,0, -20,20,0, -10,10,0, subdivisions)
tri(-20,20,0, -20,0,0, -10,10,0, subdivisions)
tri(-20,0,0, 0,0,0, -10,10,0, subdivisions);
derivation generate(infinity);
eye 24, 10, 10;
lookat 0, 10, 1;
randomize 20;
};