From 7f5e734f41ff771a1336de4772b7b9ba6e7a8459 Mon Sep 17 00:00:00 2001 From: Alejandro Laguna Date: Mon, 13 Jul 2026 14:14:26 +0200 Subject: [PATCH] refactor: separate functions into files --- Makefile | 2 +- include/mandelbrot.h | 12 ++++++++++++ src/color.c | 9 +++++++++ src/main.c | 45 +------------------------------------------- src/math.c | 33 ++++++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 45 deletions(-) create mode 100644 include/mandelbrot.h create mode 100644 src/color.c create mode 100644 src/math.c diff --git a/Makefile b/Makefile index 71b16f06d421f9ac9878eb974be8a8ef1b30ed70..87e43f09ffb4e8a21d115a9dc7b160737c3e761e 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC = gcc CFLAGS = -Wall -Wextra -Werror -g3 -I include/ -SRC = src/main.c +SRC = src/main.c src/color.c src/math.c OBJ = $(SRC:.c=.o) OUT = mandelbrot diff --git a/include/mandelbrot.h b/include/mandelbrot.h new file mode 100644 index 0000000000000000000000000000000000000000..eafd73177c3680e4a2a145b84d215d9446fcd849 --- /dev/null +++ b/include/mandelbrot.h @@ -0,0 +1,12 @@ +#ifndef MANDELBROT_H +#define MANDELBROT_H + +// math.c +int mandelbrot_iterations(double cr, double ci, int max_iter); + +// color.c +// TODO: color struct or something +void get_color(int iter, int max_iter, int* r, int* g, int* b); + + +#endif diff --git a/src/color.c b/src/color.c new file mode 100644 index 0000000000000000000000000000000000000000..7f5a776f9ca9af57990747ec36c470b6f7590429 --- /dev/null +++ b/src/color.c @@ -0,0 +1,9 @@ +void get_color(int iter, int max_iter, int* r, int* g, int* b) { + if (iter == max_iter) { + *r = *g = *b = 0; // black inside the set + return; + } + double t = (double)iter / max_iter; + int gray = (int)((1.0 - t) * 255); // whiter the furthest it was + *r = *g = *b = gray; +} diff --git a/src/main.c b/src/main.c index 5ef378263e3f13f6273f0b73c361303092327833..63bf5666e01d1d4ae62fdd37a895726a371bb612 100644 --- a/src/main.c +++ b/src/main.c @@ -1,49 +1,6 @@ +#include "mandelbrot.h" #include -// the mandelbrot set, basically, is a set of points on the complex plane - -// those points are found using the following equation: -// -// z = z^2 + c -// -// c is an initial point on this complex plane. if the value of z is bigger than -// 2 (which means that it's distance from the origin is larger than 2), it means -// the value is out of the set. -// -// but what's going on with all the shapes and colors? this is a recursive -// function. -// -// we start with z as 0, and then with the equation we get a new z, and then put -// that new z onto the equation and we do that over and over. if z doesn't grow -// over 2, then c is part of the set. if it does pass 2, we know for how many -// iterations we have checked and we color based on that - -int mandelbrot_iterations(double cr, double ci, int max_iter) { - double zr = 0.0, zi = 0.0; - int iter = 0; - while (iter < max_iter) { - double zr2 = zr * zr; - double zi2 = zi * zi; - // avoids checking for sqrt - if (zr2 + zi2 > 4.0) { - break; - } - zi = 2.0 * zr * zi + ci; - zr = zr2 - zi2 + cr; - iter++; - } - return iter; -} - -void get_color(int iter, int max_iter, int* r, int* g, int* b) { - if (iter == max_iter) { - *r = *g = *b = 0; // black inside the set - return; - } - double t = (double)iter / max_iter; - int gray = (int)((1.0 - t) * 255); // whiter the furthest it was - *r = *g = *b = gray; -} - int main() { int width = 800; int height = 600; diff --git a/src/math.c b/src/math.c new file mode 100644 index 0000000000000000000000000000000000000000..0c93b419b1236c533b3e67bd6b9085c5fc41da7b --- /dev/null +++ b/src/math.c @@ -0,0 +1,33 @@ +// the mandelbrot set, basically, is a set of points on the complex plane - +// those points are found using the following equation: +// +// z = z^2 + c +// +// c is an initial point on this complex plane. if the value of z is bigger than +// 2 (which means that it's distance from the origin is larger than 2), it means +// the value is out of the set. +// +// but what's going on with all the shapes and colors? this is a recursive +// function. +// +// we start with z as 0, and then with the equation we get a new z, and then put +// that new z onto the equation and we do that over and over. if z doesn't grow +// over 2, then c is part of the set. if it does pass 2, we know for how many +// iterations we have checked and we color based on that + +int mandelbrot_iterations(double cr, double ci, int max_iter) { + double zr = 0.0, zi = 0.0; + int iter = 0; + while (iter < max_iter) { + double zr2 = zr * zr; + double zi2 = zi * zi; + // avoids checking for sqrt + if (zr2 + zi2 > 4.0) { + break; + } + zi = 2.0 * zr * zi + ci; + zr = zr2 - zi2 + cr; + iter++; + } + return iter; +}