目录
一、核心点
二、实例
三、总结
一、核心点
1.把握递归终止条件
2.弄清递归形式。
二、实例
1.康托尔集
1 int yoffset = 10; 2 3 public void draw(Graphics g,int n,int x1,int y1,int x2,int y2) { 4 if(n<1) { //递归终止条件 5 return; 6 } 7 n--; 8 //画线 9 g.drawLine(x1, y1, x2, y2); 10 11 int tempx = (x2 - x1)/3; 12 int tempy = (y2 - y1)/3; 13 14 draw(g,n,x1,y1+yoffset,x1+tempx,y1+yoffset+tempy); //递归 15 draw(g,n,x2-tempx,y2+yoffset-tempy,x2,y2+yoffset); //递归 16 }
2.谢尔斯宾三角形
public void draw(Graphics g,int n,int x1,int y1,int x2,int y2,int x3,int y3) { int xn1 = (x2+x1)/2; //画图形 int yn1 = (y2+y1)/2; int xn2 = (x3+x2)/2; int yn2 = (y3+y2)/2; int xn3 = (x1+x3)/2; int yn3 = (y1+y3)/2; g.drawLine(xn1, yn1, xn2, yn2); g.drawLine(xn2, yn2, xn3, yn3); g.drawLine(xn3, yn3, xn1, yn1); n--; //递归终止条件 if(n<0) return; draw(g,n,x1,y1,xn1,yn1,xn3,yn3); //递归 draw(g,n,x2,y2,xn1,yn1,xn2,yn2); draw(g,n,x3,y3,xn2,yn2,xn3,yn3); }
3.谢尔斯宾地毯
public void draw(Graphics g,int n,int x1,int y1,int x3,int y3) { int tempx = (x3 - x1)/3; //画正方体 int tempy = (y3 - y1)/3; int xn1 = x1 + tempx; int yn1 = y1 + tempy; int xn3 = x1 + tempx * 2; int yn3 = y1 + tempy * 2; g.fillRect(xn1, yn1, tempx, tempy); n--; if(n<=0) { return;} //递归结束条件 draw(g,n,x1,y1,xn1,yn1); //开始递归,八个维度 draw(g,n,x1,yn1,xn1,yn3); draw(g,n,x1,yn3,xn1,y3); draw(g,n,xn1,yn3,xn3,y3); draw(g,n,xn3,yn3,x3,y3); draw(g,n,xn3,yn1,x3,yn3); draw(g,n,xn3,y1,x3,yn1); draw(g,n,xn1,y1,xn3,yn1); }
4.门格海绵
//画立方体
public void draw(Graphics g,int x1 ,int y1,int width,int height,int dx,int dy) { g.setColor(new Color(0,160,160)); g.fillRect(x1 , y1, width, height); int[] xx1 = {x1,x1+width,x1+width+dx,x1+dx}; int[] yy1 = {y1,y1,y1-dy,y1-dy}; g.setColor(new Color(0,50,50)); g.fillPolygon(xx1, yy1, 4); int[] xx2 = {x1+width,x1+width+dx,x1+width+dx,x1+width}; int[] yy2 = {y1,y1-dy,y1-dy+height,y1+height}; g.setColor(new Color(0,100,100)); g.fillPolygon(xx2, yy2, 4); } //递归 public void drawMGHM(int n,Graphics g,int x1,int y1,int width,int height,int dx,int dy) { n--; if(n<0) { //往里递归结束的时候,开始画 画出基本单元就开始递归 draw(g,x1,y1,width,height,dx,dy); return; } width = width/3; //长度缩减为1/3 height = height/3; dx = dx/3; dy = dy/3; //frist // draw(g,x1,y1,width,height,dx,dy); //1 // draw(g,x1-dx,y1+dy,width,height,dx,dy); //4 // draw(g,x1-2*dx,y1+2*dy,width,height,dx,dy); //7 // draw(g,x1+width,y1,width,height,dx,dy); //2 // draw(g,x1+2*width,y1,width,height,dx,dy); //3 // draw(g,x1+2*width-dx,y1+dy,width,height,dx,dy); //6 // draw(g,x1+width-2*dx,y1+2*dy,width,height,dx,dy); //8 // draw(g,x1+2*width-2*dx,y1+2*dy,width,height,dx,dy); //9 // //second // draw(g,x1,y1-height,width,height,dx,dy); //1 // draw(g,x1+2*width,y1-height,width,height,dx,dy); //3 // draw(g,x1-2*dx,y1+2*dy-height,width,height,dx,dy); //7 // draw(g,x1+2*width-2*dx,y1+2*dy-height,width,height,dx,dy); //9 // //third // draw(g,x1,y1-2*height,width,height,dx,dy); //1 // draw(g,x1-dx,y1+dy-2*height,width,height,dx,dy); //4 // draw(g,x1-2*dx,y1+2*dy-2*height,width,height,dx,dy); //7 // draw(g,x1+width,y1-2*height,width,height,dx,dy); //2 // draw(g,x1+2*width,y1-2*height,width,height,dx,dy); //3 // draw(g,x1+2*width-dx,y1+dy-2*height,width,height,dx,dy); //6 // draw(g,x1+width-2*dx,y1+2*dy-2*height,width,height,dx,dy); //8 // draw(g,x1+2*width-2*dx,y1+2*dy-2*height,width,height,dx,dy); //9
//20个维度的递归 //frist drawMGHM(n,g,x1,y1,width,height,dx,dy); //1 drawMGHM(n,g,x1-dx,y1+dy,width,height,dx,dy); //4 drawMGHM(n,g,x1-2*dx,y1+2*dy,width,height,dx,dy); //7 drawMGHM(n,g,x1+width,y1,width,height,dx,dy); //2 drawMGHM(n,g,x1+2*width,y1,width,height,dx,dy); //3 drawMGHM(n,g,x1+2*width-dx,y1+dy,width,height,dx,dy); //6 drawMGHM(n,g,x1+width-2*dx,y1+2*dy,width,height,dx,dy); //8 drawMGHM(n,g,x1+2*width-2*dx,y1+2*dy,width,height,dx,dy); //9 //second drawMGHM(n,g,x1,y1-height,width,height,dx,dy); //1 drawMGHM(n,g,x1+2*width,y1-height,width,height,dx,dy); //3 drawMGHM(n,g,x1-2*dx,y1+2*dy-height,width,height,dx,dy); //7 drawMGHM(n,g,x1+2*width-2*dx,y1+2*dy-height,width,height,dx,dy); //9 //third drawMGHM(n,g,x1,y1-2*height,width,height,dx,dy); //1 drawMGHM(n,g,x1-dx,y1+dy-2*height,width,height,dx,dy); //4 drawMGHM(n,g,x1-2*dx,y1+2*dy-2*height,width,height,dx,dy); //7 drawMGHM(n,g,x1+width,y1-2*height,width,height,dx,dy); //2 drawMGHM(n,g,x1+2*width,y1-2*height,width,height,dx,dy); //3 drawMGHM(n,g,x1+2*width-dx,y1+dy-2*height,width,height,dx,dy); //6 drawMGHM(n,g,x1+width-2*dx,y1+2*dy-2*height,width,height,dx,dy); //8 drawMGHM(n,g,x1+2*width-2*dx,y1+2*dy-2*height,width,height,dx,dy); //9 }
门格海绵,特殊点在于图形是立体的,通过不同面不同颜色来体现立体感。不能递归的时候画,必须根据图形覆盖原理,由里往外画,由上往下画,所以必须在往里递归的结束时候才能开始画。
三、总结
分形可以帮助理解递归,让初学递归的人更清楚更清晰的感觉到递归过程。画分形的过程也需要使用到递归这一方法。两者相互结合。
用递归画分形图形,可以先画出基本单元,画出基本单元后,再将画的基本单元替换为递归函数即可。画立体图形需从由里往外画,由上往下画,以免被覆盖。
最后一句,分形很美。