some more comments

This commit is contained in:
Nikola
2026-07-19 20:43:49 +02:00
parent 5a2f18845c
commit ade0294ec6
4 changed files with 17 additions and 12 deletions
+3
View File
@@ -133,6 +133,7 @@ void tricorn(float complex start, size_t width, ssize_t height, float res, unsig
} }
} }
// Iterative application of the equation given by the assignment
static unsigned converge(float c_real, float c_imag, unsigned n) { static unsigned converge(float c_real, float c_imag, unsigned n) {
float real = 0.0f; float real = 0.0f;
float imag = 0.0f; float imag = 0.0f;
@@ -141,10 +142,12 @@ static unsigned converge(float c_real, float c_imag, unsigned n) {
float real2 = real * real; float real2 = real * real;
float imag2 = imag * imag; float imag2 = imag * imag;
// If the pixel has diverged calculate the value and return it
if (real2 + imag2 > 4.0f) { if (real2 + imag2 > 4.0f) {
return 255 - 4 * (i % 64); return 255 - 4 * (i % 64);
} }
// Calculate imaginary and real parts separately
imag = -2.0f * real * imag + c_imag; imag = -2.0f * real * imag + c_imag;
real = real2 - imag2 + c_real; real = real2 - imag2 + c_real;
} }
+8 -11
View File
@@ -28,19 +28,15 @@ void tricorn_V1(float complex start, size_t width, ssize_t height, float res, un
float vertical_step = imag_direction * res; float vertical_step = imag_direction * res;
size_t position = 0; size_t position = 0; // Start value for the position in the image buffer. Will be accumulated later
float c_imag = c_start_imag; float c_imag = c_start_imag; // Start value for the imaginary part, which will be accumulated
// Simple iteration over every pixel in the picture
for(ssize_t y = 0; y < actual_height; y++){ for(ssize_t y = 0; y < actual_height; y++){
// Calculate imaginary part on each row as it stays the same
float c_real = c_start_real; float c_real = c_start_real;
for(size_t x = 0; x < width; x++){ for(size_t x = 0; x < width; x++){
unsigned value = converge(c_real,c_imag,n); unsigned value = converge(c_real,c_imag,n);
// Pixel value position in the output array
if(!color){ // Grayscale colors as specified by the assignment r = g = b if(!color){ // Grayscale colors as specified by the assignment r = g = b
img[position] = value; img[position] = value;
} }
@@ -50,11 +46,10 @@ void tricorn_V1(float complex start, size_t width, ssize_t height, float res, un
img[position + 2] = color_lookup_table[value][2]; img[position + 2] = color_lookup_table[value][2];
} }
position += bytesPerColor; position += bytesPerColor; // Caculate position for next pixel
c_real += res; c_real += res; // Calculate real part for next pixel
} }
c_imag += vertical_step; c_imag += vertical_step; // For reach row calculate the next value for the imaginary part by accumulation
} }
} }
@@ -67,10 +62,12 @@ static inline unsigned converge(float c_real, float c_imag, unsigned n) {
float real2 = real * real; float real2 = real * real;
float imag2 = imag * imag; float imag2 = imag * imag;
// If the pixel has diverged calculate the value and return it
if (real2 + imag2 > 4.0f) { if (real2 + imag2 > 4.0f) {
return 255 - 4 * (i % 64); return 255 - 4 * (i % 64);
} }
// Calculate imaginary and real parts separately
imag = -2.0f * real * imag + c_imag; imag = -2.0f * real * imag + c_imag;
real = real2 - imag2 + c_real; real = real2 - imag2 + c_real;
} }
+4 -1
View File
@@ -130,6 +130,7 @@ void tricorn_V2(float complex start, size_t width, ssize_t height, float res, un
} }
} }
// Iterative application of the equation given by the assignment
static unsigned converge(float c_real, float c_imag, unsigned n) { static unsigned converge(float c_real, float c_imag, unsigned n) {
float real = 0.0f; float real = 0.0f;
float imag = 0.0f; float imag = 0.0f;
@@ -138,10 +139,12 @@ static unsigned converge(float c_real, float c_imag, unsigned n) {
float real2 = real * real; float real2 = real * real;
float imag2 = imag * imag; float imag2 = imag * imag;
// If the pixel has diverged calculate the value and return it
if (real2 + imag2 > 4.0f) { if (real2 + imag2 > 4.0f) {
return 255 - 4 * (i % 64); return 255 - 4 * (i % 64);
} }
// Calculate imaginary and real parts separately
imag = -2.0f * real * imag + c_imag; imag = -2.0f * real * imag + c_imag;
real = real2 - imag2 + c_real; real = real2 - imag2 + c_real;
} }
+2
View File
@@ -212,10 +212,12 @@ static unsigned converge(float c_real, float c_imag, unsigned n) {
float real2 = real * real; float real2 = real * real;
float imag2 = imag * imag; float imag2 = imag * imag;
// If the pixel has diverged calculate the value and return it
if (real2 + imag2 > 4.0f) { if (real2 + imag2 > 4.0f) {
return 255 - 4 * (i % 64); return 255 - 4 * (i % 64);
} }
// Calculate imaginary and real parts separately
imag = -2.0f * real * imag + c_imag; imag = -2.0f * real * imag + c_imag;
real = real2 - imag2 + c_real; real = real2 - imag2 + c_real;
} }