目录
一、核心点
二、实例
三、总结
一、核心点
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, xn