#include <iostream> |
#include <fstream> |
#include <iomanip> |
#include <cmath> |
#include <stdint.h> |
using namespace std; |
#pragma pack(1) |
#define canvas_max 10000 |
#define PI 3.14159 |
struct BitMapFileHeader |
{ |
uint16_t bfType; // 2×Ö½Ú£¬ÎļþÀàÐÍ£¨magic number£©£¬Ò»°ãΪ0x4D42£» |
uint32_t bfSize; // 4×Ö½Ú£¬BMPÎļþµÄ´óС£¨µ¥Î»Îª×Ö½Ú£©£» |
uint16_t bfReserved1; // 2×Ö½Ú£¬±£Áô£¬Ò»°ãÉèÖÃΪ0£» |
uint16_t bfReserved2; // 2×Ö½Ú£¬±£Áô£¬Ò»°ãÉèÖÃΪ0£» |
uint32_t bfOffBits; // 4×Ö½Ú£¬Î»Í¼Êý¾Ý£¨ÏñËØÊý×飩µÄµØÖ·Æ«ÒÆ£¬Ò²¾ÍÊÇÆðʼµØÖ·¡£ |
}; |
struct BitMapInfoHeader |
{ |
uint32_t biSize; // 4×Ö½Ú£¬ÐÅϢͷµÄ´óС£¬¼´40£» |
int32_t biWidth; // 4×Ö½Ú£¬£¨ÓзûºÅÕûÊý£©ÒÔÏñËØΪµ¥Î»ËµÃ÷ͼÏñµÄ¿í¶È£» |
int32_t biHeight; // 4×Ö½Ú£¬£¨ÓзûºÅÕûÊý£©ÒÔÏñËØΪµ¥Î»ËµÃ÷ͼÏñµÄ¸ß¶È; |
uint16_t biPlanes; // 2×Ö½Ú£¬ÎªÄ¿±êÉ豸˵Ã÷ÑÕɫƽÃæÊý£¬×ܱ»ÉèÖÃΪ1£» |
uint16_t biBitCount; // 2×Ö½Ú£¬ËµÃ÷±ÈÌØÊý/ÏñËØÊý£¬ÖµÓÐ1¡¢2¡¢4¡¢8¡¢16¡¢24¡¢32£» |
uint32_t biCompression; // 4×Ö½Ú£¬ËµÃ÷ͼÏñµÄѹËõÀàÐÍ£¬×î³£ÓõľÍÊÇ0£¨BI_RGB£©£¬±íʾ²»Ñ¹Ëõ£» |
uint32_t biSizeImages; // 4×Ö½Ú£¬ËµÃ÷λͼÊý¾ÝµÄ´óС£¬µ±ÓÃBI_RGB¸ñʽʱ£¬¿ÉÒÔÉèÖÃΪ0£» |
int32_t biXPelsPerMeter; // 4×Ö½Ú£¬£¨ÓзûºÅÕûÊý£©±íʾˮƽ·Ö±æÂÊ£¬µ¥Î»ÊÇÏñËØ/Ã×£» |
int32_t biYPelsPerMeter; // 4×Ö½Ú£¬£¨ÓзûºÅÕûÊý£©±íʾ´¹Ö±·Ö±æÂÊ£¬µ¥Î»ÊÇÏñËØ/Ã×£» |
uint32_t biClrUsed; // 4×Ö½Ú£¬ËµÃ÷λͼʹÓõĵ÷É«°åÖеÄÑÕÉ«Ë÷ÒýÊý£¬Îª0˵Ã÷ʹÓÃËùÓУ» |
uint32_t biClrImportant; // 4×Ö½Ú£¬ËµÃ÷¶ÔͼÏñÏÔʾÓÐÖØÒªÓ°ÏìµÄÑÕÉ«Ë÷ÒýÊý£¬Îª0˵Ã÷¶¼ÖØÒª£» |
}; |
struct Pixel |
{ |
uint8_t blue; |
uint8_t red; |
uint8_t green; |
}; |
struct Pixel pixel[canvas_max][canvas_max]; |
struct BitMapFileHeader BMFH; |
struct BitMapInfoHeader BMIH; |
void plot2D ( double (*func)( double ), double minX, double maxX, int color); |
double func1 ( double x); |
double func2 ( double x); |
void plotPara ( double (*funcX)( double ), double (*funcY)( double ), double minT, double maxT, int color); |
double func31 ( double t); |
double func32 ( double t); |
//void plot3D (double (*func )(double , double),double minX, double maxX,double minY, double maxY); |
void createCanvas( int width, int height, int origX, int origY, int ratio, bool axis); |
//void createCanvas3D (int width, int height,int origX, int origY,double rxx, double rxy,double ryx, double ryy,double rzx, double rzy); |
//void saveCanvas (char * filename); |
int canvas_height, canvas_width,canvas_origX,canvas_origY,canvas_ratio; |
void creatCanvas( int width, int height, int origX, int origY, int ratio, bool axis) |
{ |
ofstream fout( "output.bmp" ,ios::binary); |
BMFH.bfType = 0x4D42; |
BMFH.bfReserved1 = 0; |
BMFH.bfReserved2 = 0; |
BMFH.bfSize = sizeof (BMFH) + sizeof (BMIH) + sizeof (Pixel)* width * height; |
BMFH.bfOffBits = sizeof (BMFH) + sizeof (BMIH); |
BMIH.biSize = 40; |
BMIH.biWidth = width; |
BMIH.biHeight = height; |
BMIH.biPlanes = 1; |
BMIH.biBitCount = 24; |
BMIH.biCompression = 0; |
BMIH.biSizeImages = sizeof (Pixel)* width * height; |
BMIH.biXPelsPerMeter = 1000; |
BMIH.biYPelsPerMeter = 1000; |
BMIH.biClrImportant = 0; |
BMIH.biClrUsed = 0; |
canvas_height=height; |
canvas_width=width; |
canvas_origX=origX; |
canvas_origY=origY; |
canvas_ratio=ratio; |
fout.write(( char *)&BMFH, sizeof (BMFH)); |
fout.write(( char *)&BMIH, sizeof (BMIH)); |
for ( int i=0; i<canvas_max; i++) |
for ( int j=0; j<canvas_max; j++) |
{ |
pixel[i][j].blue=255; |
pixel[i][j].green=255; |
pixel[i][j].red=255; |
} |
|
if (axis) |
{ |
for ( int m=0; m<width; m++) |
{ |
pixel[m][origY].blue=0; |
pixel[m][origY].green=0; |
pixel[m][origY].red=0; |
} |
for ( int n=0; n<height; n++) |
{ |
pixel[origX][n].blue=0; |
pixel[origX][n].green=0; |
pixel[origX][n].red=0; |
} |
for ( int p=origX+ratio; p<width; p+=ratio) |
{ |
for ( int a=origY; a<origY+2; a++) |
{ |
pixel[p][a].blue=0; |
pixel[p][a].green=0; |
pixel[p][a].red=0; |
} |
} |
for ( int p=origX-ratio; p>0; p-=ratio) |
{ |
for ( int a=origY; a<origY+2; a++) |
{ |
pixel[p][a].blue=0; |
pixel[p][a].green=0; |
pixel[p][a].red=0; |
} |
} |
for ( int q=origY+ratio; q<height; q+=ratio) |
{ |
for ( int b=origX; b<origX+2; b++) |
{ |
pixel[b][q].blue=0; |
pixel[b][q].green=0; |
pixel[b][q].red=0; |
} |
} |
for ( int q=origY-ratio; q>0; q-=ratio) |
{ |
for ( int b=origX; b<origX+2; b++) |
{ |
pixel[b][q].blue=0; |
pixel[b][q].green=0; |
pixel[b][q].red=0; |
} |
} |
{ //¿ÉÌ滻Ϊѻ· |
if (origX>0&&height>1) |
{ |
pixel[origX-1][height-2].blue=0; |
pixel[origX-1][height-2].green=0; |
pixel[origX-1][height-2].red=0; |
} |
if (origX>0&&height>2) |
{ |
pixel[origX-1][height-3].blue=0; |
pixel[origX-1][height-3].green=0; |
pixel[origX-1][height-3].red=0; |
} |
if (height>1) |
{ |
pixel[origX+1][height-2].blue=0; |
pixel[origX+1][height-2].green=0; |
pixel[origX+1][height-2].red=0; |
} |
if (height>2) |
{ |
pixel[origX+1][height-3].blue=0; |
pixel[origX+1][height-3].green=0; |
pixel[origX+1][height-3].red=0; |
} |
if (origX>1&&height>2) |
{ |
pixel[origX-2][height-3].blue=0; |
pixel[origX-2][height-3].green=0; |
pixel[origX-2][height-3].red=0; |
} |
if (height>2) |
{ |
pixel[origX+2][height-3].blue=0; |
pixel[origX+2][height-3].green=0; |
pixel[origX+2][height-3].red=0; |
} |
} |
{ //¿ÉÌ滻Ϊѻ· |
if (width>1&&origY>0) |
{ |
pixel[width-2][origY-1].blue=0; |
pixel[width-2][origY-1].green=0; |
pixel[width-2][origY-1].red=0; |
} |
if (width>1) |
{ |
pixel[width-2][origY+1].blue=0; |
pixel[width-2][origY+1].green=0; |
pixel[width-2][origY+1].red=0; |
} |
if (width>2&&origY>1) |
{ |
pixel[width-3][origY-2].blue=0; |
pixel[width-3][origY-2].green=0; |
pixel[width-3][origY-2].red=0; |
} |
if (width>2&&origY>0) |
{ |
pixel[width-3][origY-1].blue=0; |
pixel[width-3][origY-1].green=0; |
pixel[width-3][origY-1].red=0; |
} |
if (width>2) |
{ |
pixel[width-3][origY+2].blue=0; |
pixel[width-3][origY+2].green=0; |
pixel[width-3][origY+2].red=0; |
} |
if (width>2) |
{ |
pixel[width-3][origY+1].blue=0; |
pixel[width-3][origY+1].green=0; |
pixel[width-3][origY+1].red=0; |
} |
} |
} |
for ( int m=0; m<width; m++) |
{ |
for ( int n=0; n<height; n++) |
{ |
fout.write(( char *)&pixel[n][m], sizeof (pixel[n][m])); |
} |
} |
fout.close(); |
} |
void plot2D( double (*func)( double ), double minX, double maxX, int color) |
{ |
ofstream fbmp( "output.bmp" ,ios::binary); |
|
fbmp.write(( char *)&BMFH, sizeof (BMFH)); |
fbmp.write(( char *)&BMIH, sizeof (BMIH)); |
|
int b=(color & 0x0000ff); |
int g=(color & 0x00ff00) >> 8; |
int r=(color & 0xff0000) >> 16; |
|
int min=minX*canvas_ratio+canvas_origX, max=maxX*canvas_ratio+canvas_origX; |
if (min<0) |
min=0; |
if (min>canvas_max) |
exit (1); |
if (max>canvas_max) |
max=(( double )canvas_ratio*(*func)(( double )(canvas_max-canvas_origX)/( double )canvas_ratio)+( double )canvas_origY); |
if (max<0) |
exit (-1); |
for ( int i=min; i<=max; i++) |
{ |
pixel[i][( int )(( double )canvas_ratio*(*func)(( double )(i-canvas_origX)/( double )canvas_ratio)+( double )canvas_origY)].blue=b; |
pixel[i][( int )(( double )canvas_ratio*(*func)(( double )(i-canvas_origX)/( double )canvas_ratio)+( double )canvas_origY)].green=g; |
pixel[i][( int )(( double )canvas_ratio*(*func)(( double )(i-canvas_origX)/( double )canvas_ratio)+( double )canvas_origY)].red=r; |
} |
for ( int m=0; m<canvas_width; m++) |
{ |
for ( int n=0; n<canvas_height; n++) |
{ |
fbmp.write(( char *)&pixel[n][m], sizeof (pixel[n][m])); |
} |
} |
fbmp.close(); |
} |
double func1( double x) |
{ |
return 0.5*x; |
} |
double func2( double x) |
{ |
return x*x; |
} |
void plotPara ( double (*funcX)( double ), double (*funcY)( double ), double minT, double maxT, int color) |
{ |
ofstream fbmp( "output.bmp" ,ios::binary); |
|
fbmp.write(( char *)&BMFH, sizeof (BMFH)); |
fbmp.write(( char *)&BMIH, sizeof (BMIH)); |
|
int b=(color & 0x0000ff); |
int g=(color & 0x00ff00) >> 8; |
int r=(color & 0xff0000) >> 16; |
|
for ( double i=minT; i<maxT; i+=0.01) |
{ |
pixel[( int )(func31(( double )i)*( double )canvas_ratio+( double )canvas_origX)][( int )(func32(( double )i)*( double )canvas_ratio+( double )canvas_origY)].blue=b; |
pixel[( int )(func31(( double )i)*( double )canvas_ratio+( double )canvas_origX)][( int )(func32(( double )i)*( double )canvas_ratio+( double )canvas_origY)].green=g; |
pixel[( int )(func31(( double )i)*( double )canvas_ratio+( double )canvas_origX)][( int )(func32(( double )i)*( double )canvas_ratio+( double )canvas_origY)].red=r; |
} |
for ( int m=0; m<canvas_width; m++) |
{ |
for ( int n=0; n<canvas_height; n++) |
{ |
fbmp.write(( char *)&pixel[n][m], sizeof (pixel[n][m])); |
} |
} |
fbmp.close(); |
} |
double func31( double t) |
{ |
return cos (t)*2- cos (t*2); |
} |
double func32( double t) |
{ |
return sin (t)*2- sin (t*2); |
} |
double func4( double x) |
{ |
return sqrt (2* sqrt (x*x)-x*x); |
} |
double func5( double x) |
{ |
return asin ( abs (x)-1)-PI/2; |
} |
double func6( double x) //´Ë±í´ïʽ²»¶Ô £¬µ«ÊÇÎÒ²»»áÁË£¡ |
{ |
return 5*( sqrt ( cos (0.2*x))* cos (40*x)+ sqrt ( abs (0.2*x))-0.7)* pow ((4-0.04*x*x),0.01); |
} |
int main() |
{ |
creatCanvas(500,500,250,250,10, true ); |
//plot2D(func1,-9,9,0x000000); |
//plot2D(func2,-10,10,0x000000); |
//plotPara(func31,func32,0,2*PI,0x000000); |
plot2D(func5,-2,2,0x000000);plot2D(func4,-2,2,0x000000); |
//plotPara(func31,func32,0,2*PI,0x000000);plot2D(func6,-5,5,0x000000); |
return 0; |
} |