News:

Welcome to RetroCoders Community

Main Menu

C terminal Mandelbrot

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

Previous topic - Next topic

ron77

#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;
}

You cannot view this attachment.

ZXDunny

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 :)