dr. b.raghu professor / cse sri ramanujar engineering college

76
Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Upload: kristian-kelley-rice

Post on 18-Jan-2016

228 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Dr. B.Raghu Professor / CSE

Sri Ramanujar Engineering College

Page 2: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

TopicsWhy fractals?Fractal DimensionMandelbrot SetFractal Applications

04/21/23 Dr. B. Raghu Professor/CSE 2

Page 3: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Fractals?Fractal geometry of nature and chaos

The “middle ground of ‘organized’ or ‘orderly’ geometric chaos” (Mandelbrot 1989)

Derived partly “because of the difficulty in analyzing spatial forms and processes” (Lam and Quattrochi 1992)

Pack an infinite amount of length into a small finite area, similar to the coastline scale issue (Flake 1998).A 1 inch Koch curve at 100 iterations would

stretch around the earth 40,000 times.

04/21/23 Dr. B. Raghu Professor/CSE 3

Page 4: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Fractal Dimension

04/21/23 Dr. B. Raghu Professor/CSE 4

1 Dimension (line) 2=21

2 Dimensions (square) 4=22

Page 5: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

04/21/23 Dr. B. Raghu Professor/CSE 5

3 Dimensions (cube) 8=23

1 Dimension1 Dimension 2=22=211

2 Dimensions2 Dimensions 4=24=222

3 Dimensions3 Dimensions 8=28=233

d Dimensionsd Dimensions n=2n=2dd

Number of copies made by doubling

= 2dimension

Page 6: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

04/21/23 Dr. B. Raghu Professor/CSE 6

Fractal Dimension (Sierpinski Triangle)

3=2?

1 Dimension1 Dimension 2=22=211

F DimensionF Dimension 3=23=2??

2 Dimensions2 Dimensions 4=24=222

3 Dimensions3 Dimensions 8=28=233

d Dimensionsd Dimensions n=2n=2dd

Page 7: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Calculating Dimension with LogFor a, the original

segment is ¼ of the line length.

For b, the original square is ½ of its side.

For c, the original curve is 1/3 of the length of the iterated curve.

04/21/23 Dr. B. Raghu Professor/CSE 7

(Lam and Quattrochi 1992)(Lam and Quattrochi 1992)

Page 8: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Fractal DimensionIn fractal geometry, fractal curves are

between dimension 1 and 2, and fractal surfaces are between dimension 2 and 3.

Coastlines typically are about fractal dimension of 1.2, and their relief dimension is about 2.2.

Fractal dimensions of 1.5 and 2.5 are too large, or irregular, for modeling earth features.

Fractal dimension is an indicator of complexity (Zhou and Lam 2005)Image Characterization and Measurement

System (ICAMS) for dimension (Ibid)04/21/23 Dr. B. Raghu Professor/CSE 8

Page 9: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

The Mandelbrot SetXt+1 = xt

2 + c, where c = some imaginary number.Ex. – for c = i, x0 = 0

X1 = 0 + i

X2 = i2 + i » -1 + i

X3 = (-1 + i)2 + i » i2 – 2i + 1 + i » -i …..

Points within the Mandelbrot set are black.

04/21/23 Dr. B. Raghu Professor/CSE 9

Page 10: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Fractal ApplicationsCategorization of phenomena using

dimension similarity.Simulation (coast lines, stream patterns,

surfaces and terrain, etc.)Simulated landscapes have been used to model

habitat destruction (Malanson 2002).Art

04/21/23 Dr. B. Raghu Professor/CSE 10

Page 11: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Fractal Art

04/21/23 Dr. B. Raghu Professor/CSE 11

Page 12: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Fractals and HollywoodStar Trek – Genesis

EffectStar Wars – Death

StarThe Last Starfighter

– LandscapesThe Perfect StormApollo 13Titanic

04/21/23 Dr. B. Raghu Professor/CSE 12

Page 13: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Fractal geometry

Effective way to represent natural objectsRepresents phenomenon w/ concepts of

fragmentation small chunks

Self Similarity similar small building blocks used repeatedly

Page 14: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Fractal pattern generation

Initiatorthe basic element

Repetitorway in which basic element is repeatedly

appliedImportant idea is recursion

I.e. do againfor the definition of recursion - see recursion

