How to resolve the algorithm Barnsley fern step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Barnsley fern step by step in the C++ programming language

Table of Contents

Problem Statement

A Barnsley fern is a fractal named after British mathematician Michael Barnsley and can be created using an iterated function system (IFS).

Create this fractal fern, using the following transformations: Starting position: x = 0, y = 0

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Barnsley fern step by step in the C++ programming language

Source Code 1:

Explanation:

This C++ code creates a fractal image of the Barnsley fern using Windows GDI+ and random number generation.

  • Class myBitmap: Defines a custom class for managing bitmap operations. It provides methods for creating, clearing, setting brush and pen colors, and saving the bitmap to a file.

  • Class fern: Represents the Barnsley fern fractal. It has a draw() method that generates the fractal by iteratively drawing pixels according to a set of rules.

  • main() function:

    • Initializes the random number generator using the current time as a seed.
    • Creates an instance of the fern class and calls its draw() method.

Source Code 2:

Explanation:

This C++ code generates the Barnsley fern fractal using the Qt framework.

  • barnsleyFern() function:

    • Generates the fern fractal as a binary image and saves it to a file.
    • Uses a random number generator to determine the next coordinate according to the Barnsley fern rules.
    • Creates a QImage object and sets its pixel values based on the generated coordinates.
  • main() function:

    • Verifies the command line arguments.
    • Calls the barnsleyFern() function to generate the fractal and save the image.

Key Differences:

The main difference between the two code snippets is the platform they use for graphics handling:

  • Source Code 1 uses Windows GDI+ for bitmap manipulation and drawing.
  • Source Code 2 uses the Qt framework for creating and saving the fractal image.

Both implementations generate a Barnsley fern fractal, but they use different platform-specific libraries for graphics and image handling.

Source code in the cpp programming language

#include <windows.h>
#include <ctime>
#include <string>

const int BMP_SIZE = 600, ITERATIONS = static_cast<int>( 15e5 );

class myBitmap {
public:
    myBitmap() : pen( NULL ), brush( NULL ), clr( 0 ), wid( 1 ) {}
    ~myBitmap() {
        DeleteObject( pen ); DeleteObject( brush );
        DeleteDC( hdc ); DeleteObject( bmp );
    }
    bool create( int w, int h ) {
        BITMAPINFO bi;
        ZeroMemory( &bi, sizeof( bi ) );
        bi.bmiHeader.biSize        = sizeof( bi.bmiHeader );
        bi.bmiHeader.biBitCount    = sizeof( DWORD ) * 8;
        bi.bmiHeader.biCompression = BI_RGB;
        bi.bmiHeader.biPlanes      = 1;
        bi.bmiHeader.biWidth       =  w;
        bi.bmiHeader.biHeight      = -h;
        HDC dc = GetDC( GetConsoleWindow() );
        bmp = CreateDIBSection( dc, &bi, DIB_RGB_COLORS, &pBits, NULL, 0 );
        if( !bmp ) return false;
        hdc = CreateCompatibleDC( dc );
        SelectObject( hdc, bmp );
        ReleaseDC( GetConsoleWindow(), dc );
        width = w; height = h;
        return true;
    }
    void clear( BYTE clr = 0 ) {
        memset( pBits, clr, width * height * sizeof( DWORD ) );
    }
    void setBrushColor( DWORD bClr ) {
        if( brush ) DeleteObject( brush );
        brush = CreateSolidBrush( bClr );
        SelectObject( hdc, brush );
    }
    void setPenColor( DWORD c ) {
        clr = c; createPen();
    }
    void setPenWidth( int w ) {
        wid = w; createPen();
    }
    void saveBitmap( std::string path ) {
        BITMAPFILEHEADER fileheader;
        BITMAPINFO       infoheader;
        BITMAP           bitmap;
        DWORD            wb;
        GetObject( bmp, sizeof( bitmap ), &bitmap );
        DWORD* dwpBits = new DWORD[bitmap.bmWidth * bitmap.bmHeight];
        ZeroMemory( dwpBits, bitmap.bmWidth * bitmap.bmHeight * sizeof( DWORD ) );
        ZeroMemory( &infoheader, sizeof( BITMAPINFO ) );
        ZeroMemory( &fileheader, sizeof( BITMAPFILEHEADER ) );
        infoheader.bmiHeader.biBitCount = sizeof( DWORD ) * 8;
        infoheader.bmiHeader.biCompression = BI_RGB;
        infoheader.bmiHeader.biPlanes = 1;
        infoheader.bmiHeader.biSize = sizeof( infoheader.bmiHeader );
        infoheader.bmiHeader.biHeight = bitmap.bmHeight;
        infoheader.bmiHeader.biWidth = bitmap.bmWidth;
        infoheader.bmiHeader.biSizeImage = bitmap.bmWidth * bitmap.bmHeight * sizeof( DWORD );
        fileheader.bfType    = 0x4D42;
        fileheader.bfOffBits = sizeof( infoheader.bmiHeader ) + sizeof( BITMAPFILEHEADER );
        fileheader.bfSize    = fileheader.bfOffBits + infoheader.bmiHeader.biSizeImage;
        GetDIBits( hdc, bmp, 0, height, ( LPVOID )dwpBits, &infoheader, DIB_RGB_COLORS );
        HANDLE file = CreateFile( path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 
            FILE_ATTRIBUTE_NORMAL, NULL );
        WriteFile( file, &fileheader, sizeof( BITMAPFILEHEADER ), &wb, NULL );
        WriteFile( file, &infoheader.bmiHeader, sizeof( infoheader.bmiHeader ), &wb, NULL );
        WriteFile( file, dwpBits, bitmap.bmWidth * bitmap.bmHeight * 4, &wb, NULL );
        CloseHandle( file );
        delete [] dwpBits;
    }
    HDC getDC() const     { return hdc; }
    int getWidth() const  { return width; }
    int getHeight() const { return height; }
private:
    void createPen() {
        if( pen ) DeleteObject( pen );
        pen = CreatePen( PS_SOLID, wid, clr );
        SelectObject( hdc, pen );
    }
    HBITMAP bmp; HDC    hdc;
    HPEN    pen; HBRUSH brush;
    void    *pBits; int    width, height, wid;
    DWORD    clr;
};
class fern {
public:
    void draw() {
        bmp.create( BMP_SIZE, BMP_SIZE );
        float x = 0, y = 0; HDC dc = bmp.getDC();
        int hs = BMP_SIZE >> 1;
        for( int f = 0; f < ITERATIONS; f++ ) {
            SetPixel( dc, hs + static_cast<int>( x * 55.f ), 
                      BMP_SIZE - 15 - static_cast<int>( y * 55.f ), 
                      RGB( static_cast<int>( rnd() * 80.f ) + 20, 
                           static_cast<int>( rnd() * 128.f ) + 128, 
                           static_cast<int>( rnd() * 80.f ) + 30 ) ); 
            getXY( x, y );
        }
        bmp.saveBitmap( "./bf.bmp" );
    }
private:
    void getXY( float& x, float& y ) {
        float g, xl, yl;
        g = rnd();
        if( g < .01f ) { xl = 0; yl = .16f * y; } 
        else if( g < .07f ) {
            xl = .2f * x - .26f * y;
            yl = .23f * x + .22f * y + 1.6f;
        } else if( g < .14f ) {
            xl = -.15f * x + .28f * y;
            yl = .26f * x + .24f * y + .44f;
        } else {
            xl = .85f * x + .04f * y;
            yl = -.04f * x + .85f * y + 1.6f;
        }
        x = xl; y = yl;
    }
    float rnd() {
        return static_cast<float>( rand() ) / static_cast<float>( RAND_MAX );
    }
    myBitmap bmp;
};
int main( int argc, char* argv[]) {
    srand( static_cast<unsigned>( time( 0 ) ) );
    fern f; f.draw(); return 0;    
}


