News:

Welcome to RetroCoders Community

Main Menu

C Mandelbrot With Raylib Library

Started by ron77, May 25, 2023, 04:54 PM

Previous topic - Next topic

ron77

#include <stdio.h>
#include <stdlib.h>
#include "raylib.h"

#define WIDTH 800
#define HEIGHT 600
#define MAX_ITER 100

int mandelbrot(float x, float y)
{
    float real = 0, imag = 0;
    int iter = 0;

    while (iter < MAX_ITER && (real * real + imag * imag) < 4)
    {
        float temp = real * real - imag * imag + x;
        imag = 2 * real * imag + y;
        real = temp;
        iter++;
    }

    return iter;
}

Color getColor(int iter)
{
    if (iter == MAX_ITER)
        return BLACK;

    float t = (float)iter / MAX_ITER;
    return (Color){(int)(255 * t), (int)(255 * t), (int)(255 * t), 255};
}

int main(void)
{
    InitWindow(WIDTH, HEIGHT, "Mandelbrot Set");

    RenderTexture2D target = LoadRenderTexture(WIDTH, HEIGHT);
    SetTargetFPS(60);

    while (!WindowShouldClose())
    {
        if (IsKeyPressed(KEY_ENTER))
        {
            BeginTextureMode(target);

            for (int x = 0; x < WIDTH; x++)
            {
                for (int y = 0; y < HEIGHT; y++)
                {
                    float xPos = (float)x / WIDTH * 3.5 - 2.5;
                    float yPos = (float)y / HEIGHT * 2 - 1;

                    int iter = mandelbrot(xPos, yPos);
                    Color color = getColor(iter);
                    DrawPixel(x, y, color);
                }
            }

            EndTextureMode();
        }

        BeginDrawing();
        ClearBackground(BLACK);
        DrawTextureRec(target.texture, (Rectangle){0, 0, WIDTH, -HEIGHT}, (Vector2){0, 0}, WHITE);
        DrawText("Press ENTER to render the Mandelbrot set", 10, 10, 20, RAYWHITE);
        EndDrawing();
    }

    UnloadRenderTexture(target);
    CloseWindow();

    return 0;
}

You cannot view this attachment.

the zip project with source + raylib + executable (win 32 bit)

You cannot view this attachment. 

ron77

okay now here is an infinite zooming mandelbrot set fractal with raylib library

#include "raylib.h"

#define MAX_ITERATIONS 100

int main()
{
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "Infinite Zooming Mandelbrot Set");
    SetTargetFPS(60);

    // Mandelbrot parameters
    double zoom = 1.0;
    double offsetX = 0.0;
    double offsetY = 0.0;

    while (!WindowShouldClose())
    {
        // Update
        if (IsKeyDown(KEY_UP)) zoom *= 1.02;      // Zoom in
        if (IsKeyDown(KEY_DOWN)) zoom /= 1.02;    // Zoom out

        if (IsKeyDown(KEY_LEFT)) offsetX -= 0.02; // Move left
        if (IsKeyDown(KEY_RIGHT)) offsetX += 0.02;// Move right

        if (IsKeyDown(KEY_W)) offsetY -= 0.02;    // Move up
        if (IsKeyDown(KEY_S)) offsetY += 0.02;    // Move down

        // Draw
        BeginDrawing();
        ClearBackground(BLACK);

        for (int x = 0; x < screenWidth; x++)
        {
            for (int y = 0; y < screenHeight; y++)
            {
                // Convert screen coordinates to Mandelbrot coordinates
                double zx = (x - screenWidth / 2.0) / (0.5 * zoom * screenWidth) + offsetX;
                double zy = (y - screenHeight / 2.0) / (0.5 * zoom * screenHeight) + offsetY;

                double cx = zx;
                double cy = zy;

                int iteration = 0;
                double xtemp;

                while ((zx * zx + zy * zy) < 4 && iteration < MAX_ITERATIONS)
                {
                    xtemp = zx * zx - zy * zy + cx;
                    zy = 2.0 * zx * zy + cy;
                    zx = xtemp;
                    iteration++;
                }

                // Color the pixel based on the number of iterations
                Color pixelColor = (iteration == MAX_ITERATIONS) ? BLACK : (Color){ iteration * 8 % 256, iteration * 4 % 256, iteration * 2 % 256, 255 };
                DrawPixel(x, y, pixelColor);
            }
        }

        EndDrawing();
    }

    CloseWindow();

    return 0;
}

sshot:

You cannot view this attachment.

zip folder with source code + executable (32 bit mingw32) + raylib library (the project):

You cannot view this attachment.