Page 15: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Fractal patterns can beDeterministic

Repetitor is same each timeStochastic

some part of repetitor varies randomly

Page 16: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Space filling curvesfirstly - whyneed ways to refer to specific tiles (cells)

in a matrixone (obvious) way is by x and y

coordinates

0

1

2

3

0 1 2 3

Page 17: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Problem w/ obvious wayrequires storage of two numberscannot determine neighbors w/out

computationalternative would permit easy spatial

indexing

Page 18: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Alternatives to x,yrow orderrow prime orderCantor diagonal orderspiral orderwe are generally concerned w/ “space filling curves”

Page 19: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Peano orderingPeano ordering permits use of a key to stand

for 2 (or more - if object is volume) dimensions

Peano key is derived using bit interleaving

0

1

2

0 1 2 3

Page 20: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Bit interleaving

0

1

2

0 1 2 3

3

00 01 10 11

X 0 0 1 1 (3) Y 0 0 1 0 (2)

X

0 0 0 0 1 1 1 0

Page 21: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Topologyrelationships in space based on relative

positionsabsolute position

Thing A is at x=4, y=5 and thing B is at x=3, y=7

relative positionthing A is near thing B

removal of descriptive geometry

Page 22: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Graphsnot x/y graphsbut topological graphs they reflect relationshipselements

intersections or endpoints of lines (vertices)lines called edges (but sometimes arcs or

chains)separate links or disconnected sets of lines

called subgraphsvacant spaces (faces) between or outside edges

Page 23: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Isomorphic graphs

two graphs are isomorphic if there is a one-to-one correspondence between edges and vertices

shapes can be quite different

Page 24: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Graph types

loop, circuit or cyclic graphsone vertex connected to itself without the need

for traversing one edge in both directions highway systems, electrical systems

tree graphgraphs without loops or cycles

rivers

directed acyclic graphno circuit but have directed edge(s)directed edge is simply an edge with direction

associated sewer lines

Page 25: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Some special tree types

spanning treeeach vertex must be connected to at least one

other vertex

traveling salesmanmust be able to return to starting point w/out

retracing an edge

radial treesall peripheral vertices connect to central vertex

Page 26: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Properties of graphsdegree of a vertex

number of edges that connectfor directed graphs number of in and out edges

these numbers can be used to assess the overall character of a networkminimum/max degree, average degree etc.

can use these measures to compute Euler number

Page 27: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Euler numberV + F = E + Swhere

V is total number of verticesE is total number of edgesF is number of facesS (or G) is Euler number

if area outside graph is a face S = 2 otherwise 1

Page 28: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Computation of Euler numbers S= 1 if “outside” is not counted as a face and S=2 if “outside” is counted as a face

V=4, E=4, F=2, S=2V=6, E=5, F=2, S=1

V=4, F=2, E=5, S=1

V=9, F=3 E=11, S=1

Page 29: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

• FRACTAL LANDSCAPES

• FRACTAL IMAGE COMPRESSION

Page 30: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Dr. B.Raghu Professor / CSE

Sri Ramanujar Engineering College

Page 31: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Review:

Two important properties of a fractal F

• F has detail at every level.

• F is exactly, approximately or statistically self-similar.

Page 32: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Fractals are now used in many forms to create textured landscapes and other intricate models. It is possible to create all sorts of realistic fractal forgeries, images of natural scenes, such as lunar landscapes, mountain ranges and coastlines. This is seen in many special effects within Hollywood movies and also in television advertisements.

Fractal Landscapes

A fractal landscape created by Professor Ken Musgrave (Copyright: Ken Musgrave)

A fractal planet.

Page 33: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Simulation process

First:

Already known

The average of the 2 neighbor blue points plus r

The average of the 4 neighbor red points plus r

This random value r is normally distributed and scaled by a factor d1 related to the original d by

d is the scale constant.H is the smoothness constant.

/ 21 1

1~ (0, ) ( )

2Hr N d d d

Page 34: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

We can now carry out exactly the same procedure on each of the four smaller squares, continuing for as long as we like, but where we make the scaling factor at each stage smaller and smaller; by doing this we ensure that as we look closer into the landscape, the 'bumps' in the surface will become smaller, just as for a real landscape. The scaling factor at stage n is dn, given by

