迭代分形
分形就是一个图形的组成部分与整体相似,通过递归的形式可以实现。
更多图形可以参考http://paulbourke.net/fractals/ifs/
import java.awt.*;
import java.util.Random;
public class DrawThread extends Thread {
private String type;
private Graphics g;
public DrawThread(Graphics g){
this.g = g;
}
public void setType(String t){
type = t;
}
@Override
public void run() {
double x = 0,y = 0;
Color color = new Color(0, 0, 0, 255);
g.setColor(color);
if ("1".equals(type)){
double[] a = {0.0500,-0.0500,0.0300,-0.0300,0.5600,0.1900,-0.3300};
double[] b = {0.0000,0.0000,-0.1400,0.1400,0.4400,0.0700,-0.3400};
double[] c = {0.0000,0.0000,0.0000,0.0000,-0.3700,-0.1000,-0.3300};
double[] d = {0.4000,-0.4000,0.2600,-0.2600,0.5100,0.1500,0.3400};
double[] e = {-0.0600,-0.0600,-0.1600,-0.1600,0.3000,-0.2000,-0.5400};
double[] f = {-0.4700,-0.4700,-0.0100,-0.0100,0.1500,0.2800,0.3900};
for (int i = 0; i < 100000; i++) {
Random random = new Random();
int j = random.nextInt(7);
double tempx = a[j]*x+b[j]*y+e[j];
double tempy = c[j]*x+d[j]*y+f[j];
x = tempx;
y = tempy;
int px = (int)(-tempx*150+200);
int py = (int)(-tempy*150+200);
g.drawLine(px,py,px,py);
}
}else if ("2".equals(type)){
double[] a = {0.0000,0.7248,0.1583,0.3386};
double[] b = {0.2439,0.0337,-0.1297,0.3694};
double[] c = {0.0000,-0.0253,0.3550,0.2227};
double[] d = {0.3053,0.7426,0.3676,-0.0756};
double[] e = {0.0000,0.2060,0.1383,0.0679};
double[] f = {0.0000,0.2538,0.1750,0.0826};
for (int i = 0; i < 100000; i++) {
Random random = new Random();
int j = random.nextInt(4);
double tempx = a[j]*x+b[j]*y+e[j];
double tempy = c[j]*x+d[j]*y+f[j];
x = tempx;
y = tempy;
int px = (int)(-tempx*250+700);
int py = (int)(-tempy*250+300);
g.drawLine(px,py,px,py);
}
}else if ("3".equals(type)){
double a = -1.8, b = -2.0, c = -0.5, d = -0.9;
for (int i = 0; i < 100000; i++) {
double tempx = Math.sin(a*y)+c*Math.cos(a*x);
double tempy = Math.sin(b*x)+d*Math.cos(b*y);
x = tempx;
y = tempy;
int px = (int)(-tempx*100+200);
int py = (int)(-tempy*100+600);
g.drawLine(px,py,px,py);
}
}else {
double a = 1.6, b = -0.6, c = -1.2, d = 1.6;
for (int i = 0; i < 100000; i++) {
double tempx = Math.sin(a*y)+c*Math.cos(a*x);
double tempy = Math.sin(b*x)+d*Math.cos(b*y);
x = tempx;
y = tempy;
int px = (int)(-tempx*100+600);
int py = (int)(-tempy*100+700);
g.drawLine(px,py,px,py);
}
}
}
}
import javax.swing.*;
public class IFS extends JFrame{
public static void main(String[] args) {
JFrame ifs = new JFrame();
ifs.setTitle("迭代分形");
ifs.setSize(800,800);
ifs.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ifs.setVisible(true);
for (int i = 0; i < 4; i++) {
DrawThread drawThread = new DrawThread(ifs.getGraphics());
drawThread.setType(String.valueOf(i+1));
drawThread.start();
}
}
}
getGraphics());
drawThread.setType(String.valueOf(i+1));
drawThread.start();
}
}
}