Updated: Basic tricorn implementation fixed and comments added

This commit is contained in:
Yashar Alan Zenker
2026-06-20 19:40:35 +02:00
parent 00d59d543a
commit 3adaf79c18
2 changed files with 23 additions and 10 deletions
+1 -1
View File
@@ -17,7 +17,7 @@ unsigned tests(void) {
void benchmarking(void) { void benchmarking(void) {
// do the benchmarking // do the benchmarking
} }
// REMOVE LATER: interesting test command ./tricorn -s -2.0,0.6 -r 0.0010 -n 200 -d 1920 1080 -o image.bmp -C
const char* help_messsage = "Help Message\n\n" const char* help_messsage = "Help Message\n\n"
"Usage:\n" "Usage:\n"
" ./tricorn [OPTIONS]\n\n" " ./tricorn [OPTIONS]\n\n"
+22 -9
View File
@@ -1,37 +1,50 @@
#include "tricorn.h" #include "tricorn.h"
static const float convergenceLimit = 2.0f; static const float convergenceLimit = 2.0f; // Mandelbrot set limit
unsigned converge(float complex position,unsigned n); unsigned converge(float complex position,unsigned n);
void tricorn(float complex start, size_t width, ssize_t height, float res, unsigned n, _Bool color, unsigned char* img){ void tricorn(float complex start, size_t width, ssize_t height, float res, unsigned n, _Bool color, unsigned char* img){
// Absolute height for iteration purposes
height = height < 0 ? height*-1 : height; height = height < 0 ? height*-1 : height;
// Simple iteration over every pixel in the picture
for(size_t i = 0; i < width; i++){ for(size_t i = 0; i < width; i++){
for(ssize_t j = 0; j < height; j++){ for(ssize_t j = 0; j < height; j++){
unsigned value = converge(start + (i*res + I*j*res),n); unsigned value = converge(start + (i*res - I*j*res),n);
size_t position = (i*width +j) *3;
if(!color){ // Pixel value position in the output array
size_t position = (j*width +i) *3;
if(!color){ // Grayscale colors as specified by the assignment r = g = b
img[position] = value; img[position] = value;
img[position + 1] = value; img[position + 1] = value;
img[position + 2] = value; img[position + 2] = value;
} }
else{ // TEMPORARY / Bad looking color gradient before testing else{ // Temporary colors with random multipliers
img[position] = (value % 85)*3; img[position] = ((value*3) % 255);
img[position + 1] = 85 +(value % 85) * 2; img[position + 1] = ((value*2) % 255) ;
img[position + 2] = 170 +(value % 85); img[position + 2] = ((value*5) % 255);
} }
} }
} }
} }
// Iterative application of the equation given by the assignment
unsigned converge(float complex position,unsigned n){ unsigned converge(float complex position,unsigned n){
complex float result = 0; complex float result = 0;
for(unsigned i = 0; i < n; i++){ for(unsigned i = 0; i < n; i++){
// Application of the given complex equation with m = 2
result = cpowf(conjf(result),2) + position; result = cpowf(conjf(result),2) + position;
if(sqrtf(crealf(result)+cimagf(result)) > convergenceLimit)
// Detection of unbounded behaviour
// If the magnitude of the result vector is greater than the convergence limit, we know that the function will grow unbounded
// Current convergence limit taken from mandelbrot set
if(sqrtf(powf (crealf(result),2)+powf(cimagf(result),2)) > convergenceLimit)
return 255-4*(i % 64); return 255-4*(i % 64);
} }
return 0; return 0;