/ 21( )2

nHnd d

Simulation result by MATLAB

Using a 64x64 grid and

H=1.25d=15

Page 35: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College
Page 36: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

FRACTAL IMAGE COMPRESSIONWhat is Fractal Image Compression ?

The output images converge to the Sierpinski triangle. This final image is called attractor for this photocopying machine. Any initial image will be transformed to the attractor if we repeatedly run the machine.

On the other words, the attractor for this machine is always the same image without regardless of the initial image. This feature is one of the keys to the fractal image compression.

Page 37: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

How can we describe behavior of the machine ? Transformations of the form as follows will help us.

Such transformations are called affine transformations. Affine transformations are able to skew, stretch, rotate, scale and translate an input image.

M.Barnsley suggested that perhaps storing images as collections of transformations could lead to image compression.

i i ii

i i i

a b ex xw

c d fy y

Page 38: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Iterated Function Systems ( IFS )

An iterated function system consists of a collection of contractive affine transformations.

For an input set S, we can compute wi for each i, take the union of these sets, and get a new set W(S).

Hutchinson proved that in IFS, if the wi are contractive, then W is contractive, thus the map W will have a unique fixed point in the space of all images. That means, whatever image we start with, we can repeatedly apply W to it and our initial image will converge to a fixed image. Thus W completely determine a unique image.

1

(*) (*)n

ii

W w

( )| | lim ( )nox

W f W f

Page 39: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Self-Similarity in Images

We define the distance of two images by:

Original Lena image Self-similar portions of the image

JAVA

( , )( , ) sup | ( , ) ( , ) |

x y Pf g f x y g x y

where f and g are value of the level of grey of pixel, P is the space of the image

Page 40: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Affine transformation mentioned earlier is able to "geometrically" transform part of the image but is not able to transform grey level of the pixel. so we have to add a new dimension into affine transformation

Here si represents the contrast, oi the brightness of the transformation

For encoding of the image, we divide it into:

• non-overlapped pieces (so called ranges, R)

• overlapped pieces (so called domains, D)

0

0

0 0

i i i

i i i i

i i

x a b x e

w y c d y f

z s z o

Page 41: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Encoding Images

Suppose we have an image f that we want to encode. On the other words, we want to find a IFS W which f to be the fixed point of the map W

We seek a partition of f into N non-overlapped pieces of the image to which we apply the transforms wi and get back f.

We should find pieces Di and maps wi, so that when we apply a wi to the part of the image over Di , we should get something that is very close to the any other part of the image over Ri .

Finding the pieces Ri and corresponding Di by minimizing distances between them is the goal of the problem.

Page 42: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Decoding Images

The decoding step is very simple. We start with any image and apply the stored affine transformations repeatedly till the image no longer changes or changes very little. This is the decoded image.

First three iterations of the decompression of the image of an eye from an initial solid grey image

Page 43: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

An Example

Suppose we have to encode 256x256 pixel grayscale image.

let R1~R1024 be the 8x8 pixel non-overlapping sub-squares of the image, and let D be the collection of all overlapping 16x16 pixel sub-squares of the image (general, domain is 4 times greater than range). The collection D contains (256-16+1) * (256-16+1) = 58,081 squares. For each Ri search through all of D to find Di with minimal distances

To find most-likely sub-squares we have to minimize distance equation. That means we must find a good choice for Di that most looks like the image above Ri (in any of 8 ways of orientations) and find a good contrast si and brightness oi. For each Ri , Di pair we can compute contrast and brightness using least squares regression.

Page 44: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Fractal Image Compression versus JPEG Compression

Original Lena image (184,320 bytes)

JPEG-max. quality (32,072)comp. ratio: 5.75:1

FIF-max. quality (30,368)comp. ratio: 6.07:1

Page 45: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

One very important feature of the Fractal Image Compression is Resolution Independence.

When we want to decode image, the only thing we have to do is apply these transformations on any initial image. After each iteration, details on the decoded image are sharper and sharper. That means, the decoded image can be decoded at any size.

So we can zone in the image on the larger sizes without having the "pixelization" effect..

Resolution Independence

               