#include <iostream>
#include <random>
#include <vector>

#include <QImage>

bool barnsleyFern(const char* fileName, int width, int height) {
    constexpr int iterations = 1000000;
    int bytesPerLine = 4 * ((width + 3)/4);
    std::vector<uchar> imageData(bytesPerLine * height);

    std::random_device dev;
    std::mt19937 engine(dev());
    std::uniform_int_distribution<int> distribution(1, 100);

    double x = 0, y = 0;
    for (int i = 0; i < iterations; ++i) {
        int r = distribution(engine);
        double x1, y1;
        if (r == 1) {
            x1 = 0;
            y1 = 0.16 * y;
        } else if (r <= 86) {
            x1 = 0.85 * x + 0.04 * y;
            y1 = -0.04 * x + 0.85 * y + 1.6;
        } else if (r <= 93) {
            x1 = 0.2 * x - 0.26 * y;
            y1 = 0.23 * x + 0.22 * y + 1.6;
        } else {
            x1 = -0.15 * x + 0.28 * y;
            y1 = 0.26 * x + 0.24 * y + 0.44;
        }
        x = x1;
        y = y1;
        int row = height * (1 - y/11);
        int column = width * (0.5 + x/11);
        imageData[row * bytesPerLine + column] = 1;
    }

    QImage image(&imageData[0], width, height, bytesPerLine, QImage::Format_Indexed8);
    QVector<QRgb> colours(2);
    colours[0] = qRgb(255, 255, 255);
    colours[1] = qRgb(0, 160, 0);
    image.setColorTable(colours);
    return image.save(fileName);
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        std::cerr << "usage: " << argv[0] << " filename\n";
        return EXIT_FAILURE;
    }
    if (!barnsleyFern(argv[1], 600, 600)) {
        std::cerr << "image generation failed\n";
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}


  

You may also check:How to resolve the algorithm Dot product step by step in the AWK programming language
You may also check:How to resolve the algorithm Linear congruential generator step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Parsing/Shunting-yard algorithm step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm FASTA format step by step in the Racket programming language
You may also check:How to resolve the algorithm Old lady swallowed a fly step by step in the ALGOL 68 programming language