Recursive Pattern Generation in C++: Drawing Fractal Totems

Problem Analysis

The fractal totem pattern exhibits self-similarity properties where larger patterns are composed of smaller identical structures. For input size n, the pattern dimensions follow powers of 2.

Base Element

The fundamental building block is:

 /\
/__\

Recursive Construction

Larger patterns are formed by duplicating and arranging smaller ones:

  • Horizontal duplication creates width expansion
  • Vertical stacking with proper spacing builds height

Implementation Approach

Using a 2D character grid sized 1024×2048 to acommodate maximum input (n=10). The entire grid is initialized with spaces to prevent undefined behavior.

#include <iostream>
#include <cstring>
using namespace std;

class FractalGenerator {
private:
    static const int MAX_SIZE = 1024;
    char grid[MAX_SIZE][MAX_SIZE * 2];
    int levels;
    
    void initializeGrid() {
        for (int i = 0; i < MAX_SIZE; i++) {
            for (int j = 0; j < MAX_SIZE * 2; j++) {
                grid[i][j] = ' ';
            }
        }
    }
    
    void buildBasePattern(int row, int col) {
        grid[row][col + 1] = '/';
        grid[row][col + 2] = '\\';
        grid[row + 1][col] = '/';
        grid[row + 1][col + 1] = '_';
        grid[row + 1][col + 2] = '_';
        grid[row + 1][col + 3] = '\\';
    }
    
    void constructTotem(int size, int startRow, int startCol) {
        if (size == 1) {
            buildBasePattern(startRow, startCol);
            return;
        }
        
        int halfSize = 1 << (size - 1);  // 2^(size-1)
        
        // Generate four copies of smaller pattern
        constructTotem(size - 1, startRow, startCol + halfSize);
        constructTotem(size - 1, startRow, startCol);
        constructTotem(size - 1, startRow + halfSize, startCol);
        constructTotem(size - 1, startRow + halfSize, startCol + halfSize);
    }
    
public:
    void generate(int n) {
        levels = n;
        initializeGrid();
        constructTotem(n, 0, 0);
        
        int totalRows = 1 << n;  // 2^n
        int totalCols = 1 << (n + 1);  // 2^(n+1)
        
        for (int i = 0; i < totalRows; i++) {
            for (int j = 0; j < totalCols; j++) {
                cout << grid[i][j];
            }
            cout << endl;
        }
    }
};

int main() {
    int inputSize;
    cin >> inputSize;
    
    FractalGenerator generator;
    generator.generate(inputSize);
    
    return 0;
}

Alternative Binary Pattern Method

The pattern can also be generated using XOR-based binary sequences where each row corresponds to binary digits:

#include <iostream>
using namespace std;

int main() {
    int dimension;
    cin >> dimension;
    
    int rowCount = 1 << dimension;
    int pattern[1025] = {1};  // Initialize first element
    
    for (int row = 0; row < rowCount; row++) {
        // Leading spaces
        for (int space = 1; space < rowCount - row; space++) {
            cout << " ";
        }
        
        // Update pattern using XOR
        for (int idx = row; idx > 0; idx--) {
            pattern[idx] ^= pattern[idx - 1];
        }
        
        // Output based on row parity
        if (row % 2 == 0) {
            // Odd rows (1-indexed)
            for (int col = 0; col <= row; col++) {
                cout << (pattern[col] ? "/\\" : "  ");
            }
        } else {
            // Even rows (1-indexed)
            for (int col = 0; col <= row; col += 2) {
                cout << (pattern[col] ? "/__\\" : "    ");
            }
        }
        cout << endl;
    }
    
    return 0;
}

Tags: fractals Recursion pattern-generation binary-operations C++

Posted on Mon, 01 Jun 2026 00:56:40 +0000 by D