Lena's eyeoriginal image enlarged to 4

times

Lena's eyedecoded at 4 times its encoding

size

Page 46: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

FractalsInfinite detail at every pointSelf similarity between parts and overall features

of the objectZoom into Euclidian shape

Zoomed shape see more detail eventually smooths

Zoom in on fractalSee more detail

Does not smoothModel

Terrain, clouds water, trees, plants, feathers, fur, patterns

General equation P1=F(P0), P2 = F(P1), P3=F(P2)…P3=F(F(F(P0)))

Page 47: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Self similar fractalsParts are scaled down versions of the entire

objectuse same scaling on subpartsuse different scaling factors for subparts

Statistically self-similarApply random variation to subparts

Trees, shrubs, other vegetation

Page 48: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Fractal typesStatistically self-affine

random variations Sx<>Sy<>Sz

terrain, water, clouds

Invariant fractal setsNonlinear transformations

Self squaring fractals Julia-Fatou set

Squaring function in complex space Mandelbrot set

Squaring function in complex space Self-inverse fractals

Inversion procedures

Page 49: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Julia-Fatou and Mandelbrot

x=>x2+c x=a+bi

Complex numberModulus

Sqrt(a2+b2) If modulus < 1

Squaring makes it go toward 0

If modulus > 1 Squaring falls towards

infinity If modulus=1

Some fall to zero Some fall to infinity Some do neither

Boundary between numbers which fall to zero and those which fall to infinity Julia-Fatou Set

Foley/vanDam Computer Graphics-Principles and Practices, 2nd edition

Julia-Fatou

Page 50: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Julia Fatou and Mandelbrot con’d

Shape of the Julia-Fatou set based on c

To get Mandelbrot set – set of non-diverging pointsCorrect method

Compute the Julia sets for all possible c

Color the points black when the set is connected and white when

it is not connected

Approximate method Foreach value of c, start with complex number 0=0+0i

Apply to x=>x2+c Process a finite number of times (say 1000)

If after the iterations is is outside a disk defined by

modulus>100, color the points of c white, otherwise color it

black.

Page 51: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

04/21/23 Dr. B. Raghu Professor/CSE 51

Foley/vanDam Computer Graphics-Principles and Practices, 2nd edition

Page 52: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Constructing a deterministic self-similar fractal

InitiatorGiven geometric shape

GeneratorPattern which replaces subparts of initiator

Koch Curve

Initiator generatorFirst iteration

Page 53: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Fractal dimensionD=fractal dimension

Amount of variation in the structureMeasure of roughness or fragmentation of the

object Small d-less jagged Large d-more jagged

Self similar objectsnsd=1 (Some books write this as ns-d=1)

s=scaling factor n number of subparts in subdivision d=ln(n)/ln(1/s)

[d=ln(n)/ln(s) however s is the number of segments versus how much the main segment was reduced I.e. line divided into 3 segments. Instead of saying the

line is 1/3, say instead there are 3 sements. Notice that 1/(1/3) = 3]

If there are different scaling factors Sk

d=1K=1

n

Page 54: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Figuring out scaling factorsI prefer: ns-d=1 :d=ln(n)/ln(s)

Dimension is a ratio of the (new size)/(old size)Divide line into n

identical segments n=s

Divide lines on square into small squares by dividing each line into n identical segments n=s2 small squares

Divide cube Get n=s3 small cubes

Koch’s snowflakeAfter division have 4

segments n=4 (new segments) s=3 (old segments) Fractal Dimension

D=ln4/ln3 = 1.262

For your reference: Book method n=4

Number of new segments s=1/3

segments reduced by 1/3 d=ln4/ln(1/(1/3))

Page 55: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Sierpinski gasket Fractal Dimension

Divide each side by 2Makes 4 trianglesWe keep 3Therefore n=3

Get 3 new triangles from 1 old triangle

s=2 (2 new segments from one old segment)

Fractal dimension D=ln(3)/ln(2) = 1.585

Page 56: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Cube Fractal Dimension

Apply fractal algorithmDivide each side by 3Now push out the middle face of each cubeNow push out the center of the cube

What is the fractal dimension?Well we have 20 cubes, where we used to have

1 n=20

We have divided each side by 3 s=3

