summaryrefslogtreecommitdiff
path: root/src/genf-mandelbrot4.cc
blob: 92dd011939f18dd2b9eec645095f8cec8b1ac25f (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
/* Include libraries */
#include <SDL2/SDL.h>

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

void genf::mandelbrot4(SDL_Renderer* renderer, fractData* fd) {
	constexpr double max = 2.25;
	constexpr double min = -2.25;

	for(unsigned int x = 0; x < fd->width; ++x)
		for(unsigned int y = 0; y < fd->height; ++y) {
			long double r  = x * ((max + fd->real * fd->zoom) / fd->zoom - (min + fd->real * fd->zoom) / fd->zoom) / fd->width + (min + fd->real * fd->zoom) / fd->zoom;
			long double i  = y * ((max + fd->imag * fd->zoom) / fd->zoom - (min + fd->imag * fd->zoom) / fd->zoom) / fd->width + (min + 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) * (r2 * r2 + i2 * i2) * cos(4.0 * atan2(i2, r2)) + r;
				long double i2Temp = (r2 * r2 + i2 * i2) * (r2 * r2 + i2 * i2) * sin(4.0 * atan2(i2, r2)) + 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);
		}
}