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.   When writing fractal in Java, different graphs invoke recursive implementations based on different representations, such as: Koch curves:     Code as follows: public void draw1 (int x1, int y1, int x2, int y2,int dep TH) {//Koch curve   keleyi.com         g.drawline (x1, y1, x2, y2);           I F (depth<=1)               return;           ELSE {//Get triple point               Double x11 = (x1 * 2 &N bsp;+ x2)  /3;               Double Y11 = (y1 * 2  + y2)/3;               &NBSp Double x22 = (x1 + x2 * 2)/3;               Double Y22 = (y1 + y2 * 2)/3;                 Double x33 = (x11 + x22)/2-(Y11-Y22) * MATH.SQRT (3)/2;               Double y33 = (Y11 + Y22)/2-(x22-x11) * MATH.SQRT (3)/2;                 G.setcolor (J.getbackground ());               g.drawline (int) x1, (int) y1, (int) x2, (int) y2);               G.setcolor (Color.Black);               DRAW1 (int) x1, (int) y1, (int) x11, (int) y11,depth-1);               DRAW1 (int) x11, (int) y11, (int) x33, (int) y33,depth-1);               DRAW1 (int) x22, (int) Y22, (int) x2, (int) y2,depth-1);               DRAW1 (int) x33, (int) y33, (int) x22, (int) y22,depth-1);           {     }     Square:     code: public void draw2 (int x 1, int y1, int m,int depth) {//square keleyi.com           G.fillrect (x1, y1, M, m);     & nbsp     m = M/3;           if (depth<=1)               return;           else{          Double x11 = x1-2 * m;           Double Y11 = y1-2 * m;             Double x22 = x1 + M;           Double Y22 = y1-2 * m;             Double x33 = x1 + 4 * m;           Double y33 = y1-2 * m;             Double x44 = x1-2 * m;           Double y44 = y1 + M;             DOUble x55 = x1 + 4 * m;           Double y55 = y1 + M;             Double x66 = x1-2 * m;           Double y66 = y1 + 4 * m;             Double x77 = x1 + M;           Double y77 = y1 + 4 * m;             Double x88 = x1 + 4 * m;           Double y88 = y1 + 4 * m;             DRAW2 (int) x11, (int) y11, (int) m,depth-1);             DRAW2 (int) x22, (int) Y22, (int) m,depth-1);             DRAW2 (int) x33, (int) y33, (int) m,depth-1);             DRAW2 (int) x44, (int) y44, (int) m,depth-1);             DRAW2 (int) x55, (int) y55, (int) m,depth-1);             DRAW2 (int) x66, (int) y66, (int) m,depth-1);     &NBsp       DRAW2 (int) x77, (int) y77, (int) m,depth-1);             DRAW2 (int) x88, (int) y88, (int) m,depth-1);           {       }     Xie bingying-Delta:     code: public void DRAW3 (int x1,int y1,int x2,int y2,int x3,int y3,int depth) {//triangle   keleyi.com           Doub Le s = math.sqrt ((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1));           g.drawline (x1,y1,x2,y2);           g.drawline (X2,Y2,X3,Y3);           g.drawline (X1,Y1,X3,Y3);  //     IF (s<3)  //         return;           if (depth<=1)               return;           else           {         /*  & nbsp       * Top triangle            */          double x11= (X1*3+X2)/4;           double y11=y1-(S/4) *math.sqrt (3);             double x12= (x1+x2*3)/4;           double y12=y11;             double x13= (X1+X2)/2;           double y13=y1;            /*           * left triangle         & nbsp  */          double X21=X1-S/4;           double y21= (y1+y3)/2;             double X22=X1+S/4;           double y22=y21;             double x23=x1;           double y23=y3;            /*           * right triangle       &NBsp    */          double X31=X2+S/4;           double y31= (y1+y3)/2;             double X32=X2-S/4;           double y32=y21;             double x33=x2;           double y33=y3;                        DRAW3 (int) x11, (int) y11, (int) x12, (int) Y12, (int) x13, (int) y13, depth-1);           DRAW3 (int) x21, (int) y21, (int) x22, (int) Y22, (int) x23, (int) y23, depth-1);           DRAW3 (int) x31, (int) y31, (int) x32, (int) y32, (int) x33, (int) y33, depth-1);           {     }     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.   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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.