The initial Java -- Based Fractal

Recommended for you: Get network issues from WhatsUp Gold. Not end users.


Making making making in the blink of an eye, the big half two semester has passed quickly, although the big two of course a little busy, but still persist in learning a little Java ~ last blog is the realization of a java drawing board, but the implementation is very simple functions. For example, draw some simple graphics, a straight line, a rectangle, a cube of what. . . Knowing this, we can use the recursive and iterative to draw some more beautiful graphics. For example, we all know that draw a circle on the drawing board, you can use the fillOval method, but how to let us draw the circle looks more like a ball, you see

for(int i=0;i<256;i++){
                g.setColor(new Color(0,0,255-i));
	g.fillOval(200+i/2, 200+i/2, 255-i, 255-i);
				
	try{
		Thread.sleep(30);
	}catch(InterruptedException e1){

                }
				
}

 

Making everyone look, that is more like a ball, but the method is very simple, is to use the color change shade, then fractal in a round draw, but every time a painting will circle radius decreased a bit, it looks like a ball.

 

Making what, then turn to the subject, first of all you see a realization by the iterative method of fractal, the specific algorithm is the way x2=Math.sin (a*y1) -Math.cos (b*x1); y2=Math.sin (c*x1) -Math.cos (d*y1); a=-2; b=-2; c=-1.2; d=2; but a start painting, form what are not later, in God's guidance to know, the original X2, the results calculated by Y2 is a negative number, they should be appropriate amplification, specific methods are as follows:

for(int i=0;i<100000;i++){
	//Random colors? 
	g.setColor(new Color(0,0,random.nextInt(256)));
	//Method of drawing fractal, fractal iterative
	x2=Math.sin(a*y1)-Math.cos(b*x1);
	y2=Math.sin(c*x1)-Math.cos(d*y1);
	int srcX=(int) (100*x2+200);
	int srcY=(int) (100*y2+200);			
	g.drawLine(srcX, srcY, srcX, srcY);				x1=x2;
	y1=y2;
}

 

Making making even though the fractal is drawn out, however feel the color effect is not very ideal, if can make like a neon light effect is perfect, but I don't know... You know you can private chat me ah

Making making then recursive fractal implementation we first look at the example, first is the recursive implementation of the fractal of the most simple example, nonsense not say, we look at the code

public void draw1(double  x1,double  y1,double  x2,double  y2,int n){
	if(n<=0){
		return;
	}	
	g.drawLine((int)x1, (int)y1, (int)x2, (int)y2);
	//To get the coordinates of the new point
	double  xx1 = x1;
	double  xx2 = x1+(x2-x1)/3;
	double  xx3 = x1 + 2*(x2 - x1)/3 ;
	double  xx4 = x2;
	double  yy1 = y1 + 20;
	double  yy2 = y2 + 20;
	double  yy3 = y1 + 20;
	double  yy4= y2 + 20;
	//Recursive calls
	draw1(xx1,yy1,xx2,yy2,n -1);//Draw a straight line on the left side of the
	//Recursive calls
	draw1(xx3,yy3,xx4,yy4,n -1);//Draw a straight line on the right		
	}

 

     

Then we look fractal more high-end next, it was called the Sher Pinsky triangle:

public void draw3(int xa,int ya,int xb,int yb,int xc,int yc,int n){	
	if(n<=0){
		return;
	}
	//The next step is to coordinate calculation of drawing point
	int xxa=(xa+xb)/2;
	int yya=(ya+yb)/2;
	int xxb=(xb+xc)/2;
	int yyb=(yb+yc)/2;
	int xxc=(xc+xa)/2;
	int yyc=(yc+ya)/2;	
	g.drawLine(xxa, yya, xxb, yyb);
	g.drawLine(xxb, yyb, xxc, yyc);
	g.drawLine(xxc, yyc, xxa, yya);		
	draw3(xa,ya,xxa,yya,xxc,yyc,n-1);
	draw3(xb,yb,xxa,yya,xxb,yyb,n-1);
	draw3(xc,yc,xxc,yyc,xxb,yyb,n-1);
	
	}

 

Making making in the triangle add some random color, and give him a death cycle, so the triangle looks like has been in flash, just awesome

Making making the last one is the Koch curve, the specific algorithm is:

In order toL system:
Characters: F
Constant : +, −
Axiom : F++F++F
Rules: F → F−F++F−F
F : Forward
- : Turn left at the 60°
+ : Turn right at the 60°
Although these are only some of the very basis of fractal, but we also can draw the fractal some more beautiful, more creative, but the specific algorithm requires our own slowly to study ~ refueling
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Nathaniel at December 12, 2013 - 4:45 PM