/*********************************************************************************************/
/*                                                                                           */
/*  Java Image Demo - Random Square Fractal Pattern                                          */
/*                    Koch Snowflake                                                         */
/*  Auth: bkenwright@xbdev.net                                                               */
/*  URL: www.xbdev.net                                                                       */
/*                                                                                           */
/*  Ben Kenwright (bkenwright@xbdev.net)                                                     */
/*                                                                                           */
/*********************************************************************************************/
/*
  Details:
  This is a rather nifty idea if I say so myself.  Its a Koch Snowflake, and the principles
  behind it are easier than you think.
  To draw the simple snowflake you start by considering a square with a side length of 4 to the
  power of n...so for example, 4, 16, 64..etc.  So if you start with our 4x4 side square, this
  means that our square can be divided into 16 smaller squares.  And if we have a larger area
  square this can be sub-divided into smaller 4x4 squares.
  Now if we take our square and number the parts 0 to 15 starting at the top left.
  i.e.
          0   1   2   3
          4   5   6   7
          8   9   10  11
          12  13  14  15
  
  We can rearrange some of these square, 1,7,8 and 14 to produce a different pattern.
  Our fractal recursive function goes through our pixel data until it gets to squares of
  size 4x4 and rearranges them, until finally all the squares have been rearranged to produce
  our fractal pattern.
  
*/
/*********************************************************************************************/

//HOW TO COMPILE FOR JAVA 1.1.

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//
//  C:>javac -target 1.1 fractals.java
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

 
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.awt.*;
import java.applet.Applet;



class pixelvector
{
  public int x;
  public int y;
};//End pixelvector class




public class fractals extends java.applet.Applet 
{
   Image img;
   Graphics backg;

   int a[];
   int b[];

   int data[] = {0,0,         2,0,   3,0,
                 0,1,   1,1,  2,1,
                        1,2,  2,2,   3,2,
                 0,3,   1,3,         3,3,
                 2,-1,  4,2,  -1,1,  1,4 };


   public void paint(Graphics g) 
   {
      g.drawImage( img , 0, 0, this );
   }// End of paint(..)



   // Recursive function
   // Called initially from init()
   public void fractal(pixelvector offset, int sqsize) 
   {
     pixelvector newoffset = new pixelvector();
     int i;
     for(i=0; i<16; i++)
     {
        newoffset.x=offset.x+a[i]*sqsize;
        newoffset.y=offset.y+b[i]*sqsize;
        if(sqsize==1 || sqsize==0) 
              backg.drawLine(newoffset.x,newoffset.y, newoffset.x,newoffset.y);
        else 
              fractal(newoffset, sqsize/4);
     }//End for loop i
   }//End fractal(..)

   
   public void init()
   {
      int width = getSize().width;
      int height = getSize().height;
      img = createImage(width, height );
      backg = img.getGraphics();

      int levels = 64;  // Should be a power of 4

      a = new int[16];
      b = new int[16];

      int indexa = 0;
      int indexb = 0;
      for(int c=0; c<32; c+=2)
      {
          a[indexa++] = data[c];
          b[indexb++] = data[c+1];
      }
      
      pixelvector p = new pixelvector();
      int i,j;

      p.x = width/2 - 2*levels;
      p.y = height/2 - 2*levels;

      fractal(p, levels);
     
      
      backg.setColor(Color.black);
      backg.drawString("Fractal Pattern (www.xbdev.net)", 30, 30);

  }// End of init(..)
  
}// End of applet

