class="MsoNormal" style="text-align: center; margin: 0cm 0cm 0pt;">分形(二)——分形树
上次我们画出了谢尔宾斯基三角形,这次我们所画分形图形同样也是比较简单的——分形树,记得在上次的递归里~我们传入的参数是所绘的点的坐标,但这种方法并不一定的最好的,在绘制分形图案的时候,使用递归,所传参数应根据实际情况来定:(可以是角度,变长等)
同学们可以自己也试着画一下分形:这是今天的题目:
分形树一次递归调用:
?
分形树两次递归调用:
?
?
分形树六次递归调用:
?
?
分形树十次递归调用:
?
?
分形树二十五次递归调用
?
?
后面的我不敢往下试了——机子会爆掉的……
?
?
下面是绘制次分形树的方法:
package Elps;
?
?
import java.awt.Graphics;
?
import javax.swing.JFrame;
?
public class Main extends JFrame {
?
????????? /**
????????? ?* @param args
????????? ?*/
????????? public static void main(String[] args) {
?????????????????? // TODO Auto-generated method stub
?????????????????? Main a = new Main();
?????????????????? a.draw();
?????? }
?????????
????????? ?public void draw(){//绘制窗体,添画布
??? ????????? ?this.setSize(1000,700);//
??? ????????? ?this.setLocationRelativeTo(null);
??? ????????? ?this.setDefaultCloseOperation(3);
??? ????????? ?this.setVisible(true);
??? ????????? ?Graphics g = this.getGraphics();
?
????????? ?}
????????? ?
????????? ?public void paint(Graphics g){
??????????????????????????? super.paint(g);
??????????????????????????? this.Show(500,550,100, Math.PI/2,0,Math.PI/6,25,g);
??????????????????????????? //(Math.PI为180°)
????????? ?}
????????? ?
????????? ?public void Show(double x0,double y0,double l,double a,double b,double c,double count,Graphics g){
????????? ???? double x2;
????????? ???? double y2;
????????? ???? double x3;
????????? ???? double y3;
????????? ???? double x4;
????????? ???? double y4;
????????? ???? double x5;
????????? ???? double y5;
????????? ???? if(count<1)
????????? ???? {
????????? ??? ???????? ?return;
????????? ???? }//判断是否继续进行递归调用,注意:判断一定要放在递归调用之前,否则这段代码将永远不会被执行
???????? x2 = x0 - l*Math.cos(a);
???????? y2 = y0 - l*Math.sin(a);
???????? x3 = x2 - l*Math.cos(b);
???????? y3 = y2 - l*Math.sin(b);
???????? x4 = x0 - l*Math.cos(b);
???????? y4 = y0 - l*Math.sin(b);
???????? x5 = x2 - l*Math.cos(Math.PI/6)*Math.cos(c);
???????? y5 = y2 - l*Math.cos(Math.PI/6)*Math.sin(c);
//计算五个点的位置,以右下点为(X0,Y0)
???????? g.drawLine((int)x0, (int)y0, (int)x2, (int)y2);
???????? g.drawLine((int)x2, (int)y2, (int)x3, (int)y3);
???????? g.drawLine((int)x3, (int)y3, (int)x4, (int)y4);
???????? g.drawLine((int)x4, (int)y4, (int)x0, (int)y0);
???????? g.drawLine((int)x2, (int)y2, (int)x5, (int)y5);
???????? g.drawLine((int)x5, (int)y5, (int)x3, (int)y3);
//划线——注意方法所需要的数据类型
????????? ???? Show(x2,y2,l*Math.cos(Math.PI/6),a+Math.PI/6,b+Math.PI/6,c+Math.PI/6,count-1,g);
????????? ???? Show(x5,y5,l*Math.sin(Math.PI/6),a-Math.PI/3,b-Math.PI/3,c-Math.PI/3,count-1,g);
//进行递归调用(注意传到方法里的点是相对于新正方形的右下点)
????????? ?}
?
?
?
????????? ????
????????? ?}
?