用迭代绘制出图像

今天的内容呢emmm,关于图像形成网址问题,pc端浏览器不知道怎么就打不开了(若有大佬知道怎么弄出来,请务必踹我一下(抱拳!!!))


传送门:http://paulbourke.net/fractals/ifs/

网上扒了一张图,大家凑合着先看哈,一会给大家看效果图
在这里插入图片描述

写这类图像代码感受:

  • 都是以Math类中的方法作为媒介
  • 通过迭代目标的方式进行操作
  • 通过循环的方式不断让图像凝实(其实这是人类视觉的错觉,因为所有的图像都是由矢量图根据 数量的增加和距离的把控,让我们有视觉上的冲击)

代码构建:
1、搭建图形窗口的框架

public class Task extends JFrame {
 public void initUI() {
        JFrame jFrame = this;//构建窗体
        jFrame.setTitle("迭代分形");//窗体左上角命名
        jFrame.setSize(800, 800);//组件大小
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //窗口关闭停止运行
        jFrame.setLocationRelativeTo(null);//窗体居中
        jFrame.setVisible(true);//显示窗体
        }

2、写入我们要写入的图形方法中的参数

public void drawIFS(Graphics graphics) {
        double a = -1.8, b = -2.0, c = -0.5, d = -0.9;
        double x = 0d, y = 0d;//设定初始值
        for (int i = 0; i < 100000; i++) {
            double nx = Math.sin(a * y) + c * Math.cos(a * x);
            double ny = Math.sin(x * b) + d * Math.cos(b * y);
            x = nx;
            y = ny;//迭代目标

            int px = (int) (x * 100);//放大目标100倍 控制图像的大小
            int py = (int) (y * 100);

            graphics.setColor(Color.blue);//给图像上色
            graphics.fillRect(px + 400, py + 400, 1, 1);
        }

3、重写"绘制容器" paint()方法

  public void paint(Graphics graphics)//重绘必须重写paint方法,不然画不出来
    {
        super.paint(graphics);
	}

4、主方法中调用

 public static void main(String[] args) {
    new Task().initUI();
    }

5、在paint方法中调用写好的函数方法

public void paint(Graphics graphics)
    {
        super.paint(graphics);
		drawIFS(graphics);
		}

效果图如下:
在这里插入图片描述

还有其他的几组图片,把方法中的参数换一下即可

public void DrawPicture(Graphics graphics){
        double x=0d,y=0d;
        double a=-1.7,b=1.3,c=-0.1,d=-1.2;
        for (int i=0;i<100000;i++){
            double nx=Math.sin(a*y)+c*Math.cos(a*x);
            double ny=Math.sin(b*x)+d*Math.cos(b*y);
            x=nx;
            y=ny;
            int px=(int)(x*50);
            int py=(int)(y*50);
            graphics.setColor(Color.black);
            graphics.fillRect(px+400,py+400,1,1);
        }

效果图:在这里插入图片描述


 public void drawFen(Graphics graphics)
    {
        double x = 0d, y=0d;
        double a = 1,b = 4,c = 60;
        for (int i = 0; i < 1000000000; i++) {
            double nx = y - Math.signum(x) * Math.sqrt(Math.abs(b*x-c));
            double ny = a-x;
            x= nx;
            y= ny;
            int px = (int)(x);
            int py = (int)(y);
            graphics.setColor(Color.cyan);
            graphics.fillRect(px+400,py+400,1,1);
        }
    }

在这里插入图片描述

 public void DrawFen1(Graphics graphics)
    {
        double a =0.4,b=1,c=0;
        double x=0d,y=0d;
        for (int i = 0; i < 1000000; i++) {
        double nx = y-Math.signum(x)*Math.sqrt(Math.abs(b*x-c));
        double ny = a-x;
        x=nx;
        y=ny;
        int px = (int)(x*50);
        int py = (int)(y*50);
        graphics.setColor(Color.green);
        graphics.fillRect(px+400,py+400,1,1);
        }
    }
}

在这里插入图片描述
网址上面还有很多,目前只选了其中几个进行制作,网页打不开求大佬指点(抱拳!!!)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值