Java笔记-迭代分形

迭代分形

分形就是一个图形的组成部分与整体相似,通过递归的形式可以实现。

在这里插入图片描述

更多图形可以参考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();
}
}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值