1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| #include "mybmp.h" #include <math.h>
struct my_complex { double real; double imag; }; typedef struct my_complex my_complex; int chkIteration(struct my_complex c, int n) { struct my_complex z1, z0 = {0, 0}; double d; int i; for (i = 0; i < n; i++) { z1.real = z0.real * z0.real - z0.imag * z0.imag + c.real; z1.imag = 2 * z0.real * z0.imag + c.imag; d = z1.real * z1.real + z1.imag * z1.imag; if (d > 3 * 3) return i + 1; z0 = z1; } return -1; }
void map(int it, int *r, int *g, int *b) { int min = 0, max = 256; double tmp, step, m, localr; int range = max - min + 1, idx; if (it < min) { *r = 0, *g = 0, *b = 0; return; } if (it > max) { *r = 255, *g = 255, *b = 255; return; } tmp = (double)(it - min) / range; if (tmp > 0.2) idx = 4, localr = (it - min - range * 0.2) / (range * 0.8); else if (tmp > 0.1) idx = 3, localr = (range * 0.1 - (it - min - range * 0.1)) / (range * 0.1); else if (tmp > 0.05) idx = 2, localr = (it - min - range * 0.05) / (range * 0.05); else if (tmp > 0.02) idx = 1, localr = (range * 0.03 - (it - min - range * 0.02)) / (range * 0.03); else idx = 0, localr = (it - min) / (range * 0.02); switch (idx) { case 0: *r = 0, *g = localr * 255, *b = 255; break; case 1: *r = 0, *g = 255, *b = localr * 255; break; case 2: *r = localr * 255, *g = 255, *b = 0; break; case 3: *r = 255, *g = localr * 255, *b = 0; break; case 4: *r = 0, *g = 255, *b = localr * 255; break; default: break; } } int t;
void drawPicture(char *fn, int w, int h, double x1, double y1, double y2) { int cnt = 0; int i, j; double x, y, rxy = (y2 - y1) / h, x2 = x1 + w * rxy; bmpInitial(w, h); for (i = 0; i < h; i++) { y = y1 + i * (y2 - y1) / h; for (j = 0; j < w; j++) { x = x1 + j * (x2 - x1) / w; my_complex cpx = {x, y}; int r, g, b; int it = chkIteration(cpx, 256); map(it, &r, &g, &b); bmpSetPixel(i, j, r, g, b); } } bmpWriteToFile(fn); if (1 <= t && t <= 4) printf("fn=Mand06_%d.BMP, w=%d, h=%d\nx1=%lg, x2=%lg, y1=%lg, y2=%lg, rxy=%lg\n", t, w, h, x1, x2, y1, y2, rxy); else printf("fn=Mand06.BMP, w=%d, h=%d\nx1=%lg, x2=%lg, y1=%lg, y2=%lg, rxy=%lg\n", w, h, x1, x2, y1, y2, rxy); }
int randci(char s[], int i) { int ret; while (i == -1 || s[i] != ' ') scanf("%c", &s[++i]); ret = atoi(s); for (i = 0; i < 100; ++i) s[i] = '\0'; return ret; }
double randcf(char s[], int i) { double ret; while (i == -1 || s[i] != ' ' && s[i] != '\n') scanf("%c", &s[++i]); ret = atof(s); for (i = 0; i < 100; ++i) s[i] = '\0'; return ret; }
void read(int *w, int *h, double *x1, double *y1, double *y2) { char s[100]; double nums[4][3] = { {-2, -2, 1}, {-2.25, -1.5, 0.5}, {0, 0, 1}, {0, 0, 0.5}}; *w = 160, *h = 120; *x1 = -2.25, *y1 = -1.25, *y2 = 1.25;
scanf("%c", s); if (s[0] == 'z') return; *w = randci(s, 0); *h = randci(s, -1);
scanf("%c", s); if (s[0] == 'z') return; t = randci(s, 0); if (t >= 1 && t <= 4) { *x1 = nums[t - 1][0]; *y1 = nums[t - 1][1]; *y2 = nums[t - 1][2]; return; } *x1 = randcf(s, -1); *y1 = randcf(s, -1); *y2 = randcf(s, -1); }
int main(void) { int w, h; double x1, y1, y2; char pic[15] = "Mand06.BMP"; read(&w, &h, &x1, &y1, &y2); if (1 <= t && t <= 4) { char pict[] = "Mand06_#.BMP"; pict[7] = '0' + t; drawPicture(pict, w, h, x1, y1, y2); } else drawPicture(pic, w, h, x1, y1, y2); return 0; }
|