之前用C语言实现过一些分形,但是代码比较复杂。而对于天生对绘图友好的Processing,及其方便。
在大自然中分形普遍存在,我们用图形模拟,主要是找到一个贴近的函数。
代码
1 /** 2 * Wallpaper 3 * 微信公众号:维度模态 4 * 作者:Hewes 5 */ 6 7 size(800, 800); //画布尺寸 8 background(0); //背景色 9 10 //相关参数的设定 11 //double a = 0.50, b = 2.00, c = 0.00; 12 //int mag=60; 13 //double a = 0.4, b = 1, c = 0; 14 //int mag=100; 15 double a = 1, b = 4, c = 60; 16 int mag=1; 17 //double a = -1, b = -2, c = -3; 18 //int mag=5; 19 //double a = -1000, b = 0.1, c = -10; 20 //int mag=10; 21 22 double x1=0, y1=0, temp; //涉及变量 23 24 for (int i = 0; i < 2550000; i++) { 25 //壁纸公式: 26 //xn+1 = yn - sign(xn) | b xn - c |1/2 27 //yn+1 = a - xn 28 temp = x1; 29 x1 = y1 - Math.signum(a * x1) * Math.sqrt(Math.abs(b * x1 - c)); 30 y1 = a - temp; 31 32 //放大+平移 33 int m = (int) (x1 * mag + width/2); 34 int n = (int) (y1 * mag + height/2); 35 //笔触颜色设置 36 stroke(255, i/10000, i/10000); 37 //stroke(255-i/10000,255-i/10000, 0); 38 point(m, n); 39 }
调节参数会有看起来完全不同的结果:
double a = 1, b = 4, c = 60;
int mag=1;
double a = –1000, b = 0.1, c = –10;
int mag=10;
double a = 0.4, b = 1, c = 0;
int mag=100;
参考链接:http://iprocessing.cn/2017/08/10/第四期-processing分形之一-wallpaper/