RetroCoders Community

C / C ++ Programming => C / C++ => Topic started by: ron77 on May 25, 2023, 04:14 PM

Title: C terminal Mandelbrot
Post by: ron77 on May 25, 2023, 04:14 PM
#include <stdio.h>

#define WIDTH 80
#define HEIGHT 60
#define MAX_ITERATIONS 100

int mandelbrot(double real, double imag) {
    double zReal = real;
    double zImag = imag;
    int n;
   
    for (n = 0; n < MAX_ITERATIONS; ++n) {
        double zRealSq = zReal * zReal;
        double zImagSq = zImag * zImag;
       
        if (zRealSq + zImagSq > 4.0)
            break;
       
        double nextReal = zRealSq - zImagSq + real;
        double nextImag = 2.0 * zReal * zImag + imag;
       
        zReal = nextReal;
        zImag = nextImag;
    }
   
    return n;
}

int main() {
    int x, y;
   
    for (y = 0; y < HEIGHT; ++y) {
        for (x = 0; x < WIDTH; ++x) {
            double real = (x - WIDTH / 2.0) * 4.0 / WIDTH;
            double imag = (y - HEIGHT / 2.0) * 4.0 / HEIGHT;
           
            int iterations = mandelbrot(real, imag);
           
            if (iterations == MAX_ITERATIONS)
                printf("*");
            else
                printf(" ");
        }
       
        printf("\n");
    }
   
    return 0;
}

mandel_c.png
Title: Re: C terminal Mandelbrot
Post by: ZXDunny on May 25, 2023, 09:33 PM
Oh! I remember we did a small challenge for an ASCII brot a while back on one of the BASIC forums. It was great fun :)