#include #include #include typedef struct { uint8_t r, g, b; } rgb_t; #define ZOOM 2 #define SMOOTH 1 #define graphwidth ((uint16_t) (128 * ZOOM * 3 / 2.5)) #define graphheight ((uint16_t) (128 * ZOOM)) static void putpixel (uint16_t x, uint16_t y, uint8_t r, uint8_t g, uint8_t b); static inline uint32_t rgbfromvalue (uint8_t r, uint8_t g, uint8_t b) { uint32_t rgb = 0; rgb |= r; rgb |= (uint16_t) (g << 8); rgb |= (uint32_t) b << 16; return rgb; } void mandelbrot (uint16_t ofsx, uint16_t ofsy) { const uint16_t max_it = 20; const float m = 20.0; float xmin= -2.2, xmax = 0.8, ymin = -1.25, ymax = 1.25; // alternative Zahlenwerte // xmin= -0.5328; xmax= -0.2078; ymin= 0.3742; ymax= 0.892; const float dx = (float) (xmax - xmin) / graphwidth; const float dy = (float) (ymax - ymin) / graphheight; const float dd = sqrtf (dx * dy); float jy = ymin; for (uint16_t y = 0; y < graphheight; ++y, jy += dd) { float jx = xmin; for (uint16_t x = 0; x < graphwidth; ++x, jx += dd) { float r, wx = 0.0, wy = 0.0; uint16_t it = 0; do { float tx = wx*wx - wy*wy + jx; float ty = ldexpf (wx*wy, 1) + jy; r = wx*wx + wy*wy; wx = tx; wy = ty; it++; } while (r <= m*m && it < max_it); rgb_t rgb; /* if (k < 3) { rgb.r = rgb.g = rgb.b = k * 20; } if (k > 2 && k < 35) { rgb.b = k * 9; rgb.g = k; rgb.r = k * 2; } if (k > 34) { rgb.r = 128; rgb.g = k * 3; rgb.b = k; } */ if (it >= max_it) rgb.r = rgb.g = rgb.b = 0; else { uint8_t c = it & 1 ? 250 : 0; if (SMOOTH) { c = 252 * (logf (r) / (2 * logf (m)) - 0.99); c = it & 1 ? c : 254 - c; } rgb.r = rgb.g = rgb.b = c; } putpixel (ofsx + x, ofsy + y, rgb.r, rgb.g, rgb.b); } } } void putpixel (uint16_t x, uint16_t y, uint8_t r, uint8_t g, uint8_t b) { (void) y; #ifndef AVRTEST_H printf ("%u %u %u%c", r, g, b, x == graphwidth - 1 ? '\n' : ' '); #else LOG_FMT_U16 ("%u ", r); LOG_FMT_U16 ("%u ", g); LOG_FMT_U16 ("%u", b); LOG_STR (x == graphwidth - 1 ? "\n" : " "); #endif } int main (void) { printf ("P3\n"); printf ("%u %u\n", graphwidth, graphheight); printf ("255\n"); mandelbrot (0, 0); return 0; }