Home > Java > javaTutorial > Example code about Mandelbrot fractal

Example code about Mandelbrot fractal

零下一度
Release: 2017-07-23 10:23:11
Original
1353 people have browsed it

At present, the fractals that the author has come into contact with mainly include the following types:

1. Similar to Clifford's fractals. The characteristics of this fractal are: the initial coordinates of the fractal are (0,0). After a large number of iterations through the initial coordinates, a series of points are obtained, and the fractal curve is drawn based on the obtained points. This type of fractal has limited parameters and can be easily implemented.

2. Fractal like IFS fern. This fractal has more parameters than the previous one. It is worth noting that there is a P value in the parameter list of the IFS fern fractal. This value represents the probability that each set of different parameters should appear. If this value is useless It is impossible to get the desired graphics.

3. Fractal like Mandelbrot. This fractal involves the knowledge of complex numbers and time escape algorithms. Essentially, it is a set of a series of points on the complex plane. The time escape algorithm is used to determine whether the point is within the set, and a series of points are obtained, and graphics are drawn based on these points.

4. Fractal like L-System Sticks. This type of fractal needs to define the parent string and the rules of evolution, and draw graphics through the points reached by different parent strings and evolution rules. It is not difficult to understand the evolution rules and parent strings. The main reason is that the transformation between coordinates is difficult to calculate.

The following is a piece of code about Mandelbrot fractal.

/**
 * 复数类
 * @author CBS */public class Complex {    public double r;public double i;    public Complex(double real,double image){this.r=real;this.i=image;
    }//取复数的模public double modulus(){return Math.sqrt(r*r+i*i);
    }//复数的加法public Complex add(Complex z){double addr=r+z.r;double addi=i+z.i;return new Complex(addr,addi);
    }//复数的乘法public Complex mul(Complex z){double mulr=r*z.r-i*z.i;double muli=i*z.r+r*z.i;return new Complex(mulr,muli);
    }
}
Copy after login
// 求最大的迭代次数的算法,时间逃逸算法public int mand(Complex z, int maxIte) {
        Complex curComp = new Complex(0, 0);for (int i = 0; i < maxIte; i++) {if (curComp.modulus() > 2)return i;
            curComp = curComp.mul(curComp).add(z);
        }return maxIte;
    }
Copy after login
// 画图的算法public void drawMand(Complex z, double scale, int MaxIte) {double pixUnit = 3 / (1080 * scale);double startx = z.r - 1080 * pixUnit / 2;double starty = z.i - 720 * pixUnit / 2;for (int i = 0; i < 1080; i++) {for (int j = 0; j < 720; j++) {double x0 = startx + i * pixUnit;double y0 = starty + j * pixUnit;
                Complex curComplex = new Complex(x0, y0);int time = mand(curComplex, MaxIte);if (time == MaxIte) {double x = x0 * 150 + 500;// 扩大出现方格double y = y0 * 150 + 500;
                    g.drawLine((int) x, (int) y, (int) x, (int) y);
                }
            }
        }
    }
Copy after login

The above is the detailed content of Example code about Mandelbrot fractal. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template