Mandelbrot Set

Mandelbrot Set

This is an interesting java project for displaying a Mandelbrot set in multiple interesting ways. This project is heavily inspired by the java Mandelbrot set created by Robert Sedgewick and Kevin Wayne used by the introduction to CS course at Princeton. 

Prerequisites:
For this project you will need to have imported the following classes in order for the project to work.


  1. Complex.java
  2. Picture.java
  3. Color.java

The complex and picture classes are also created by Robert Sedgewick and Kevin Wayne for their respective Princeton classes.

Here is the product of the standard Mandelbrot set without colour





For this standard variation of the Mandelbrot set I used this code:
  • Before you launch this code you need to input three arguments that are -0.5 0 2 where the final number (in the standard case 2) can be used for scaling the shape, making it either bigger or smaller in an inverse relationship (making the number bigger will make the shape smaller)

import java.awt.Color;

public class Mandelbrot {

    // return number of iterations in which it confirms it's a mandelbrot set, if it's not return the value
    public static int mand(Complex z0, int max)
    {
    Complex comp = z0;
    for(int f = 0; f < max; f++)
    {
    if(comp.abs() > 2.0)
    {
    //get the number of iterations it took to find if it's mandelbrot
    return f + 1;
    }
    comp = comp.times(comp).plus(z0);
    }
    //return white
    return 255;
    }

    public static void main(String[] args)  {
    //the arguments that need to be input are -0.5 0 2
        double xc   = Double.parseDouble(args[0]);
        double yc   = Double.parseDouble(args[1]);
        double size = Double.parseDouble(args[2]);
        //number of pixels on one side of an image
        int n   = 650;
        int max = 255;
        Picture picture = new Picture(n, n);
        for(int i = 0; i < n; i++)
        {
        for(int j = 0; j < n; j++)
        {
        //coordinates of individual pixel
        double x0 = xc - size/2 + size*i/n;
                double y0 = yc - size/2 + size*j/n;
                //create new complex number (based on the points given of x and y)
                Complex comp = new Complex(x0, y0);
                int colorAlt = (max - mand(comp, max));
                Color color = new Color(colorAlt, colorAlt, colorAlt);
                color.brighter();
                picture.set(i, n-1-j, color.brighter());
        }
        }
        picture.show();
    }
}

This is certainly an interesting piece of geometry but it can be made even more interesting through the addition of colour and making the shape a bit more understandable and clear because as of now it looks pretty pixelated.

For a colourful version of this shape we will edit this line of the code 

Color color = new Color(colorAltcolorAltcolorAlt);

The three parameters of this method are the RGB values of the colour you want to sign the shape. You can play with what numbers you input where (note that you can only replace the colorAlt value only with values 0 through 255) however one colorAlt parameter needs to remain in this method in order to display the Mandelbrot set as this is how the shape is displayed, if all three parameters are replaced with a custom number the end product will be a screen with one solid colour.

My personal favourite is the really nice magenta colour created when the green value is set to 0, like so

Color color = new Color(colorAlt, 0colorAlt);

This will be the result of such change in code




This change makes the background colourful because the Mandelbrot set is displayed not through whether a point is a part of the set but rather what points aren't a part of the set, those which aren't are coloured in while the ones that are a part of the set are left to be the standard background colour of black.
to make the shape a bit more complex and a bit more understandable we can edit this line in order to further polarise the set.

int colorAlt = (max - mand(comp, max));

By dividing the value that is the current output of this method with another mand(comp, max) will result in the shape being more interesting as the color value isn't just subtracted from the max (255) but also divided by it, making it a more intriguing shape.

int colorAlt = (max - mand(comp, max))/mand(comp, max);

This is the result of that change


(note that for this display I used 3 instead of 2 for the final argument for better display)

I hope that this was an informative post and I thank the authors of all of the source code I used in order to show some interesting edits you can make to a standard Mandelbrot set code.


Komentáre