# Thread: Can someone help me understand this code for generating fractal?

1. Member
Join Date
Jun 2012
Posts
2
Rep Power
0

## Can someone help me understand this code for generating fractal?

Hello everyone.

I'm trying to understand how this specific Koch curve works through java but I'm quite a beginner to understand some parts of the code. Here is an example I'd like to analyze (not written by me, of course, found it on the internet). I'm studying applied math in informatics and I've just begun learning java, so I'm really interested in this but in the same time kind of lost. Some parts of the code are already commented, but I'd appreciate if some of you could comment the most important parts or at least explain to me in some other way how this specific works. Thanks a lot.

Java Code:
import java.awt.*;
import java.awt.event.*;
public class Koch extends Frame
{  public static void main(String[] args){new Koch();}
Koch()
{  super("Koch. Click the mouse button to increase the level");
{public void windowClosing(
WindowEvent e){System.exit(0);}});
setSize (600, 500);
show();
}
}

class CvKoch extends Canvas
{  public float x, y;
double dir;
int midX, midY, level = 1;

int iX(float x){return Math.round(midX+x);}
int iY(float y){return Math.round(midY-y);}

CvKoch()
{  public void mousePressed(MouseEvent evt)
{  level++; // each mouse click increases the level
repaint();
}
});
}
public void paint(Graphics g)
{  Dimension d = getSize();
int maxX = d.width - 1, maxY = d.height - 1,
length=3* maxX / 4;
midX = maxX/2; midY = maxY/2;
x = (float)(-length/2);          // Start point
y = 0;
dir = 0;
drawKoch(g, length, level);
}

public void drawKoch(Graphics g, double len, int n)
{  if (n==0)
xInc = len * Math.cos(dirRad);    // x increment
yInc = len * Math.sin(dirRad);    // y increment
float x1=x+ (float)xInc,
y1=y+ (float)yInc;
g.drawLine(iX(x), iY(y), iX(x1), iY(y1));
x = x1;
y = y1;
}
else
{  drawKoch(g, len/=3, --n);
dir += 60;
drawKoch(g, len, n);
dir-=120;
drawKoch(g, len, n);
dir += 60;
drawKoch(g, len, n);
}
}
}

2. ## Re: Can someone help me understand this code for generating fractal?

Originally Posted by Eretaia
... but I'd appreciate if some of you could comment the most important parts or at least explain to me in some other way how this specific works.
We're all volunteers here so the brunt of effort should be yours: Better for you to tell us what specifically confuses you rather than have us try to comment "found" code.

3. ## Re: Can someone help me understand this code for generating fractal?

No part of this book may be reproduced without the written permission of the publisher.
Do you have that permission?

If you don't reply within a few hours, this thread will be deleted.

db

4. Member
Join Date
Jun 2012
Posts
2
Rep Power
0

## Re: Can someone help me understand this code for generating fractal?

Originally Posted by Fubarable
We're all volunteers here so the brunt of effort should be yours: Better for you to tell us what specifically confuses you rather than have us try to comment "found" code.
I apologize. I understand the mathematical part, but my scarce knowledge of the java and its syntax is what confuses me on several places. For example, what do these parts mean?

Java Code:
int iX(float x){return Math.round(midX+x);}
int iY(float y){return Math.round(midY-y);}
Thank you for your willingness to help though.

Originally Posted by DarrylBurke
Do you have that permission?

If you don't reply within a few hours, this thread will be deleted.

db
The code is from Computer Graphics for Java Programmers and all the project files can be downloaded freely from the publisher's website and no warning was issued. This code is actually quoted on many websites as an example for a functional Koch curve. However, if it's breaking the rules, feel free to delete it.

5. ## Re: Can someone help me understand this code for generating fractal?

Those statements look like the definitions for two methods. Normally they are coded like this:
Java Code:
int iX(float x){
return Math.round(midX+x);
}
The method takes a float arg and returns an int value.

6. ## Re: Can someone help me understand this code for generating fractal?

Originally Posted by Eretaia
The code is from Computer Graphics for Java Programmers and all the project files can be downloaded freely from the publisher's website and no warning was issued. This code is actually quoted on many websites as an example for a functional Koch curve..
1. Always credit the source when posting copied code on a public forum.
Computer Graphics For Java Programmers, 2Nd Ed - Leen Ammeraal & Kang Zhang - Google Books

2. 'many websites'? A Google search for class CvKoch extends Canvas produces 5 results.

db

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•