Fractals
Soon Tee Teoh
CS 116B
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)
Iteration 0 Iteration 1 Iteration 2 Iteration 3
Generator
1/3 1/3
1/3 1/3
1
Fractal Tree
Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5
Generator
Fractal Fern
Generator
Iteration 0 Iteration 1 Iteration 2 Iteration 3
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)
• 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
// 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
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// 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// 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();
}
}
xz
y
h
30o

More Related Content

PDF
Tone deaf: finding structure in Last.fm data
PDF
暗認本読書会5
PDF
Ch20 20
PDF
Prob-Dist-Toll-Forecast-Uncertainty
DOCX
PDF
Christian Gill ''Functional programming for the people''
PDF
Fractal Rendering in Developer C++ - 2012-11-06
PDF
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Tone deaf: finding structure in Last.fm data
暗認本読書会5
Ch20 20
Prob-Dist-Toll-Forecast-Uncertainty
Christian Gill ''Functional programming for the people''
Fractal Rendering in Developer C++ - 2012-11-06
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...

What's hot (6)

PDF
A Novice's Guide to WebGL
PDF
OpenGL L07-Skybox and Terrian
PDF
10CSL67 CG LAB PROGRAM 3
PDF
Tonethompson
PDF
Modular Macros for OCaml
PDF
OpenGL Starter L01
A Novice's Guide to WebGL
OpenGL L07-Skybox and Terrian
10CSL67 CG LAB PROGRAM 3
Tonethompson
Modular Macros for OCaml
OpenGL Starter L01
Ad

Similar to Lecture01 fractals (20)

PPT
PDF
The Day You Finally Use Algebra: A 3D Math Primer
PDF
7.curves Further Mathematics Zimbabwe Zimsec Cambridge
DOCX
Whats u need to graphing polynomials
PDF
2 random variables notes 2p3
PPT
Cascades Demo Secrets
PDF
SA09 Realtime education
PDF
Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...
PPTX
Teknik Simulasi
PDF
[shaderx5] 4.2 Multisampling Extension for Gradient Shadow Maps
PDF
Math lecture 9 (Absolute Value in Algebra)
PDF
chapter5_marked_optimization_with_SGD.pdf
PPT
Lesson all about Derivatives PPT (with examples)
PPT
DerivativesXP.ppt
PPTX
Tmua exam for qualifying entrance for cambridge and oxford
PPT
CS 354 Graphics Math
PDF
267 handout 2_partial_derivatives_v2.60
PDF
Mesh Processing Course : Multiresolution
PPTX
Trident International Graphics Workshop 2014 2/5
PDF
mc-ty-polynomial-2009-1.pdf
The Day You Finally Use Algebra: A 3D Math Primer
7.curves Further Mathematics Zimbabwe Zimsec Cambridge
Whats u need to graphing polynomials
2 random variables notes 2p3
Cascades Demo Secrets
SA09 Realtime education
Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...
Teknik Simulasi
[shaderx5] 4.2 Multisampling Extension for Gradient Shadow Maps
Math lecture 9 (Absolute Value in Algebra)
chapter5_marked_optimization_with_SGD.pdf
Lesson all about Derivatives PPT (with examples)
DerivativesXP.ppt
Tmua exam for qualifying entrance for cambridge and oxford
CS 354 Graphics Math
267 handout 2_partial_derivatives_v2.60
Mesh Processing Course : Multiresolution
Trident International Graphics Workshop 2014 2/5
mc-ty-polynomial-2009-1.pdf
Ad

Recently uploaded (20)

PPT
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PDF
Convolutional neural network based encoder-decoder for efficient real-time ob...
PPTX
Modernising the Digital Integration Hub
DOCX
search engine optimization ppt fir known well about this
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
The influence of sentiment analysis in enhancing early warning system model f...
PDF
OpenACC and Open Hackathons Monthly Highlights July 2025
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PDF
Credit Without Borders: AI and Financial Inclusion in Bangladesh
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
Developing a website for English-speaking practice to English as a foreign la...
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PPTX
The various Industrial Revolutions .pptx
PDF
STKI Israel Market Study 2025 version august
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
Abstractive summarization using multilingual text-to-text transfer transforme...
PDF
Hindi spoken digit analysis for native and non-native speakers
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
Convolutional neural network based encoder-decoder for efficient real-time ob...
Modernising the Digital Integration Hub
search engine optimization ppt fir known well about this
1 - Historical Antecedents, Social Consideration.pdf
The influence of sentiment analysis in enhancing early warning system model f...
OpenACC and Open Hackathons Monthly Highlights July 2025
Taming the Chaos: How to Turn Unstructured Data into Decisions
Credit Without Borders: AI and Financial Inclusion in Bangladesh
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Getting started with AI Agents and Multi-Agent Systems
Developing a website for English-speaking practice to English as a foreign la...
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
The various Industrial Revolutions .pptx
STKI Israel Market Study 2025 version august
A review of recent deep learning applications in wood surface defect identifi...
A contest of sentiment analysis: k-nearest neighbor versus neural network
Abstractive summarization using multilingual text-to-text transfer transforme...
Hindi spoken digit analysis for native and non-native speakers

Lecture01 fractals

  • 2. 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.
  • 3. Koch Fractals (Snowflakes) Iteration 0 Iteration 1 Iteration 2 Iteration 3 Generator 1/3 1/3 1/3 1/3 1
  • 4. Fractal Tree Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 Generator
  • 5. Fractal Fern Generator Iteration 0 Iteration 1 Iteration 2 Iteration 3
  • 6. 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.
  • 7. 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
  • 8. 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; }
  • 10. 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.
  • 11. 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
  • 12. 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(); } } xz y h 30o