java,分形——谢宾斯基地毯,科赫曲线及运用递归的一点感想

画分形图形时,常要用到递归思想,递归思想网上有很多文章介绍,在此写一点我的应用方法总结如下。
1、搞清楚分形图形的基本单位。
2、确定所需要的参数类型个数及个数。
3、根据传入的参数绘制这个基本图形。
4、搞清楚在此基础图形上、我们需要绘制几个相同的绘制图形(比如 在绘制谢宾斯基地毯时,基本图形是一个正方形,在此正方形四周,我们需要绘制八个小正方形)这决定我们在此方法中需要再次调用此方法的次数。
5、确定计数,计数一定要有不然此方法不会结束,会导致栈溢出。
我们需要确定的是,计数方式,不同的图形,计数方式会补同,有些图形从第一个基本图形开始绘制(谢宾斯基三角形,谢宾斯基地毯),但是有些图形需要从最后的一个图形开始绘制(科赫曲线)
6、在此方法里调用此方法,注意调用次数,和传入的参数。

另外:在这个方法中调用这个方法超过一次,那么传入的参数,一定不能是全局变量,多是这个方法中的局部变量。

可能数的不是很清楚 ,下附代码,希望能够帮助没理解

private Graphics g;
private int x;
private int y;
private int width;
private int height;
//画谢宾斯基地毯得方法, (需要实例化画布对象)
public void coordinateCar(double x, double y, double width, double height,int con) {
if (con < 1) {
return;
}
this.x = (int) x;
this.y = (int) y;
this.width = (int) width;
this.height = (int) height;
g.setColor(new Color(con*10%255,con*40%255,con*20%255));
g.fillRect(this.x, this.y, this.width, this.height);
//这里计算下次传入的参数值 ,这个可以另写一个方法
double x1 = x - width * 2 / 3;
double y1 = y - height * 2 / 3;
double width1 = width / 3;
double height1 = height / 3;
double x2 = x + width / 3;
double y2 = y - height * 2 / 3;
double x3 = x + width * 4 / 3;
double y3 = y - height * 2 / 3;

double x4 = x + width * 4 / 3;
double y4 = y + height / 3;
double x5 = x + width * 4 / 3;
double y5 = y + height * 4 / 3;
double x6 = x + width / 3;
double y6 = y + height * 4 / 3;
double x7 = x - width * 2 / 3;
double y7 = y + height * 4 / 3;
double x8 = x - width * 2 / 3;
double y8 = y + height / 3;
con--;
//调用这个方法 超过了一次, 那么传入的参数一定不能是,this.x等等
coordinateCar(x1, y1, width1, height1, con);
coordinateCar(x2, y2, width1, height1, con);
coordinateCar(x3, y3, width1, height1, con);
coordinateCar(x4, y4, width1, height1, con);
coordinateCar(x5, y5, width1, height1, con);
coordinateCar(x6, y6, width1, height1, con);
coordinateCar(x7, y7, width1, height1, con);
coordinateCar(x8, y8, width1, height1, con);

}



//调用这个方法的结果如图 注意计数的位置 与科赫曲线比较

[img]http://dl2.iteye.com/upload/attachment/0086/7204/a9da57d6-f674-3e40-b086-00f4cff04f55.png[/img]

//画科赫曲线的方法

private double pi=Math.PI;
private Graphics g;
private int x;
private int y;
private int x2;
private int y2;
private int x3;
private int y3;
private int x4;
private int y4;
private int x5;
private int y5;
private double angle1=pi/3;
private double angle2=pi*5/3;
public void drawKoch(double x,double y,double initialangle,double length,int con){
//计算下一次的需要传入的值
double initialangle1 =initialangle;
double initialangle2 =initialangle+this.angle1;
double initialangle3 =this.angle2+initialangle;
double x2=x+length*Math.cos(initialangle1);
double x3=x2+length*Math.cos(initialangle2);
double x4=x3+length*Math.cos(initialangle3);
double x5=x4+length*Math.cos(initialangle1);
double y2=y-length*Math.sin(initialangle1);
double y3=y2-length*Math.sin(initialangle2);
double y4=y3-length*Math.sin(initialangle3);
double y5=y4-length*Math.sin(initialangle1);
double length1=length/3;
con--;
//此为关键 计数的方式,理解只能靠自己了,为了从最小的单位开始绘制,如果从最大的单位开始,突出部分的底部会有上次绘制图形留下的线
if(con<0){
this.x=(int)x;
this.x2=(int)x2;
this.x3=(int)x3;
this.x4=(int)x4;
this.x5=(int)x5;
this.y=(int)y;
this.y2=(int)y2;
this.y3=(int)y3;
this.y4=(int)y4;
this.y5=(int)y5;
//g.setColor(new Color(0,(int)(x+y)%255, (int)(x+y)%255));
g.drawLine(this.x, this.y, this.x2, this.y2);
g.drawLine(this.x2, this.y2, this.x3, this.y3);
g.drawLine(this.x3, this.y3, this.x4, this.y4);
g.drawLine(this.x4, this.y4, this.x5, this.y5);
return;
}
//连续调用四次 ,因为一个基本图形有四条线段,每条线段都要“重新绘制”成一个基本图形 (实际上不能是重新绘制 ,因为会留一条线段,所以计数方式是关键,从最小的开始画)
drawKoch(x,y,initialangle1,length1,con);
System.out.println("11111111111111111");
drawKoch(x2,y2,initialangle2,length1,con);
System.out.println("222222222222222");
drawKoch(x3,y3,initialangle3,length1,con);
System.out.println("3333333333333333");
drawKoch(x4,y4,initialangle1,length1,con);
System.out.println("4444444444444444");

}




我调用方法时赋的初值 drawKoch(30,650,pi/4,150,5);结果如下图
[img]http://dl2.iteye.com/upload/attachment/0086/7221/bad0ae9a-2fd5-3245-82c8-71958c1c6f05.png[/img]

总结:计算下一次传入的参数值和计数方式是关键。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值