分形小实验

学习了一段时间分形,我分别尝试了用循环和递归的方实现分形。

一、使用循环实现分形:

实验1:

private void drawGraphy1() {
		int x3;
		int y3;
		Random rand=new Random();
		int x0=rand.nextInt(500);
		int y0=rand.nextInt(500);
		x3=x1+x0;
		y3=y1+y0;
		int x11=rand.nextInt(500);
		int y11=rand.nextInt(500);
		x4=x1+x11;
		y4=y1+y11;
		int x12=rand.nextInt(500);
		int y12=rand.nextInt(500);
		x5=x1+x12;
		y5=y1+y12;
		int x13=rand.nextInt(500);
		int y13=rand.nextInt(500);
		x6=x1+x13;
		y6=y1+y13;
		for(int j=0;j<50000;j++){
			int f=rand.nextInt(3);
			System.out.print(f);
			if(f==1){
				x3=(x3+x4)/2;
				y3=(y3+y4)/2;
				g.drawLine(x3,y3,x3,y3);//画P点与任意一点的中点
			}else if(f==2){
				x3=(x3+x5)/2;
				y3=(y3+y5)/2;
				g.drawLine(x3,y3,x3,y3);	
			}else if(f==0){
				x3=(x3+x6)/2;
				y3=(y3+y6)/2;
				g.drawLine(x3,y3,x3,y3);	
			}
			
			
		}
		
		
	}

 

 

实验2:

private void drawGraphy3() {
		float a=1.40f;
		float b=1.56f;
		float c=1.40f;
		float d=-6.56f;
		double x=0;
		double y=0;
		double x1=0;
		double y1=0;
		int x2=0;
		int y2=0;
		
		for(int i=0;i<100000;i++){
			x1=d*Math.sin(a*x)-Math.sin(b*y);
			y1=c*Math.cos(a*x)+Math.cos(b*y);
			x2=(int) (x1*30+300);
			y2=(int) (y1*30+300);
			x=x1;
			y=y1;
			g.drawLine(x2, y2, x2, y2);
		}
		
	}

 



 

实验3:

private void drawGraphy4() {
		float a=1f;
		float b=4f;
		float c=60f;
		double x=0;
		double y=0;
		double x1=0;
		double y1=0;
		int x2=0;
		int y2=0;
		int d=2;
	
		
		for(int i=0;i<50000;i++){
			x1=y-Math.signum(x)*Math.sqrt(Math.abs(b*x-c));
			y1=a-x;
			x2=(int) (x1*d+400);
			y2=(int) (y1*d+400);
			Color [] array={Color.WHITE,Color.BLUE,Color.GREEN,Color.ORANGE,Color.PINK,Color.RED,Color.YELLOW};
			Random rand=new Random();
			
			switch(i){
				case 1000:
					//d=d+2;
					Color c1=array[rand.nextInt(array.length)];
					g.setColor(c1);
					break;
				case 2000:
					//d=d+2;
					Color c2=array[rand.nextInt(array.length)];
					g.setColor(c2);
					break;
				case 3000:
					//d=d+2;
					Color c3=array[rand.nextInt(array.length)];
					g.setColor(c3);
					break;
				case 4000:
					//d=d+2;
					Color c4=array[rand.nextInt(array.length)];
					g.setColor(c4);
					break;
			}
			System.out.println("d="+d);
			
			x=x1;
			y=y1;
			g.drawLine(x2, y2, x2, y2);
			System.out.println("x1="+x1);
			System.out.println("y1="+y1);
			System.out.println("x2="+x2);
			System.out.println("y2="+y2);
		}
		
	}

 

 

 

 


实验4:

public void graphic4(int x1,int y1,int x2,int y2) {
		if(x1>0&&y1>0&&x2>0&&y2>0){
		    for(int i=0;i<25;i++){
		    	x1=x1-5;
				y1=y1+5;
				x2=x2+5;
				y2=y2+5;
				g.setColor(new Color(10*i,10*i,10*i));
				g.fillRect(x1,y1,Math.abs(x2-x1+20),15);
		    }
		} 
		
	}

 

 

 

二、使用递归实现分形:

实验1:谢尔宾斯基三角形

