-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
139 lines (87 loc) · 3.17 KB
/
main.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "MinGL.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <cmath>
#include <array>
std::array<double, 2> f(double za, double zb, float a, float b)
{
// since (a + bi)² = a² - b² + 2abi, we know, that z² = za² - zb² + 2 * za * zb * i. This result can be stored in a real and imaginary part again: zz = zza + zzb * i with zza = za² - zb² and zzb = 2 * za * zb.
double zza = za * za - zb * zb;
double zzb = 2 * za * zb;
// now we have to add c, which in our case is represented by a as the real and b as the imaginary part.
double resulta = zza + a;
double resultb = zzb + b;
std::array<double, 2> result = {resulta, resultb};
return result;
}
bool divergesToInfinity(float a, float b)
{
// we are representing the complex number z with za and zb, which correspond to the real and imaginary part in z like this: z = za + zb * i.
// at the first iteration z is 0. Therefore the real and imaginary parts are both 0 as well.
double za = 0;
double zb = 0;
int maxIterations = 1000;
for(int iteration = 0; iteration < maxIterations; iteration++)
{
// to figure out if z is further than 2 away from 0, we square the real and imaginary part, add them and check if the result is bigger than 2². (a² + b² = c²)
if((za * za + zb * zb) >= 2 * 2){
// we have proved, that for this c f(z) will diverge to infinity.
return true;
}
std::array<double, 2> newResult = f(za, zb, a, b);
za = newResult[0];
zb = newResult[1];
}
return false;
}
float convertIteratorToXCoordinate(int number, int numberLimit){
float minValue = -2.0f;
float maxValue = 1.5f;
float maxIteratorValue = 1920.0f;
float interval = maxValue - minValue;
float relativeValue = number / maxIteratorValue;
float absoluteValue = minValue + relativeValue * interval;
return absoluteValue;
}
float convertIteratorToYCoordinate(int number, int numberLimit){
float minValue = -0.9f;
float maxValue = 0.9f;
float maxIteratorValue = 1050.0f;
float interval = maxValue - minValue;
float relativeValue = number / maxIteratorValue;
float absoluteValue = minValue + relativeValue * interval;
return absoluteValue;
}
int main()
{
// Window sizes
const int width = 1920;
const int height = 1050;
MinGL minGL;
if (!minGL.init(width, height, "MinGL"))
return -1;
const char* glsl_version = "#version 130";
const MinGLColor red{ 1.0f, 0.0f, 0.0f, 1.0f };
while (!minGL.windowShouldClose())
{
minGL.pollEvents();
minGL.processInput();
for(int a = 0; a < width; a++)
{
float aCoordinate = convertIteratorToXCoordinate(a, width);
for(int b = 0; b < height; b++)
{
float bCoordinate = convertIteratorToYCoordinate(b, height);
if(divergesToInfinity(aCoordinate, bCoordinate)){
minGL.putPixel(a, b, red);
}
}
std::cout << a << std::endl;
}
minGL.flush(0.0f, 0.0f, 0.0f, 1.00f);
}
// Clean up
minGL.shutdown();
return 0;
}