

/*
 *
 * Jonathan Riddell jr050@students.stir.ac.uk
 * Version 0.1
 * Copyright (C) 1999/2000 JR Released under GNU General Public Licence version 2 or at your discretion later
 * A program to draw a madelbrot set
 */

import java.awt.*;
import java.applet.Applet;
public class Mandelbrot extends java.applet.Applet {

DrawMandelbrot myMandelbrot;

  public void init () {
  myMandelbrot = new DrawMandelbrot();


  }

  public void paint(Graphics g) {
    System.out.println("painting started");
    myMandelbrot.draw(g);
    System.out.println("painting finished");
  }

}

class DrawMandelbrot {  //draws mandelbrot set over 300 x 300 pixels
/* int iterations = 1;
 float xStart, yStart, xOld, yOld, xNew, yNew;//variables to do calulations with
 float r = 1; //put in formula to see if black or n */

 public void draw(Graphics g) {

  System.out.println("started myMandelbrot.draw");
   for (int yPixel = 0; yPixel < 300; yPixel++) {   //counters go through all pixels on screen
     for (int xPixel = 0; xPixel < 300; xPixel++) {
       if (inSet(xPixel, yPixel))  //method which decides if pixel is in mandelbrot set
         g.drawLine(xPixel,yPixel,xPixel,yPixel);
     }
   }
 }

 private boolean inSet(int xPixel, int yPixel){
 int iterations = 1;
 float xStart, yStart, xOld, yOld, xNew, yNew;//variables to do calulations with
 float r = 1; //result of formula to see if in set or not

  yStart = (yPixel - 150.0f) / 100.0f; //maps pixel coordinates onto our coordinates
  xStart = (xPixel - 50.0f) / 100.0f;  //between -150<y<150 -500<x<200
  xOld = xStart;
  yOld = yStart;
  
  while ((r < 10000.0f) && (iterations < 100)) {
    iterations++;
    xNew = (xOld * xOld) - (yOld * yOld) - xStart;
    yNew = (2 * xOld * yOld) - yStart;
    r = (float) (Math.sqrt((xNew * xNew) + (yNew * yNew)));  //if always r < 10000 then pixel is in set
    xOld = xNew;
    yOld = yNew;
   }
  if (r < 10000.0f)
     return true;
  else
     return false;
 }

} //end of class


