使用Visual C++ 2010 Express和EasyX绘制函数图像

使用Visual C++ 2010 Express和EasyX绘制函数图像

math中常用库函数:

int abs(int i);//返回整型参数i的绝对值

double fabs(double x);//返回双精度参数x的绝对值

long labs(long n);//返回长整型参数n的绝对值

double exp(double x);//返回指数函数e^x的值

double log(double x);//返回logex的值

double log10(double x) 返回log10x的值

double pow(double x,double y) 返回x^y的值

double pow10(int p) 返回10^p的值

double sqrt(double x) 返回+√x的值

double acos(double x) 返回x的反余弦arccos(x)值,x为弧度

double asin(double x) 返回x的反正弦arcsin(x)值,x为弧度

double atan(double x) 返回x的反正切arctan(x)值,x为弧度

double cos(double x) 返回x的余弦cos(x)值,x为弧度

double sin(double x) 返回x的正弦sin(x)值,x为弧度

double tan(double x) 返回x的正切tan(x)值,x为弧度

double hypot(double x,double y) 返回直角三角形斜边的长度(z),

                                                                    x和y为直角边的长度,z^2=x^2+y^2

double ceil(double x) 返回不小于x的最小整数

double floor(double x) 返回不大于x的最大整数

double atof(char *nptr) 将字符串nptr转换成浮点数并返回这个浮点数

double atol(char *nptr) 将字符串nptr转换成长整数并返回这个整数

double atof(char *nptr) 将字符串nptr转换成双精度数,并返回这个数,错误返回0

int atoi(char *nptr) 将字符串nptr转换成整型数, 并返回这个数,错误返回0

long atol(char *nptr) 将字符串nptr转换成长整型数,并返回这个数,错误返回0

使用easyx绘制函数图像

一直在黑窗口练习字符输出程序,是否感觉很乏味。现在使用easyx绘制函数图像。

EasyX使用介绍,可见 https://blog.csdn.net/cnds123/article/details/127865557

下面给出y=x^2函数图像源码。

启动Visual C++(我用的是Visual C++ 2010 Express),创建一个空的控制台项目(Win32 Console Application),【若出现如下提示

使用Visual C++ 2010 Express和EasyX绘制函数图像_第1张图片

(大意是“是否添加CLR组件”,不需要),单击“No”】

然后添加一个新的代码文件(.cpp),下面给出一个绘制数学函数图像的源码:

#include    // 引用图形库头文件
#include 
#include

void tenZ() {//直角坐标绘制函数
	//直角坐标绘制函数
	int centx = 500;//原点x
	int centy = 300;//原点y
	setlinecolor(RED);
	settextcolor(GREEN);
	line(30, 300, 970, 300);//x轴
	line(500, 0, 500, 600);//y轴
	outtextxy(485,305, 'O');//原点
	line(500, 0, 490, 20);//y轴箭头
	line(500, 0, 510, 20);//y轴箭头
	outtextxy(460, 10, 'Y');//y
	line(950, 290, 970, 300);//x轴箭头
	line(950, 310, 970, 300);//x轴箭头
	outtextxy(950, 320, 'X');

	int x=500, y=300;
	int num = 1;
	char ch;
	while (y > 10)//y上半轴坐标点
	{
		line(x, y, x+5, y);
		y = y - 50;
		ch = num + 48;
		if(y>10)
			outtextxy(x - 10, y-5, ch);
		num++;
	}


	y = 300;
	num = 1;
	while (y <600)//y下半轴坐标点
	{
		line(x, y, x + 5, y);
		y = y + 50;
		ch = num + 48;
		if (y < 600) {
			outtextxy(x - 10, y - 5, ch);
			outtextxy(x - 15, y - 5, '-');
		}
		num++;
	}

	y = 300;
	num = 1;
	while (x < 940)//x右半轴坐标点
	{
		line(x, y-5, x, y);
		x = x + 50;
		ch = num + 48;
		if (x < 940)
			outtextxy(x-3, y+3, ch);
		num++;
	}


	num = 1;
	x = 500;
	while (x > 30)//x左半轴坐标点
	{
		line(x, y - 5, x, y);
		x = x - 50;
		ch = num + 48;
		if (x > 30) {
			outtextxy(x - 3, y + 3, ch);
			outtextxy(x - 8, y +3, '-');
		}
		num++;
	}

}

void Phan()//绘制函数
{
	double x, y;
	double tempx,tempy;
	for(x = -8; x < 8; x=x+0.001) {
		//y =tan(x);  //y=2*sin(2*x)即y=2sin(2x)
		y=pow(x,2);  //y=x^2  
		tempx = x;
		tempy = y;
		if (tempx >= 0){
			tempx = x*50+500;
		}else if(tempx<0){
			tempx = 500+x*50;
		}
		if (tempy >= 0){
			tempy = 300 - y * 50;
		}else if(tempy<0){
			tempy = 300 - y * 50;
		}
		putpixel(tempx, tempy, RED);
	}
}


void Hname()//绘制函数名字
{   
	//wchar_t s[] = L"函数:f(x)=tan(x)";
	wchar_t s[] = L"函数:f(x)=x^2";
	//outtextxy(800,5, 'U');
	outtextxy(800,5, s);
}

int main()
{
	initgraph(1000, 600);// 初始化绘图窗口
	setbkcolor(WHITE);
	cleardevice();
	tenZ(); //调用直角坐标绘制函数
	Phan();
	Hname();

	// 按任意键退出
	_getch();
	closegraph();
	return 0;
}

 效果图:

使用Visual C++ 2010 Express和EasyX绘制函数图像_第2张图片

其中,y=pow(x,2)表示是画y=x^2函数图像,修改该句可以画出其它函数图像,如y=2*sin(2*x)即y=2sin(2x)等等。

OK!

你可能感兴趣的:(C,C++学习,编程实践系列,c++,开发语言)