packagecom.thealgorithms.others;importjava.awt.*;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.IOException;importjavax.imageio.ImageIO;publicclassMandelbrot{publicstaticvoidmain(String[] args){// Test black and whiteBufferedImage blackAndWhiteImage =getImage(800,600,-0.6,0,3.2,50,false);// Pixel outside the Mandelbrot set should be white.assert blackAndWhiteImage.getRGB(0,0)==newColor(255,255,255).getRGB();// Pixel inside the Mandelbrot set should be black.assert blackAndWhiteImage.getRGB(400,300)==newColor(0,0,0).getRGB();// Test color-codingBufferedImage coloredImage =getImage(800,600,-0.6,0,3.2,50,true);// Pixel distant to the Mandelbrot set should be red.assert coloredImage.getRGB(0,0)==newColor(255,0,0).getRGB();// Pixel inside the Mandelbrot set should be black.assert coloredImage.getRGB(400,300)==newColor(0,0,0).getRGB();// Save imagetry{ImageIO.write(coloredImage,"png",newFile("Mandelbrot.png"));}catch(IOException e){
e.printStackTrace();}}publicstaticBufferedImagegetImage(int imageWidth,int imageHeight,double figureCenterX,double figureCenterY,double figureWidth,int maxStep,boolean useDistanceColorCoding){if(imageWidth <=0){thrownewIllegalArgumentException("imageWidth should be greater than zero");}if(imageHeight <=0){thrownewIllegalArgumentException("imageHeight should be greater than zero");}if(maxStep <=0){thrownewIllegalArgumentException("maxStep should be greater than zero");}BufferedImage image =newBufferedImage(imageWidth, imageHeight,BufferedImage.TYPE_INT_RGB);double figureHeight = figureWidth / imageWidth * imageHeight;// loop through the image-coordinatesfor(int imageX =0; imageX < imageWidth; imageX++){for(int imageY =0; imageY < imageHeight; imageY++){// determine the figure-coordinates based on the image-coordinatesdouble figureX = figureCenterX +((double) imageX / imageWidth -0.5)* figureWidth;double figureY = figureCenterY +((double) imageY / imageHeight -0.5)* figureHeight;double distance =getDistance(figureX, figureY, maxStep);// color the corresponding pixel based on the selected coloring-function
image.setRGB(
imageX,
imageY,
useDistanceColorCoding
?colorCodedColorMap(distance).getRGB():blackAndWhiteColorMap(distance).getRGB());}}return image;}/**
* Black and white color-coding that ignores the relative distance. The
* Mandelbrot set is black, everything else is white.
*
* @param distance Distance until divergence threshold
* @return The color corresponding to the distance.
*/privatestaticColorblackAndWhiteColorMap(double distance){return distance >=1?newColor(0,0,0):newColor(255,255,255);}/**
* Color-coding taking the relative distance into account. The Mandelbrot
* set is black.
*
* @param distance Distance until divergence threshold.
* @return The color corresponding to the distance.
*/privatestaticColorcolorCodedColorMap(double distance){if(distance >=1){returnnewColor(0,0,0);}else{// simplified transformation of HSV to RGB// distance determines huedouble hue =360* distance;double saturation =1;double val =255;int hi =(int)(Math.floor(hue /60))%6;double f = hue /60-Math.floor(hue /60);int v =(int) val;int p =0;int q =(int)(val *(1- f * saturation));int t =(int)(val *(1-(1- f)* saturation));switch(hi){case0:returnnewColor(v, t, p);case1:returnnewColor(q, v, p);case2:returnnewColor(p, v, t);case3:returnnewColor(p, q, v);case4:returnnewColor(t, p, v);default:returnnewColor(v, p, q);}}}/**
* Return the relative distance (ratio of steps taken to maxStep) after
* which the complex number constituted by this x-y-pair diverges. Members
* of the Mandelbrot set do not diverge so their distance is 1.
*
* @param figureX The x-coordinate within the figure.
* @param figureX The y-coordinate within the figure.
* @param maxStep Maximum number of steps to check for divergent behavior.
* @return The relative distance as the ratio of steps taken to maxStep.
*/privatestaticdoublegetDistance(double figureX,double figureY,int maxStep){double a = figureX;double b = figureY;int currentStep =0;for(int step =0; step < maxStep; step++){
currentStep = step;double aNew = a * a - b * b + figureX;
b =2* a * b + figureY;
a = aNew;// divergence happens for all complex number with an absolute value// greater than 4 (= divergence threshold)if(a * a + b * b >4){break;}}return(double) currentStep /(maxStep -1);}}