/* autogenerated by Processing revision 1286 on 2022-12-05 */
import processing.core.*;
import processing.data.*;
import processing.event.*;
import processing.opengl.*;

import java.util.HashMap;
import java.util.ArrayList;
import java.io.File;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

public class sketch_MandelBrotWoohoo extends PApplet {

public int Size = 900;
public ComplexNumber[][] Screen = new ComplexNumber[Size][Size];
public int TempColorStorage = 0;
public float Divisor = Size / 3;
public int XOffset = 0;
public int YOffset = 0;
public int MaxLayers = 100;
public float Scale = 1;
public float ScaleFactor = 1.3f;
public Point LastCenter = new Point(0, 0);
public Point CurrentCenter = new Point(0, 0);


class Point
{
 float x = 0;
 float y = 0;
 public Point(float xargs, float yargs)
 {
  x = xargs;
  y = yargs;
 }
  public Point Copy()
 {
   return(new Point(this.x, this.y));
 }
}

class ComplexNumber
{
 public float R = 0;
 public float I = 0;
 public float Modulo = 0;
 public ComplexNumber(float Rargs, float Iargs)
 {
   R = Rargs;
   I = Iargs;
 }
}

 public void settings()
{
  size(Size + XOffset, Size + YOffset);
}


 public void setup()
{
  frameRate(100 );
  InitializeScreen();
  DrawMandelBrot();
  print("Hi");
}
 public void DrawMandelBrot()
{
  background(255);
  for(int r = 0; r < Size; r++)
  {
    for(int j = 0; j < Size; j++)
    {
      if(CheckInfinity(Screen[r][j], new ComplexNumber(0, 0), 0) == false)
      {
        //stroke(TempColorStorage);
        //fill(TempColorStorage);
        point(r, j);
      }
    }
  }
  point(Size/2 + XOffset, Size/2 + YOffset);
}
 public boolean CheckInfinity(ComplexNumber OriginalNumber, ComplexNumber Z, int Layerdepth)
{
  if(Layerdepth == MaxLayers)
  {
    return false;
  }
  Z.Modulo = sqrt(Z.R * Z.R + Z.I * Z.I);
    if(Z.Modulo > 2)
    {
      TempColorStorage = PApplet.parseInt(Z.Modulo * 50);
      return(true);
    }
  ComplexNumber Z2 = new ComplexNumber(0, 0);
  Z2.R = Z.R * Z.R - Z.I * Z.I + OriginalNumber.R;
  Z2.I = Z.R * Z.I * 2 + OriginalNumber.I;
  return CheckInfinity(OriginalNumber, Z2, Layerdepth + 1);
}
 public void mousePressed()
{
  Scale *= ScaleFactor;
  CurrentCenter.x = -Size / 2 * Scale;
  CurrentCenter.y = -Size / 2 * Scale;
  translate(CurrentCenter.x + Size / 2, CurrentCenter.y + Size / 2);
  LastCenter = CurrentCenter.Copy();
  
  scale(Scale);
  DrawMandelBrot();
}
 public void keyPressed()
{
 if(keyCode == ENTER)
 {
  //popMatrix();
  //pushMatrix();
  Scale = 1;
  scale(Scale);
  translate(0, 0);
  DrawMandelBrot();
 }
}
 public void draw()
{
  
}

 public void InitializeScreen()
{
  for(int r = -(Size / 2); r < Size / 2; r++)
  {
    for(int j = -(Size / 2); j < Size / 2; j++)
    {
      Screen[r + Size/2][j + Size/2] = new ComplexNumber(PApplet.parseFloat(r - Size / 3) / Divisor, PApplet.parseFloat(j) / Divisor);
    }
  }
}


  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "sketch_MandelBrotWoohoo" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}
