# lecture01_fractals

50 %
50 %
Education

Published on December 24, 2008

Author: aSGuest8037

Source: authorstream.com

Fractals : Fractals Soon Tee Teoh CS 116B Fractals : Fractals Fractals are geometric objects. Many real-world objects like ferns are shaped like fractals. Fractals are formed by iterations. Fractals are self-similar. In computer graphics, we use fractal functions to create complex objects. Koch Fractals (Snowflakes) : Koch Fractals (Snowflakes) Iteration 0 Iteration 1 Iteration 2 Iteration 3 Generator 1/3 1/3 1/3 1/3 1 Fractal Tree : Fractal Tree Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 Generator Fractal Fern : Fractal Fern Generator Iteration 0 Iteration 1 Iteration 2 Iteration 3 Add Some Randomness : Add Some Randomness The fractals we’ve produced so far seem to be very regular and “artificial”. To create some realism and variability, simply change the angles slightly sometimes based on a random number generator. For example, you can curve some of the ferns to one side. For example, you can also vary the lengths of the branches and the branching factor. Terrain (Random Mid-point Displacement) : Terrain (Random Mid-point Displacement) Given the heights of two end-points, generate a height at the mid-point. Suppose that the two end-points are a and b. Suppose the height is in the y direction, such that the height at a is y(a), and the height at b is y(b). Then, the height at the mid-point will be: ymid = (y(a)+y(b))/2 + r, where r is the random offset This is how to generate the random offset r: r = srg|b-a|, where s is a user-selected “roughness” factor, and rg is a Gaussian random variable with mean 0 and variance 1 How to generate a random number with Gaussian (or normal) probability distribution : How to generate a random number with Gaussian (or normal) probability distribution // given random numbers x1 and x2 with equal distribution from -1 to 1 // generate numbers y1 and y2 with normal distribution centered at 0.0 // and with standard deviation 1.0. void Gaussian(float &y1, float &y2) { float x1, x2, w; do { x1 = 2.0 * 0.001*(float)(rand()%1000) - 1.0; x2 = 2.0 * 0.001*(float)(rand()%1000) - 1.0; w = x1 * x1 + x2 * x2; } while ( w >= 1.0 ); w = sqrt( (-2.0 * log( w ) ) / w ); y1 = x1 * w; y2 = x2 * w; } Procedural Terrain Example : Procedural Terrain Example Building a more realistic terrain : Building a more realistic terrain Notice that in the real world, valleys and mountains have different shapes. If we have the same terrain-generation algorithm for both mountains and valleys, it will result in unrealistic, alien-looking landscapes. Therefore, use different parameters for valleys and mountains. Also, can manually create ridges, cliffs, and other geographical features, and then use fractals to create detail roughness. Koch fractal : Koch fractal // x0, y0 are the starting coordinates // x1, y1 are the ending coordinates // xout, yout is the outward vector // level is how many iterations to draw this koch fractal. 0 means just draw the line void Koch2D(float x0, float y0, float x1, float y1, float xout, float yout, int level) { float xa, xb, xd, ya, yb, yd; // (xa,ya) is a third of the way // (xb,yb) is two thirds of the way // (xd,yd) is the new point float xmid,ymid, outmag, displacemag; if (level==0) { // if level 0, just draw the line glBegin(GL_LINES); glVertex3f(x0,y0,0.0); glVertex3f(x1,y1,0.0); glEnd(); } else { // otherwise, call Koch2D for the four line-segments xa = x0+0.3333333*(x1-x0); ya = y0+0.3333333*(y1-y0); xb = x0+0.6666666*(x1-x0); yb = y0+0.6666666*(y1-y0); Koch2D(x0,y0,xa,ya,xout,yout,level-1); // draw the first third Koch2D(xb,yb,x1,y1,xout,yout,level-1); // draw the second third outmag = sqrt(xout*xout+yout*yout); displacemag = tan(3.14159/3.0)*sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0))/6.0; xmid = xout*displacemag/outmag; ymid = yout*displacemag/outmag; xd = x0+0.5*(x1-x0)+xmid; yd = y0+0.5*(y1-y0)+ymid; Koch2D(xa,ya,xd,yd,xa+0.5*(xd-xa)-xb,ya+0.5*(yd-ya)-yb,level-1); // protrusion Koch2D(xd,yd,xb,yb,xd+0.5*(xb-xd)-xa,yd+0.5*(yb-yd)-ya,level-1); // protrusion } } (x0,y0) (xa,ya) (xb,yb) (x1,y1) (xd,yd) (xout,yout) L L/6 60 degrees or 3.14159 / 3 radians displacemag Fractal Tree : Fractal Tree // radius, height, iteration void FractalTree(float r, float h, int iter) { GLUquadricObj *optr; // *************** Draw the vertical cylinder ************************************ optr = gluNewQuadric(); gluQuadricDrawStyle(optr,GLU_FILL); glPushMatrix(); glRotatef(-90.0,1.0,0.0,0.0); gluCylinder(optr,r,r,h,10,2); // ptr, rbase, rtop, height, nLongitude, nLatitudes glPopMatrix(); // *************** If more iterations, then recursively draw branches ******** if (iter>0) { glPushMatrix(); glTranslatef(0.0,h,0.0); // translate upwards by h glRotatef(30.0,1.0,0.0,0.0); // rotate about the x axis by 30 degrees FractalTree(0.8*r,0.8*h,iter-1); // draw the next iteration fractal tree glPopMatrix(); glPushMatrix(); glTranslatef(0.0,h,0.0); // translate upwards by h glRotatef(120.0,0.0,1.0,0.0); // rotate about the y axis by 120 degrees glRotatef(30.0,1.0,0.0,0.0); // rotate about the x axis by 30 degrees FractalTree(0.8*r,0.8*h,iter-1); // draw the next iteration fractal tree glPopMatrix(); glPushMatrix(); glTranslatef(0.0,h,0.0); glRotatef(240.0,0.0,1.0,0.0); glRotatef(30.0,1.0,0.0,0.0); FractalTree(0.8*r,0.8*h,iter-1); glPopMatrix(); } } x z y h 30o

 User name: Comment:

June 20, 2018

June 20, 2018

June 20, 2018

June 20, 2018

June 20, 2018

June 20, 2018

## Related pages

### lecture01_fractals - Ace Recommendation Platform - 1

Related Contents; newSimulating Normal Random Variables ating Normal Random Variables Simulation can provide a great deal of information about the behavior ...

### Lecture01 Intro 2up - Instruction manuals - DocFoc.com

Lecture01 Intro 2up. 31. views. on Nov 25, 2015. Extension: PDF. Category: Instruction manuals. Pages: 13 Download: 0. Report. Related. Comments. Description