Previously written several sections of the algorithm code, used to test 2d graphics performance is also good __ algorithm

Source: Internet
Author: User
#include <fractal.h> #include <gfx.h> #include <math.h> #include <debug.h> #define PI 3.141
        592654 void Koch (unsigned int h, int ax, int ay, int bx, int by, int c) {int CX, CY, DX, DY, ex, EY;

        Double L, Alpha;
        if ((bx-ax) * (Bx-ax) + (by-ay) * (By-ay)) <c) {Gfx_lineto (H,ax, Ay, bx, by);
                else {cx = ax + (BX-AX)/3;

                cy = ay + (by-ay)/3;
                ex = BX-(BX-AX)/3;

                EY = by-(By-ay)/3;

                L = sqrt ((double) ex-cx) * (EX-CX) + (ey-cy) * (Ey-cy));

                Alpha = Atan ((double) ey-cy)/(EX-CX));
                if ((EX-CX) < 0) {alpha = = PI;
                dy = cy + sin (alpha + pi/3) * l;

         DX = cx + cos (alpha + pi/3) * l;       Koch (h, Ax, Ay, CX, CY, c);
                Koch (H, ex, EY, BX, by, c);
                Koch (H, CX, CY, DX, DY, c);
        Koch (h, DX, DY, ex, EY, c);  } void Sierpinski (unsigned int h, int x, int y, int L, int n) {int x1, y1, x2, y2, x3, Y3, X01,

        X03, y03;
                if (n = = 1) {X1=X-L/2;
                y1=y+l* (Tan (PI/6))/2;
                X2=X+L/2;
                y2=y+l* (Tan (PI/6))/2;
                X3=x;
                y3=y-l* (Tan (PI/6));
                Gfx_lineto (H, x1, y1, x2, y2);
                Gfx_lineto (H, x2, y2, X3, y3);
        Gfx_lineto (H, X3, Y3, x1, y1);
                else {X01=X-L/4;
                y01=y+l* (Tan (PI/6))/4;
                X02=X+L/4;
                y02=y+l* (Tan (PI/6))/4;
                X03=x;
                y03=y-l* (Tan (PI/6))/2;
                Sierpinski (H, x01,y01,l/2,n-1);
           Sierpinski (H, x02,y02,l/2,n-1);     Sierpinski (H, x03,y03,l/2,n-1);
        } void Ramus (unsigned int h, int x, int y, float alpha, int L, int n) {int x1, y1;

        float alpha_l, Alpha_r;
        if (n > 0) {x1=x+cos (alpha) *l;
        Y1=y-sin (Alpha) *l;
        Gfx_lineto (h, x, Y, x1, y1);
        ALPHA_L=ALPHA+PI/8;
        ALPHA_R=ALPHA-PI/8;
        L=2*L/3;
        Ramus (H, x1,y1,alpha_l,l,n-1);
        Ramus (H, x1,y1,alpha_r,l,n-1); } #define B (PI/6) #define S1 2 #define S2 3 #define S3 1.4 void Tree (unsigned int h, int x, int y

        , int L, float A, float C) {int x1, y1, x2, y2, x1l, y1l, x2l, y2l, x1r, y1r, X2R;
                if (l>s1) {x2 = x + cos (A) * L;
                y2 = Y-sin (A) * L;
                x2l = x2 + cos (a+b) * L/S2;
                y2l = Y2-sin (a+b) * L/S2;
                X2R = x2 + cos (a-b) * L/S2;     Y2r= Y2-sin (a-b) * L/S2;
                x1 = x + cos (A) * L/S2;
                Y1 = Y-sin (A) * L/S2;
                x1l = x1 + cos (a+b) * L/S2;
                y1l = Y1-sin (a+b) * L/S2;
                X1R = x1 + cos (a-b) * L/S2;

                Y1R = Y1-sin (a-b) * L/S2;
                /*!~30%*/Gfx_lineto (h, x, Y, x2, y2);
                Gfx_lineto (H, x2, Y2, x2l, y2l);
                Gfx_lineto (H, x2, Y2, x2r, y2r);
                Gfx_lineto (H, x1, y1, x1r, y1r);

                Gfx_lineto (H, x1, y1, x1l, y1l);
                Tree (h, x2, Y2, L/s3, a-c, C);
                Tree (H, x2l, y2l, L/s2, A+b, C);
                Tree (H, X2R, Y2R, L/s2, A-b, C);
                Tree (H, x1r, y1r, L/s2, A-b, C);
        Tree (H, x1l, y1l, L/s2, A+b, C); }
}


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.