/*********************************************************************************************/
/*                                                                                           */
/*  Java Image Demo - Sierpinski Triangle Pattern                                            */
/*  Auth: bkenwright@xbdev.net                                                               */
/*  URL: www.xbdev.net                                                                       */
/*                                                                                           */
/*  Ben Kenwright (bkenwright@xbdev.net)                                                     */
/*                                                                                           */
/*********************************************************************************************/
/*
    Few Words on its workings...
    Looking at the triangle screenshot to see what where aiming for.
    Now in the code we start by taking three pixel points fixed[3], at the top centre and two 
    bottom corners of the viewport.  Then starting at the top left (0,0), a variable pixel p is
    repeatedly calculated to be half way between the present value of p and the one of the
    fixed pixels chosen at random.  After the process has settled down, the pixel is coloured
    in to give this suprising result.
*/
/*********************************************************************************************/

//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;
import java.lang.Math;      // For Random Numbers



class pixelvector
{
  public int x;
  public int y;
};//End pixelvector class




public class fractals extends java.applet.Applet 
{
   Image img;
   Graphics backg;

   public void paint(Graphics g) 
   {
      g.drawImage( img , 0, 0, this );
   }// End of paint(..)
   
   public void init()
   {
      int width = getSize().width;
      int height = getSize().height;
      img = createImage(width, height );
      backg = img.getGraphics();



      pixelvector fixed[]  = new pixelvector[3];
      fixed[0] = new pixelvector();
      fixed[1] = new pixelvector();
      fixed[2] = new pixelvector();

      pixelvector p = new pixelvector();

      int i,j;
	
      fixed[0].x = width/2; fixed[0].y=1;           // top middle
      fixed[1].x = 0;       fixed[1].y = height;    // bottom left
      fixed[2].x = width;   fixed[2].y = height;    // bottom right
      
      for(i=0; i<3; i++)
        backg.drawLine(fixed[i].x,fixed[i].y, fixed[i].x,fixed[i].y);        

      p.x=0;   p.y=0;

      for(i=0; i<30; i++)
      {
         j = (int)(Math.random()*3);     // Random number from 0 to 3
         p.x = (p.x + fixed[j].x) / 2;
         p.y = (p.y + fixed[j].y) / 2;
      }



      // Larger values for greater details
      for(int k=0; k<10000; k++)
      {
         j = (int)(Math.random()*3);     // Random number from 0 to 3
         p.x = (int) ( (float)(p.x + fixed[j].x) / 2.0 + 0.5);
         p.y = (int) ( (float)(p.y + fixed[j].y) / 2.0 + 0.5);
         backg.drawLine(p.x,p.y, p.x,p.y);
      }


     backg.setColor(Color.black);
     backg.drawString("Sierpinski Triangle (www.xbdev.net)", 30, 30);
    

  }// End of init(..)
  
}// End of applet

