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); } }
// 求最大的迭代次数的算法,时间逃逸算法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; }
// 画图的算法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); } } } }
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!