Back

How to Construct the Graph

[Scroll to the bottom of the page to see the image this code constructs]

        // See https://www.tropicalcoder.com/APermutationOnCombinatorialAlgorithms.htm

        #define SCREEN_WIDTH 1152
        #define SCREEN_HEIGHT 640

        LPPOINT leftBranch(LPPOINT p, int length) {
            int x = p->x;
            int y = p->y;

            for (int i = 0; i < length; i++) {
                --x;
                --y;
                if (x == 0) return NULL;
                if (y == 0) return NULL;
                putColour(x, y, 0x0040FF40);
            }
            p->x = x;
            p->y = y;
            return p;
        }

        LPPOINT rightBranch(LPPOINT p, int length) {
            int x = p->x;
            int y = p->y;

            for (int i = 0; i < length; i++) {
                ++x;
                --y;
                if (x == SCREEN_WIDTH) return NULL;
                if (y == 0) return NULL;
                putColour(x, y, 0x0040FF40);
            }
            p->x = x;
            p->y = y;
            return p;
        }

        void recurseBranches(int x, int y, int length) {
            POINT point, *p;

            point.x = x;
            point.y = y;
            p = leftBranch(&point, length);
            if (p != NULL) {
                if ((length >> 1) != 0) {
                    recurseBranches(p->x, p->y, length >> 1);
                }
            }

            point.x = x;
            point.y = y;
            p = rightBranch(&point, length);
            if (p != NULL) {
                if ((length >> 1) != 0) {
                    recurseBranches(p->x, p->y, length >> 1);
                }
            }
        }

        void __cdecl thread(void *pv) {
            RECT rect;
            int x, y;

            x = SCREEN_WIDTH / 2;
            y = SCREEN_HEIGHT - 64;
            putColour(x, y, 0x0040FF40);
            recurseBranches(x, y, 256);

            GetClientRect(hWnd, &rect);
            InvalidateRect(hWnd, &rect, false);
            UpdateWindow(hWnd);
            bThreadLive = false;
        }
    
Fractal pattern

This is the image produced by the code shown.