Fractal dimension ln(20)/ln(3) = 2.727

Image from Angel book

Page 57: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Language Based Models of generating images

Typical Alphabet {A,B,[,]}

RulesA=> AAB=> A[B]AA[B]

Starting Basis=BGenerate words

Represents sequence of segments in graph structure

Branch with brackets

Interesting, but I want a tree

BA[B]AA[B]AA[A[B]AA[B]]AAAA[A[B]AA[B]]

A

AA

B

B

A

AAB

AA

B

AAAA

A

B

AA

B

Page 58: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Language Based Models of generating images con’d

Modify Alphabet {A,B,[,],(,)}

RulesA=> AAB=> A[B]AA(B) [] = left branch () =

right branchStarting Basis=B

Generate wordsRepresents sequence of

segments in graph structure

Branch with brackets

BA[B]AA(B)AA[A[B]AA(B)]AAAA(A[B]AA(B))

A

AA

B

B

A

AAB

AA

B

AAAA

AB

AA

B

Page 59: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Language Based models have no inherent geometryGrammar based model

requiresGrammarGeometric interpretation

Generating an object from the word is a separate processexamples

Branches on the tree drawn at upward angles

Choose to draw segments of tree as successively smaller lengths The more it branches, the

smaller the last branch is Draw flowers or leaves at

terminal nodes

A

AAB

AA

B

AAAA

A

B

AA

B

Page 60: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Grammar and GeometryChange branch size according to depth of graph

Foley/vanDam Computer Graphics-Principles and Practices, 2nd edition

Page 61: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Particle SystemsSystem is defined by a collection of particles

that evolve over timeParticles have fluid-like properties

Flowing, billowing, spattering, expanding, imploding, exploding

Basic particle can be any shape Sphere, box, ellipsoid, etc

Apply probabilistic rules to particles generate new particles Change attributes according to age

What color is particle when detected? What shape is particle when detected? Transparancy over time?

Particles die (disappear from system) Movement

Deterministic or stochastic laws of motion Kinematically forces such as gravity

Page 62: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Particle Systems modelingModel

Fire, fog, smoke, fireworks, trees, grass, waterfall, water spray.

GrassModel clumps by setting up trajectory paths for

particlesWaterfall

Particles fall from fixed elevation Deflected by obstacle as splash to ground

Eg. drop, hit rock, finish in pool Drop, go to bottom of pool, float back up.

Page 63: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Physically based modelingNon-rigid object

Rope, cloth, soft rubber ball, jelloDescribe behavior in terms of external and internal

forcesApproximate the object with network of point nodes

connected by flexible connection Example springs with spring constant k

Homogeneous object All k’s equal

Hooke’s LawFs=-k x

x=displacement, Fs = restoring force on springCould also model with putty (doesn’t spring back)Could model with elastic material

Minimize strain energy

kk

k

k

Page 64: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

“Turtle Graphics”Turtle can F=Move forward a unitL=Turn leftR=Turn right

Stipulate turtle directions, and angle of turns

Equilateral triangleEg. angle =120FRFRFR

What if change angle to 60 degreesF=> FLFRRFLFBasis F

Koch Curve (snowflake)Example taken from Angel

book

Page 65: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Using turtle graphics for treesUse push and pop for side

branches []F=> F[RF]F[LF]FAngle =27Note spaces ONLY for

readabilityF[RF]F[LF]F

[RF[RF]F[LF]F] F[RF]F[LF]F [LF[RF]F[LF]F] F[RF]F[LF]F

Page 66: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

FractalsFractals 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.

Page 67: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Iteration 0 Iteration 1 Iteration 2 Iteration 3

Generator1/3 1/3

1/3 1/3

1

Page 68: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5

Generator

Page 69: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Generator

Iteration 0 Iteration 1 Iteration 2 Iteration 3

Page 70: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Add Some RandomnessThe 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.

Page 71: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

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

Page 72: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

// 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;

}

Page 73: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College
Page 74: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

Building a more realistic terrainNotice 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.

Page 75: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

// 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 linevoid 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 or3.14159 / 3 radians

displacemag

Page 76: Dr. B.Raghu Professor / CSE Sri Ramanujar Engineering College

// radius, height, iterationvoid 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