数字的美丽——分形图形

第一次接触分形就被那些图形深深的吸引住了,在那些简单的杂乱无章的外貌下 实际上藏着的事一颗简单智慧
的心(分形公式)。
最开始接触的事丢色子游戏:
1.平面上随机选A,B,C三个点。再随机选一个点,记为P。
2.有一个三面色子,每丢一次,则选中ABC三个中一点。
开始游戏:
1、重复丢色子,如果选中A,则取A和P的中点P1,画黑,
2、如果选中B,则取B和P1的中点P2,画黑
3、如果选中A,则取A和P2的中点P3,画黑
4、...一直重复(如每点一下鼠标,丢100次色子。
最终画出来的效果出乎意料之外 看是杂乱的规则 最后却画出了规则的图形(倾斜的谢冰斯基三角形)。

慢慢的又得到了许多分形公式,一一画出来,顿时觉得数学是多么的强大,数学也可以如此美丽。

得到公式后并不是简单的一模一样的调用还是需要灵活的应用的:
第一类应用方法:迭代。 所谓迭代关系式,指如何从变量的前一个值推出其下一个值的公式(或关系)。
迭代关系式的建立是解决迭代问题的关键,通常可以使用递推或倒推的方法来完成。
类似于高中的数列,由n推出n+1项。

第二类应用方法:递归。 递归就是在过程或函数里调用自身;在使用递归策略时,必须有一个明确的
递归结束条件,称为递归出口。一般来说,递归需要有边界条件、递归前进段和递归返
回段。当边界条件不满足时,递归前进;当边界条件满足时,递归返回。
斐波纳契数列(Fibonacci Sequence),又称黄金分割数列,指的是这样一个数列
:1、1、2、3、5、8、13、21..... I[1] 斐波纳契数列是典型的递归案例。

课后小结:总体来说分形还是蛮简单的,只要分析出规律还是很好做的。但还是有些比较难的,迭代比较简单,
一般就是直接应用就行,递归的话,条理一定要清晰。不然很容易混乱。记得递归时参数的变化,及递归的出口,次数。

下面是几个简单的分形例子:
丢色子游戏:



import java.awt.Graphics;
import java.util.Random;

import javax.swing.JFrame;

public class DrawPicture extends JFrame {

/**
* @param args
*/
public static void main(String[] args) {
DrawPicture dp = new DrawPicture();
dp.init();
}

public void init(){
this.setSize(600, 600);
this.setDefaultCloseOperation(3);
this.setResizable(false);
this.setLocationRelativeTo(null);


this.setVisible(true);
g = this.getGraphics();

JFrameListener jfl = new JFrameListener(g);
this.addMouseListener(jfl);
}

public void paint(Graphics g){
super.paint(g);
}

Graphics g;
}




package cn.gxx.study09;

import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

public class JFrameListener extends MouseAdapter {
Graphics g;
public JFrameListener(Graphics g){
this.g=g;
}

public void mouseClicked(MouseEvent e) {
drawpoint(g);
}
public void drawpoint(Graphics g){
Random rand = new Random();
//画A
ax = rand.nextInt(600)+1;
ay = rand.nextInt(600)+1;
g.drawLine(ax,ay,ax,ay);
//画B
bx = rand.nextInt(600)+1;
by = rand.nextInt(600)+1;
g.drawLine(bx,by,bx,by);
//画C
cx = rand.nextInt(600)+1;
cy = rand.nextInt(600)+1;
g.drawLine(cx,cy,cx,cy);
//画P
px = rand.nextInt(600)+1;
py = rand.nextInt(600)+1;
g.drawLine(px, py, px, py);




while(count<100000){
choice = rand.nextInt(3);
if(choice==0){
px=(px+ax)/2;
py=(py+ay)/2;
g.drawLine(px, py, px, py);
}
else if (choice==1){
px=(px+bx)/2;
py=(py+by)/2;
g.drawLine(px, py, px, py);
}
else if(choice==2){
px=(px+cx)/2;
py=(py+cy)/2;
g.drawLine(px, py, px, py);
}
count++;
}

}
private int ax,bx,cx,dx,ex,fx,ay,by,cy,dy,ey,fy;
private int px,py;
private int choice;
private long count;

}



三角形:

//谢冰斯基三角形
public void draw2(Graphics g){
double x1=400,y1=50;
double x2=100,y2=570;
double x3=700,y3=570;

g.setColor(Color.GREEN);
g.drawLine((int)x1,(int) y1,(int) x2,(int) y2);
g.drawLine((int)x1, (int)y1,(int) x3,(int)y3);
g.drawLine((int)x2,(int) y2,(int) x3,(int) y3);

draw3ang_2(x1,y1,x2,y2,x3,y3);

}

public void draw3ang_2(double x1,double y1,double x2,double y2,double x3,double y3){
double x1_temp=0,y1_temp=0;
double x2_temp=0,y2_temp=0;
double x3_temp=0,y3_temp=0;
x1_temp=(x1+x2)/2;
y1_temp=(y1+y2)/2;
x2_temp=(x3+x2)/2;
y2_temp=(y3+y2)/2;
x3_temp=(x1+x3)/2;
y3_temp=(y1+y3)/2;
if((int)Math.abs(x1_temp-x2_temp)==0){
return;
}
g.drawLine((int)x1_temp,(int) y1_temp,(int) x2_temp,(int) y2_temp);
g.drawLine((int)x1_temp, (int)y1_temp,(int) x3_temp,(int)y3_temp);
g.drawLine((int)x2_temp,(int) y2_temp,(int) x3_temp,(int) y3_temp);


draw3ang_2(x1,y1,(x2+x1)/2,(y1+y2)/2,(x1+x3)/2,(y1+y3)/2);
draw3ang_2((x2+x1)/2,(y1+y2)/2,x2,y2,(x2+x3)/2,(y2+y3)/2);
draw3ang_2((x1+x3)/2,(y1+y3)/2,(x2+x3)/2,(y3+y2)/2,x3,y3);
}



矩形:


//画矩形
public void draw4(Graphics g){
double x1=150,y1=80;
double width=500,height=500;
g.setColor(Color.GREEN);
g.fillRect((int)x1, (int)y1, (int)width, (int)height);
drawrec(x1, y1,width,height);

}
public void drawrec(double x1,double y1,double width,double height){
if((int)width==0){
return ;
}
double x=0,y=0;
double width_temp=width/3;
double height_temp=height/3;
x=x1+width_temp;
y=y1+height_temp;
g.setColor(Color.WHITE);
g.fillRect((int)x,(int)y,(int)width_temp,(int)height_temp);

drawrec(x-width_temp,y-height_temp,width_temp,height_temp);
drawrec(x,y-height_temp,width_temp,height_temp);
drawrec(x+width_temp,y-height_temp,width_temp,height_temp);
drawrec(x-width_temp,y,width_temp,height_temp);
drawrec(x+width_temp,y,width_temp,height_temp);
drawrec(x-width_temp,y+height_temp,width_temp,height_temp);
drawrec(x,y+height_temp,width_temp,height_temp);
drawrec(x+width_temp,y+height_temp,width_temp,height_temp);

}


树叶:


public void drawLeaf(Graphics g, double x, double y, double L, double a) {
// // 可以方面速度画以了解其算法
// try {
// Thread.sleep(10);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }


int red = random.nextInt(126);
int green = random.nextInt(126);
int blue = random.nextInt(126);
//随机颜色
g.setColor(new Color(red, green, blue));
// g.setColor(Color.GREEN);
double x1, x2, x1L, x2L, x2R, x1R, y1, y2, y1L, y2L, y2R, y1R;
float deflection = 50-random.nextInt(20);//侧干主干的夹角
float intersection = random.nextInt(40)-20;//主干偏转角度
float depth = 2+random.nextInt(2);//限制递归深度
float ratio = 3f;//主干侧干长度比(可调整使其更茂密或稀疏)
float ratio2 = 1.2f;//上级主干与本级主干长度比(可调整使其变高低)
if (L > depth) {

System.out.println("");


x2=x+L*Math.cos(a*PI1);
y2=y+L*Math.sin(a*PI1);
x2R=x2+L/ratio*Math.cos((a+deflection)*PI1);
y2R=y2+L/ratio*Math.sin((a+deflection)*PI1);
x2L=x2+L/ratio*Math.cos((a-deflection)*PI1);
y2L=y2+L/ratio*Math.sin((a-deflection)*PI1);
x1=x+L/ratio*Math.cos(a*PI1);
y1=y+L/ratio*Math.sin(a*PI1);
x1L=x1+L/ratio*Math.cos((a-deflection)*PI1);
y1L=y1+L/ratio*Math.sin((a-deflection)*PI1);
x1R=x1+L/ratio*Math.cos((a+deflection)*PI1);
y1R=y1+L/ratio*Math.sin((a+deflection)*PI1);
g.drawLine((int)x-50,(int)y+50,(int)x2-50,(int)y2+50);
g.drawLine((int)x2-50,(int)y2+50,(int)x2R-50,(int)y2R+50);
g.drawLine((int)x2-50,(int)y2+50,(int)x2L-50,(int)y2L+50);
g.drawLine((int)x1-50,(int)y1+50,(int)x1L-50,(int)y1L+50);
g.drawLine((int)x1-50,(int)y1+50,(int)x1R-50,(int)y1R+50);
drawLeaf(g,x2,y2,L/ratio2,a+intersection);
drawLeaf(g,x2R,y2R,L/ratio,a+deflection);
drawLeaf(g,x2L,y2L,L/ratio,a-deflection);
drawLeaf(g,x1L,y1L,L/ratio,a-deflection);
drawLeaf(g,x1R,y1R,L/ratio,a+deflection);
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MATLAB可以用来生成分形图形,其中之一就是火焰形态的分形图形。 要生成火焰形态的分形图形,可以使用递归函数和随机数生成器。 首先,我们可以定义一个函数,该函数根据一些随机数生成规则生成火焰形态的分形图形。随机数在生成分形形状时起到重要的作用,因为它们使每次生成的图像都具有一定的变化性。 然后,在函数中使用递归算法,该算法允许我们将图像分成多个小的分形形状。对于每个小的分形形状,我们可以递归地应用同样的规则来生成更多的小分形,直到达到设置的终止条件。 在每次递归生成新的小分形时,我们可以使用一些数学变换,例如平移、旋转和缩放,以使每个分形形状都略有不同。这使得火焰形态的分形图形看起来更加动态和真实。 最后,我们可以使用绘图函数将生成的火焰形态的分形图形显示出来。在MATLAB中,可以使用图形绘制函数(如plot和scatter)来绘制分形图形。 通过调整随机化规则、递归算法和数学变换的参数,我们可以更改生成的火焰形态分形图形的外观和复杂性。 总之,使用MATLAB可以生成火焰形态的分形图形,通过随机数、递归算法和数学变换来生成多样的火焰形态图像。这种方法可以帮助我们更好地理解分形几何学,同时也可以用于艺术创作、科学研究和其他应用领域。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值