Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By DarrylBurke

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

  1. #1
    Eretaia is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default 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");
          addWindowListener(new WindowAdapter()
             {public void windowClosing(
                     WindowEvent e){System.exit(0);}});
          setSize (600, 500);
          add("Center", new CvKoch());
          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()
       {  addMouseListener(new MouseAdapter()
          {  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)
          {  double dirRad, xInc, yInc;
             dirRad = dir * Math.PI/180;
             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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

    Quote Originally Posted by Eretaia View Post
    ... 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. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default 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
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Eretaia is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

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

    Quote Originally Posted by Fubarable View Post
    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.

    Quote Originally Posted by DarrylBurke View Post
    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. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

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

    Quote Originally Posted by Eretaia View Post
    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. I don't understand the code help!
    By gomdohri in forum New To Java
    Replies: 3
    Last Post: 10-15-2011, 02:58 PM
  2. Trying to understand this code better
    By Kimomaru in forum New To Java
    Replies: 2
    Last Post: 09-28-2011, 10:44 PM
  3. Can any one help me to understand the Code
    By soomroimran in forum New To Java
    Replies: 2
    Last Post: 04-28-2011, 09:23 AM
  4. Generating activation code
    By yalgashev in forum New To Java
    Replies: 5
    Last Post: 01-18-2011, 07:40 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

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