#include #include "image.h" typedef double (* function_t) (double x); #define SCALE 32.0 typedef struct { double r, g, b; } color_t; static void plot (complex_image_t *image, color_t color, function_t f) { int i; double max, min; int first = 1; double a, b; for (i = 0; i < image->width; ++i) { double v = f(i / SCALE); if (first || v < min) min = v; if (first || v > max) max = v; first = 0; } if (max - min < DBL_EPSILON) return; max = 2; min = -2; printf ("min %f max %f\n", min, max); a = (image->height - 1) / (max - min); b = - (min * (image->height - 1)) / (max - min); for (i = 0; i < image->width; ++i) { double v = f (i / SCALE); int idx; complex_t p; v = a * v + b; printf ("scaled: %f\n", v); idx = v + 0.5; if (idx > image->height - 1) idx = image->height - 1; if (idx < 0) idx = 0; idx = idx * image->width + i; p.re = 0; p.im = 0; image->red[idx] = image->green[idx] = image->blue[idx] = p; image->red[idx].re = color.r; image->green[idx].re = color.g; image->blue[idx].re = color.b; } } static const color_t red = { 1.0, 0.0, 0.0 }; static const color_t blue = { 0.4, 0.4, 1.0 }; static const color_t green = { 0.0, 1.0, 0.0 }; static const color_t yellow = { 1.0, 1.0, 0.0 }; #define HIGH (2) #define ALIAS (1) static double orig (double x) { double v = (0.4 * sin (HIGH * x) + sin (0.3 * x)); return v; } static double antialiased (double x) { double v = sin (0.3 * x); return v; } static double imperfect (double x) { return sin (0.3 * x) + 0.1 * sin (ALIAS * x); } static double aliased (double x) { return sin (0.3 * x) + 0.4 * sin (ALIAS * x); } int main () { complex_image_t *image = complex_image_new (1024, 256); plot (image, red, orig); plot (image, blue, antialiased); plot (image, green, aliased); plot (image, yellow, imperfect); complex_image_show ("func", image, CONVERT_RE); }