


什么是IFS?
IFS(Iterated Function System)迭代函数系统,可以通过迭代来创造出分形图案
什么是分形图案?
分形图案:力图通过数学方法,来创造自相似的图案(例如树叶等)
下面,我们通过三类例子来深入探索IFS的世界:
1.单一常量

代码实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| void generate(Graphics g){ double x=0,y=0; double tx,ty; double a=-1.8,b=-2.0,c=-0.5,d=-0.9; for (int i = 0; i < 500000; i++) { tx=Math.sin(a*y)+c*Math.cos(a*x); ty=Math.sin(b*x)+d*Math.cos(b*y); int px=(int)(tx*100)+400; int py=(int)(ty*100)+400; g.drawLine(px,py,px,py); x=tx; y=ty; } }
|
在这里,我们通过放大坐标,使图像能够正常显示
一定要注意类型转换问题!!
生成效果:


不过有一个问题,在生成图片的时候会面临着生成效率慢的问题,为啥呢?
上述程序在运行时,每处理一个像素点,就会将上述流程走一遍,所以会拖慢速度。
假如,我先处理好所有的像素点,再一起输出到GPU,会不会更快呢?
这里,需要用到一个新的类型:BufferedImage,我们先将所有像素点缓存到BufferedImage中,最后一起输出即可
1 2
| BuffedImage Bfg = new BufferImage(界面x长度,界面y长度,颜色表示方法);
|
完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| package ahc0926.DrawIFS.drawagain;
import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage;
public class DrawIFS extends JFrame { private BufferedImage buffer; public DrawIFS(){ setTitle("IFS"); setSize(1000,1000); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable(true); setLocationRelativeTo(null); buffer = new BufferedImage(1000, 1000,BufferedImage.TYPE_INT_RGB); setVisible(true); } @Override public void paint(Graphics g){ super.paint(g); draw(); g.drawImage(buffer,0,0,null); } public void draw(){ Graphics g = buffer.getGraphics(); double a=-1.7,b=1.3,c=-0.1,d=-1.2; double x=0,y=0; double tx,ty; for (int i = 0; i < 1000000; i++) { tx=Math.sin(a*y)+c*Math.cos(a*x); ty=Math.sin(b*x)+d*Math.cos(b*y); int px=(int)(tx*200)+500; int py=(int)(ty*200)+500; g.drawLine(px,py,px,py); x=tx; y=ty; } } public static void main(String[] args) { new DrawIFS(); } }
|
2.几组常量

