# Java fractal drawing Koch snowflake curve code sharing

Source: Internet
Author: User

The

Part is in some form similar to the whole, called fractal, and the Koch curve is a geometric curve shaped like a snowflake, so it is called a snowflake curve, which is one of the fractal curves, which is described as follows

Let's start with an example: we can see that a small cluster of broccoli is a branch of the whole flower clusters, and they have a similar shape at different scales. In other words, smaller branches can get a flower cluster almost exactly the same as the whole by amplifying the appropriate proportions. So we can say that the broccoli cluster is an example of fractal. Fractals generally have the following characteristics: There are fine structures on any small scale, so irregular that it is difficult to describe the traditional Euclidean geometry, at least roughly or arbitrarily, the number of self-similar Hausdorff dimension is larger than the topological dimension; there is a simple recursive definition. (i) The fractal set has the details of the proportions at any small scale, or it has a fine structure. (ii) The fractal set cannot be described in the traditional geometric language, it is neither a locus of points satisfying certain conditions, nor a solution set of some simple equations. (iii) The fractal set has a self-similar form, which may be self-similar or statistically self-similar. (iv) The "fractal Dimension" of a fractal set is strictly greater than its corresponding topological dimension. (v) In most interesting cases, the fractal set is defined by a very simple method and may be generated by the iteration of the transformation. &nbsp; When writing fractal in Java, different graphs invoke recursive implementations based on different representations, such as: Koch curves: &nbsp; &nbsp; Code as follows: public void draw1 (int x1, int y1, int x2, int y2,int dep TH) {//Koch curve &nbsp; keleyi.com &nbsp; &nbsp; &nbsp; &nbsp; g.drawline (x1, y1, x2, y2); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; I F (depth&lt;=1) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ELSE {//Get triple point &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double x11 = (x1 * 2 &N bsp;+ x2) &nbsp;/3; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double Y11 = (y1 * 2 &nbsp;+ y2)/3; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &NBSp Double x22 = (x1 + x2 * 2)/3; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double Y22 = (y1 + y2 * 2)/3; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double x33 = (x11 + x22)/2-(Y11-Y22) * MATH.SQRT (3)/2; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double y33 = (Y11 + Y22)/2-(x22-x11) * MATH.SQRT (3)/2; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; G.setcolor (J.getbackground ()); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.drawline (int) x1, (int) y1, (int) x2, (int) y2); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; G.setcolor (Color.Black); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DRAW1 (int) x1, (int) y1, (int) x11, (int) y11,depth-1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DRAW1 (int) x11, (int) y11, (int) x33, (int) y33,depth-1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DRAW1 (int) x22, (int) Y22, (int) x2, (int) y2,depth-1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DRAW1 (int) x33, (int) y33, (int) x22, (int) y22,depth-1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; Square: &nbsp; &nbsp; code: public void draw2 (int x 1, int y1, int m,int depth) {//square keleyi.com &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; G.fillrect (x1, y1, M, m); &nbsp; &nbsp; & nbsp &nbsp; &nbsp; m = M/3; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (depth&lt;=1) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double x11 = x1-2 * m; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double Y11 = y1-2 * m; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double x22 = x1 + M; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double Y22 = y1-2 * m; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double x33 = x1 + 4 * m; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double y33 = y1-2 * m; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double x44 = x1-2 * m; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double y44 = y1 + M; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DOUble x55 = x1 + 4 * m; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double y55 = y1 + M; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double x66 = x1-2 * m; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double y66 = y1 + 4 * m; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double x77 = x1 + M; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double y77 = y1 + 4 * m; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double x88 = x1 + 4 * m; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double y88 = y1 + 4 * m; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DRAW2 (int) x11, (int) y11, (int) m,depth-1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DRAW2 (int) x22, (int) Y22, (int) m,depth-1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DRAW2 (int) x33, (int) y33, (int) m,depth-1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DRAW2 (int) x44, (int) y44, (int) m,depth-1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DRAW2 (int) x55, (int) y55, (int) m,depth-1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DRAW2 (int) x66, (int) y66, (int) m,depth-1); &nbsp; &nbsp; &NBsp &nbsp; &nbsp; &nbsp; DRAW2 (int) x77, (int) y77, (int) m,depth-1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DRAW2 (int) x88, (int) y88, (int) m,depth-1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; Xie bingying-Delta: &nbsp; &nbsp; code: public void DRAW3 (int x1,int y1,int x2,int y2,int x3,int y3,int depth) {//triangle &nbsp; keleyi.com &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Doub Le s = math.sqrt ((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.drawline (x1,y1,x2,y2); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.drawline (X2,Y2,X3,Y3); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.drawline (X1,Y1,X3,Y3); &nbsp;//&nbsp; &nbsp; &NBSP;IF (s&lt;3) &nbsp;//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (depth&lt;=1) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*&nbsp; & nbsp &nbsp; &nbsp; &nbsp;&nbsp;* Top triangle &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double x11= (X1*3+X2)/4; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double y11=y1-(S/4) *math.sqrt (3); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double x12= (x1+x2*3)/4; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double y12=y11; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double x13= (X1+X2)/2; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double y13=y1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* left triangle &nbsp; &nbsp; &nbsp; &nbsp; & nbsp &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double X21=X1-S/4; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double y21= (y1+y3)/2; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double X22=X1+S/4; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double y22=y21; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double x23=x1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double y23=y3; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* right triangle &nbsp; &nbsp; &nbsp; &NBsp &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double X31=X2+S/4; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double y31= (y1+y3)/2; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double X32=X2-S/4; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double y32=y21; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double x33=x2; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double y33=y3; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DRAW3 (int) x11, (int) y11, (int) x12, (int) Y12, (int) x13, (int) y13, depth-1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DRAW3 (int) x21, (int) y21, (int) x22, (int) Y22, (int) x23, (int) y23, depth-1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DRAW3 (int) x31, (int) y31, (int) x32, (int) y32, (int) x33, (int) y33, depth-1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; The Koch curve is a geometric curve shaped like a snowflake, so it's called a snowflake curve, which is one of the fractal curves, as follows: 1 , arbitrarily draw a positive triangle, and put each side three equal; 2, take three evenly divided side of the middle section for the edge outward as a positive triangle, and the "middle section" to wipe off; 3, repeat the above two steps, draw a smaller triangle. 4, repeating until infinity, the curve is called the Koch curve. &nbsp; Summary: Fractal is a very fun thing, according to their own wonderful imagination can draw a lot of beautiful graphics, not only already exist, you can create your own graphics!
Related Keywords:
Related Article

## E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

## Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

## Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD