Fractal Geometry
Fractal Geometry
ITEPC 06 - Worksho 2
Self-Similarity Property of Fractal
Self similarity across scales
– As one zooms in or out the geometry/image h
as a similar (sometimes exact) appearance
– Types of self-similarity
Exact self similarity
Approximate self similarity
Statistical self similarity
ITEPC 06 - Worksho 3
Exact Self-Similarity
Self similarity may be exact
– Normally only occurs in mathematically defi
ned fractals
– Example:
Koch snowflake
ITEPC 06 - Worksho 4
Exact Self-Similarity
ITEPC 06 - Worksho 5
Approximate Self-Similarity
Structures that are recognizably similar bu
t not exactly so
– More common type of
self-similarity
– Example: Mandelbrot set
ITEPC 06 - Worksho 6
Statistical Self-Similarity
Irregularity is the same on the average
– Example: coastline
ITEPC 06 - Worksho 7
Self-Similarity in Real World
ITEPC 06 - Worksho 8
Successive Refinement of Curves
Very complex curves can be made by repe
atedly refining a simple curve.
The simplest example: Koch curve
– Discovered in 1904 by Helge von Koch, an S
wedish mathematician
– Exactly self-similar fractal
– Fascinating feature: an infinitely long line withi
n a region of finite area
ITEPC 06 - Worksho 9
Koch Curves
K0, K1, K2, …: successive generations of the Koc
h curve
K0: a horizontal line of length unity.
Kn Kn+1: subdivide each segment of Kn into thre
e equal parts and replace the middle part with a
bump in the shape of an equilateral triangle.
ITEPC 06 - Worksho 11
Dimension of Koch Curves?
A line is one dimensional and a plane is
two dimensional, are there “creatures” in
between?
Koch curves are infinite in length (4/3) n,
yet lie inside a finite rectangle
– Their dimension lies somewhere between 1
and 2.
ITEPC 06 - Worksho 12
Fractional Dimension
Self-similar dimension
1D
2D
3D
ITEPC 06 - Worksho 13
Fractional Dimension
Self-similar dimension
– An object has dimension D if, when it is
subdivided into N equal parts, each part must
be made smaller on each side by r = 1 / N1/D.
– Fractional dimension: D = log (N) / log (1 / r)
– Fractional dimension of Koch curve
From one generation to the next, N = 4 segments
are created from each parent segment, but their
lengths are r = 1/3
D = log(4) / log(3) = 1.26
ITEPC 06 - Worksho 14
Drawing Koch Curves
Another view of Koch curves
– Each generation consists of four versions of the previous
generation.
Pseudo-code of drawing Koch curve recursively
To draw Kn:
if (n equals 0) Draw a straight line;
else {
Draw Kn-1;
Perform a transformation;
Draw Kn-1;
Perform a transformation;
Draw Kn-1;
Perform a transformation;
Draw Kn-1;
}
One demo program to draw Koch curves will be covered later.
ITEPC 06 - Worksho 15
OpenGL Programming
Part II
ITEPC 06 - Worksho 18
Transformations in OGL
For each matrix type, OGL maintains a sta
ck of matrices
– Stack: last in, first out
To operate on a specific matrix type,
call glMatrixMode(mode)
– Different modes:
GL_MODELVIEW, GL_PROJECTION
Once the matrix mode is set, perform
various operations on the stack.
ITEPC 06 - Worksho 19
Matrix Stack Operations
glLoadIdentity(): sets the current matrix to the identity matrix.
glLoadMatrix(M): loads a given matrix over the current matrix.
glMultMatrix(M): multiplies the current matrix by a given matrix and r
eplaces the current matrix with this result.
glPushMatrix(): pushes a copy of the current matrix on top the stack.
glPopMatrix(): pops the current matrix off the stack.
ITEPC 06 - Worksho 20
Transformation Pipeline
How do we apply the matrix to some point
we want to transform?
– Answer: OGL do it for you automatically.
ITEPC 06 - Worksho 22
Transformation Example
Assume the matrix mode is GL_MODELVIEW
General, the top of Modelview matrix is not identity
– This represents some more global transformation.
– This will be composed with our rotation transformation.
To avoid destroying the contents of the Modelview
matrix, we may need to save the contents of the M
odelview matrix and restore its contents after we ar
e done.
– glPushMatrix(), glPopMatrix().
ITEPC 06 - Worksho 23
Transformation Example
In OGL, whenever you draw, the points are automa
tically transformed using the current Modelview ma
trix.
Common way of object transformations in OGL
– Push the matrix stack.
– Apply all desired transformations
– Draw your objects (the transformations will be applied au
tomatically)
– Pop the matrix stack.
ITEPC 06 - Worksho 24
Transformation Example
First attempt
– Rotation command
glRotatef(angle_in_degree, x, y, z);
Rotation is performed about the origin of the coordinate system .
– Translation command
glTranslatef(x, y, z);
glPushMatrix(); // save the current matrix
glRotatef(20, 0, 0, 1); // rotate by 20 degrees CCW
glRectf(x-2, y-2, x+2, y+2); // draw the rectangle
glPopMatrix(); // restore the old matrix
equivalent
ITEPC 06 - Worksho 26
World Windows & Viewports
Coordinates in the previous demo examples are
essentially in pixels
– From 0 to some value screenWidth - 1 in x
– From 0 to some value screenHeight - 1 in y
If we want to use coordinates not in terms of pixel
s, say, x varying from [-1, 0.5], how to do it?
Solution: separate the values to describe geometr
ical objects and the size/position of the pictures of
those objects on the display.
ITEPC 06 - Worksho 27
World Windows & Viewports
Define a scene in world coordinates (natural coordinate system for your
problem)
– World window (with clipping)
Viewport: a rectangle in the screen window, showing a snapshot of the
scene through world window
Need a transformation between world window & viewport
ITEPC 06 - Worksho 28
Window to Viewport Mapping
An axis-aligned rectangle another axis-
aligned rectangle
– Distortion may occur (different aspect-ratio)
ITEPC 06 - Worksho 29
Window to Viewport Mapping
Mapping is defined as
– sx = Ax + C
A = (V.r – V.l) / (W.r – W.l)
C = V.l – A W.l
– sy = By + D
B = (V.t – V.b) / (W.t – W.b)
D = V.b – B W.b
Note OGL will do the above mapping for y
ou.
ITEPC 06 - Worksho 30
WV Mapping in OGL
For 2D drawing, world window is set by
– gluOrtho2D(GLdouble left, GLdouble right,
GLdouble bottom, GLdouble top)
– The mapping is performed by a transformation matrix (called projecti
on matrix), internally maintained by OGL.
glMatrixMode(GL_PROJECTION); // set projection matrix
glLoadIdentity(); // initialize to identity
gluOrtho2D(left, right, bottom, top);
ITEPC 06 - Worksho 31
WV Mapping in OGL
Viewport is set by
– glViewport(GLint x, GLint y,
GLint width, GLint height)
– E.g. usually we use the entire window as the viewport
glViewport(0, 0, screen_with, screen_height)
ITEPC 06 - Worksho 32
Window/Viewport Setting Tips
Setting the window automatically
– Let the application determine a good window to use
– Find the bounding box of scene
ITEPC 06 - Worksho 33
Window/Viewport Setting Tips
Automatically setting the viewport to pres
erve the aspect ratio
– Draw the largest undistorted version of a fig
ure fitting in the screen window.
a. glViewport(0, W, 0, W / R);
b. glViewport(0, H * R, 0, h);
ITEPC 06 - Worksho 34
Window/Viewport Setting Tips
Making a matched viewport during resizing
– Register a callback function (e.g. myReshape) f
or the resize event (through glutReshapeFun
c(myReshape))
void myReshape(GLsizei W, GLsizei H)
{
if (R > W / H) // use (global) window aspect ratio
glViewport(0, W, 0, W / R);
else
glViewport(0, H * R, 0, H);
}
ITEPC 06 - Worksho 35
void DrawKoch(int level) { // recursion function
if (level == 0) {
Draw Koch Curves in OGL
(see demo program)
glBegin(GL_LINES);
glVertex2f(0.0, 0.0); glVertex2f(1, 0.0);
glEnd();
}
else{
glPushMatrix();{
glScalef(0.3333333, 0.3333333, 0.3333333);
DrawKoch(level - 1); // first component
glTranslatef(1, 0, 0); // second component
glRotatef(60, 0, 0, 1);
DrawKoch(level - 1);
glTranslatef(1, 0, 0); // third component
glRotatef(-120, 0, 0, 1);
DrawKoch(level - 1);
glTranslated(1, 0, 0); // fourth component
glRotated(60, 0, 0, 1);
DrawKoch(level - 1);
}
glPopMatrix();
}
ITEPC
} 06 - Worksho 36