1 #include <stdio.h>
2 #include <math.h>
3 #include <stdlib.h>
4 #include <stdbool.h>
5 #define max(x, y) ((x)>(y)?(x):(y))
6
7 #define XDIM 1024 //此项必须为8的倍数,否则有BUG
8 #define YDIM 768
9
10 //为图形协调,请保持与XDIM、YDIM的比例
11 #define X_RANGE 2*4
12 #define Y_RANGE 2*3
13
14 #define INACCURACY 1.0 //单位:像素
15
16 #define XI XDIM/2
17 #define YI YDIM/2
18 bool tof(int i, int j)
19 {
20 //坐标转换
21 double x = (double)(i-XI)*X_RANGE/XDIM;
22 double y = Y_RANGE-(double)(j+YI)*Y_RANGE/YDIM;
23 double d = INACCURACY*max(X_RANGE/(double)XDIM, Y_RANGE/(double)YDIM);
24
25 if
26 (
27 i == XI || j == YI-1 ||
28 j == YI || i == XI-1 ||
29 fabs(x-(int)x)<d ||
30 fabs(y-(int)y)<d ||
31 //添加函数
32 fabs(y-x*x)<d ||
33 fabs(y-sin(x))<d ||
34 fabs(x+y*y*y-3*2*x*y+3)<2*d ||
35 fabs(y-exp(x))<d ||
36 fabs(y*y+x*x-2)<d ||
37 fabs(y-exp(-x*x))<d
38 )
39 return true;
40 return false;
41 }
42
43 void writeBit(bool bit, FILE * fp)
44 { //白色为true
45 static int bitNum = 0;
46 static unsigned char Byte = 0;
47 if(bitNum==8)
48 {
49 fputc(Byte, fp);
50 Byte = 0;
51 bitNum = 0;
52 }
53 Byte ^= bit<<(7-bitNum);
54 bitNum++;
55 }
56
57 int main() {
58 FILE *fp;
59 fp = fopen("picture.pbm","wb");
60 fprintf(fp, "P4 %d %d\n", XDIM, YDIM);
61 for(int j=0; j<YDIM; j++)
62 for(int i=0; i<XDIM; i++)
63 writeBit(tof(i, j), fp);
64 fclose(fp);
65 return 0;
66 }
Source Code
Demo
![]()