Created
July 27, 2013 09:07
-
-
Save YukiSakamoto/6094322 to your computer and use it in GitHub Desktop.
output gif image of Madelbrot Set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
const int times = 10; | |
const double x_max = 0.6; | |
const double x_min =-1.6 ; | |
const double y_max = 1.00; | |
const double y_min = -0.35; | |
const double dx = 0.002; | |
const double dy = 0.002; | |
/* If mandelbrot() function returns non-zero, that inidicates divergence. */ | |
int mandelbrot(double c_x, double c_y, int n) | |
{ | |
double x_n = 0; | |
double y_n = 0; | |
double x_n_1; | |
double y_n_1; | |
int i; | |
for(i = 0; i < n; i++) { | |
x_n_1 = pow(x_n, 2) - pow(y_n, 2) + c_x; | |
y_n_1 = 2 * x_n * y_n + c_y; | |
if ( pow(x_n_1, 2) + pow(y_n_1, 2) > 4.0 ){ | |
return i; | |
} else { | |
x_n = x_n_1; | |
y_n = y_n_1; | |
} | |
} | |
return -1; | |
} | |
/* FOR CONSOLE */ | |
/* | |
int main(void) | |
{ | |
double x; | |
double y; | |
int x_length = (x_max - x_min) / dx; | |
for(y = y_max; y > y_min; y -= dy) { | |
for(x = x_min; x < x_max; x += dx) { | |
putchar(mandelbrot(x, y, 300) == 0 ? '*' : ' '); | |
} | |
putchar('\n'); | |
} | |
return 0; | |
} | |
*/ | |
#include "gd.h" | |
double x_imagepos2gaussianpos(int pos, int pixel_number, double min, double max) | |
{ | |
return min + (max-min) * pos / pixel_number; | |
} | |
double y_imagepos2gaussianpos(int pos, int pixel_number, double min, double max) | |
{ | |
return max - (max-min) * pos / pixel_number; | |
} | |
int x_pos2pixelnum(double pos, double min, double max, int pixel_number) | |
{ | |
int n = -1; | |
if (min < pos && pos < max) { | |
n = (int)( (pos - min) / (max - min) * pixel_number); | |
} | |
return n; | |
} | |
int y_pos2pixelnum(double pos, double min, double max, int pixel_number) | |
{ | |
int n = x_pos2pixelnum(pos, min, max, pixel_number); | |
if (n != -1) { | |
return pixel_number - n; | |
} else { | |
return n; | |
} | |
} | |
int main(void) | |
{ | |
FILE *out; | |
int x, y; | |
int x_width = 1600; | |
int y_width = 900; | |
gdImagePtr im = gdImageCreate(x_width, y_width); | |
int black = gdImageColorAllocate(im, 0, 0, 0); | |
int axis_color = gdImageColorAllocate(im, 255, 255, 255); | |
int aqua = gdImageColorAllocate(im, 175, 223, 228); | |
int blue = gdImageColorAllocate(im, 0x00, 0x00, 0xff); | |
int turquoise = gdImageColorAllocate(im, 0x40, 0xe0, 0xd0); | |
int DodgerBlue = gdImageColorAllocate(im, 0x1e, 0x90, 0xff); | |
int DeepSkyBlue= gdImageColorAllocate(im, 0x00, 0xbf, 0xff); | |
int LightSkyBlue= gdImageColorAllocate(im, 0x87, 0xce, 0xfa); | |
int SkyBlue = gdImageColorAllocate(im, 0x87, 0xce, 0xeb); | |
int x0_pixel; | |
int y0_pixel; | |
for(x = 0; x < x_width; x++) { | |
double cur_x = x_imagepos2gaussianpos(x, x_width, x_min, x_max); | |
for(y = 0; y < y_width; y++) { | |
double cur_y = y_imagepos2gaussianpos(y, y_width, y_min, y_max); | |
int n; | |
printf("%d, %d, -> %f, %f\n", x, y, cur_x, cur_y); | |
n = mandelbrot(cur_x, cur_y, 300); | |
if (n == -1) { | |
/* Mandelbrot Set */ | |
gdImageSetPixel(im, x, y, blue); | |
} else if (n < 8) { | |
/* fastest divergence */ | |
gdImageSetPixel(im, x, y, black); | |
} else if (n < 10) { | |
gdImageSetPixel(im, x, y, SkyBlue); | |
} else if (n < 20) { | |
gdImageSetPixel(im, x, y, LightSkyBlue); | |
} else if (n < 30) { | |
gdImageSetPixel(im, x, y, DeepSkyBlue); | |
} else { | |
/* slowest divergence */ | |
gdImageSetPixel(im, x, y, DodgerBlue); | |
} | |
} | |
} | |
/* draw axis */ | |
x0_pixel = x_pos2pixelnum(0, x_min, x_max, x_width); | |
y0_pixel = y_pos2pixelnum(0, y_min, y_max, y_width); | |
/* y_axis */ | |
if (x0_pixel != -1) { | |
int scale_len = 20; | |
int half_scale_len = scale_len / 2; | |
int y_05_pixel = y_pos2pixelnum(0.5, y_min, y_max, y_width); | |
int y_minus05_pixel = y_pos2pixelnum(-0.5, y_min, y_max, y_width); | |
gdImageLine(im, x0_pixel, 0, x0_pixel, y_width - 1, axis_color); | |
/* insert y-scale */ | |
/* y = 0.5 */ | |
gdImageLine(im, x0_pixel - half_scale_len, y_05_pixel, x0_pixel + half_scale_len, y_05_pixel, axis_color); | |
/* y = -0.5 */ | |
/* | |
gdImageLine(im, x0_pixel - half_scale_len, y_minus05_pixel, x0_pixel + half_scale_len, y_minus05_pixel, axis_color); | |
*/ | |
} | |
/* x_axis */ | |
if (y0_pixel != -1) { | |
int scale_len = 20; | |
int half_scale_len = scale_len / 2; | |
int x_minus1_pixel = x_pos2pixelnum(-1, x_min, x_max, x_width); | |
int x_minus05_pixel = x_pos2pixelnum(-0.5, x_min, x_max, x_width); | |
int x_minus15_pixel = x_pos2pixelnum(-1.5, x_min, x_max, x_width); | |
int x_05_pixel = x_pos2pixelnum(0.5, x_min, x_max, x_width); | |
gdImageLine(im, 0, y0_pixel, x_width - 1, y0_pixel, axis_color); | |
/* insert x-scale */ | |
/* x = -1 */ | |
gdImageLine(im, x_minus1_pixel, y0_pixel - half_scale_len, x_minus1_pixel, y0_pixel + half_scale_len, axis_color); | |
/* x = -0.5 */ | |
gdImageLine(im, x_minus05_pixel, y0_pixel - half_scale_len, x_minus05_pixel, y0_pixel + half_scale_len, axis_color); | |
/* x = -1.5 */ | |
gdImageLine(im, x_minus15_pixel, y0_pixel - half_scale_len, x_minus15_pixel, y0_pixel + half_scale_len, axis_color); | |
/* x = 0.5 */ | |
gdImageLine(im, x_05_pixel, y0_pixel - half_scale_len, x_05_pixel, y0_pixel + half_scale_len, axis_color); | |
} | |
out = fopen("new_mandelbrot.gif", "w"); | |
gdImageGif(im, out); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment