summaryrefslogtreecommitdiff
path: root/src/genf-mandelbrot.cc
blob: c94ff5fdfa4fe903114c28d9b4a0e12779601a93 (plain) (blame)
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
/* Include libraries */
#include <SDL2/SDL.h>

/* Include header files */
#include "fractData.hh"
#include "genf.hh"
#include "mansdl.hh"

void genf::mandelbrot(SDL_Renderer* renderer, fractData* fd) {
	const double maxR = 2.25;
	const double minR = -2.25;
	const double maxI = 2.25;
	const double minI = -2.25;

	for(unsigned int x = 0; x < fd->width; ++x)
		for(unsigned int y = 0; y < fd->height; ++y) {
			long double r  = x * ((maxR + fd->real * fd->zoom) / fd->zoom - (minR + fd->real * fd->zoom) / fd->zoom) / fd->width + (minR + fd->real * fd->zoom) / fd->zoom;
			long double i  = y * ((maxI + fd->imag * fd->zoom) / fd->zoom - (minI + fd->imag * fd->zoom) / fd->zoom) / fd->width + (minI + fd->imag * fd->zoom) / fd->zoom;
			long double r2 = 0.0;
			long double i2 = 0.0;

			unsigned int iter;
			for(iter = 0; iter < fd->maxIter; ++iter) {
				if(r2 * r2 + i2 * i2 >= 4.0)
					break;

				long double r2Temp = r2 * r2 - i2 * i2 + r;
				long double i2Temp = 2.0 * r2 * i2 + i;

				if(r2 == r2Temp && i2 == i2Temp) {
					iter = fd->maxIter;
					break;
				}

				r2 = r2Temp;
				i2 = i2Temp;
			}

			unsigned short int red   = 0;
			unsigned short int green = 0;
			unsigned short int blue  = 0;

			if(iter != fd->maxIter) {
				double t = (double)(iter) / (double)(fd->maxIter);
				red      = (int)(9.0 * (1.0 - t) * t * t * t * 255.0);
				green    = (int)(15.0 * (1.0 - t) * (1.0 - t) * t * t * 255.0);
				blue     = (int)(8.5 * (1.0 - t) * (1.0 - t) * (1.0 - t) * t * 255.0);
			}

			SDL_SetRenderDrawColor(renderer, red, green, blue, SDL_ALPHA_OPAQUE);
			SDL_RenderDrawPoint(renderer, x, y);
		}
}