-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmandelbrot.c
More file actions
102 lines (86 loc) · 2.98 KB
/
mandelbrot.c
File metadata and controls
102 lines (86 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#define WIDTH 800
#define HEIGHT 600
#define MAX_ITER 1000
// Function to compute whether a point belongs to the Mandelbrot set
int mandelbrot(double x0, double y0) {
double x = 0.0;
double y = 0.0;
int iter = 0;
while (x*x + y*y <= 4.0 && iter < MAX_ITER) {
double xtemp = x*x - y*y + x0;
y = 2*x*y + y0;
x = xtemp;
iter++;
}
return iter;
}
// Function to map pixel coordinates to the complex plane
double map(double value, double start1, double stop1, double start2, double stop2) {
return start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));
}
// Function to set pixel in SDL texture
void set_pixel(SDL_Renderer *renderer, int x, int y, int r, int g, int b) {
SDL_SetRenderDrawColor(renderer, r, g, b, 255);
SDL_RenderDrawPoint(renderer, x, y);
}
int main(int argc, char* argv[]) {
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
return 1;
}
// Create SDL window and renderer
SDL_Window *window = SDL_CreateWindow("Mandelbrot Fractal",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
WIDTH, HEIGHT,
SDL_WINDOW_SHOWN);
if (!window) {
fprintf(stderr, "Could not create window: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
fprintf(stderr, "Could not create renderer: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
// Variables for the complex plane
double minRe = -2.0, maxRe = 1.0;
double minIm = -1.2, maxIm = minIm + (maxRe - minRe) * HEIGHT / WIDTH;
// Generate the Mandelbrot fractal
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
// Map the pixel coordinates to the complex plane
double a = map(x, 0, WIDTH, minRe, maxRe);
double b = map(y, 0, HEIGHT, minIm, maxIm);
// Compute the Mandelbrot value
int iter = mandelbrot(a, b);
// Choose color based on iteration count
int color = map(iter, 0, MAX_ITER, 0, 255);
set_pixel(renderer, x, y, color % 8 * 32, color % 16 * 16, color % 32 * 8);
}
}
// Show the rendered fractal
SDL_RenderPresent(renderer);
// Wait for user to close the window
int quit = 0;
SDL_Event event;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = 1;
}
}
}
// Clean up
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}