void graphic2(int x1,int y1,int x2,int y2,int count) {
		int x0=0;
		int y0=0;
		int x3=0;
		int y3=0;
		int x4=0;
		int y4=0;
		int x5=0;
		int y5=0;
		
		x0=(x1+x2)/2;
		y0=y1;
		x3=x0;
		y3=(int) (y1-Math.sqrt(3)*Math.abs(x2-x1)/2);
		g.drawLine(x1, y1, x2, y2);
		g.drawLine(x1, y1, x3, y3);
		g.drawLine(x3, y3, x2, y2);
		x4=(x1+x3)/2;
		y4=(y1+y3)/2;
		x5=(x2+x3)/2;
		y5=y4;
		g.drawLine(x4, y4, x0, y0);
		g.drawLine(x0, y0, x5, y5);
		g.drawLine(x4, y4, x5, y5);
		count--;
		if(count>0){
			graphic2(x4,y4,x5,y5,count);
			graphic2(x1,y1,x0,y0,count);
			graphic2(x0,y0,x2,y2,count);
		}
				
	}

 
 

 

实验2:谢尔宾斯基地毯

	void graphic3(int x1, int y1, int x2, int y2, int count) {
		
		int x3=0;
		int y3=0;
		int x4=0;
		int y4=0;
		x3=x1+Math.abs(x2-x1)/3;
		y3=y1+Math.abs(y2-y1)/3;
		x4=x2-Math.abs(x2-x1)/3;
		y4=y2-Math.abs(y2-y1)/3;
		g.drawLine(x1,y3,x2,y3);
		g.drawLine(x1,y4,x2,y4);
		g.drawLine(x3,y1,x3,y2);
		g.drawLine(x4,y1,x4,y2);
		count--;
		if(count>0){
			graphic3(x1,y1,x3,y3,count);
			graphic3(x3,y1,x4,y3,count);
			graphic3(x4,y1,x2,y3,count);
			graphic3(x1,y3,x3,y4,count);
			graphic3(x4,y3,x2,y4,count);
			graphic3(x1,y4,x3,y2,count);
			graphic3(x3,y4,x4,y2,count);
			graphic3(x4,y4,x2,y2,count);
		}
	}

 

 

实验3:

void graphic5(int x1, int y1, int x2, int y2, int k) {
		System.out.println("in---count:"+count);
		int x3=0;
		int y3=0;
		int x4=0;
		int y4=0;
		int x5=0;
		int y5=0;
		int x6=0;
		int y6=0;
		int x7=0;
		int y7=0;
		int x8=0;
		int y8=0;
		int x9=0;
		int y9=0;
		x3=(int) (x1-Math.sqrt(3)*(y2-y1)/2);
		y3=y2+(y2-y1)/2;
		x4=(int) (x1+Math.sqrt(3)*(y2-y1)/2);
		y4=y3;
		g.setColor(Color.RED);
		g.drawLine(x1, y1, x2, y2);
		g.setColor(Color.YELLOW);
		g.drawLine(x3, y3, x2, y2);
		g.setColor(Color.ORANGE);
		g.drawLine(x4, y4, x2, y2);
		//count--;
		k++;
		x5=x1;
		y5=(y1+y2)/2;
		x6=(x2+x3)/2;
		y6=(y2+y3)/2;
		x7=(x2+x4)/2;
		y7=y6;
		x8=(x1+x3)/2;
		y8=(y1+y3)/2;
		x9=(x1+x4)/2;
		y9=y8;
		if(count<6){
			if(k<count){
				graphic5(x1,y1,x5,y5,k);
				graphic5(x8,y8,x6,y6,k);
				graphic5(x9,y9,x7,y7,k);
			}
		}
		
		System.out.println("out---count:"+count);
	}

 

 

实验4:

void graphic6(int x1, int y1, int x2, int y2, int k) {
		 g.drawOval(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2));
		int x3=0;
		int y3=0;
		int x4=0;
		int y4=0;
		int x5=0;
		int y5=0;
		int x6=0;
		int y6=0;
		x3=x1;
		y3=y1+(y2-y1)/4;
		x4=x1+(x2-x1)/2;
		y4=y2-(y2-y1)/4;
		x5=x4;
		y5=y1+(y2-y1)/4;
		x6=x2;
		y6=y4;
		g.drawOval(x3, y3, Math.abs(x3-x4), Math.abs(y3-y4));
		g.drawOval(x5, y5, Math.abs(x5-x6), Math.abs(y5-y6));
		k--;
		if(k>0){
			graphic6(x3,y3,x4,y4,k);
			graphic6(x5,y5,x6,y6,k);
		}
		
	}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值