EasyX下载:EasyX Graphics Library for C++
绘制对勾函数 y=x+1/x;
#include <graphics.h>
#include<stdio.h>
#include <conio.h>
#include <math.h>
#define f(x) (x+(1/x))
double s; //缩放比例
int main()
{
initgraph(640, 480); //创建一个尺寸为 640x480 的绘图窗口;
setbkcolor(WHITE); //设置背景色为白色
cleardevice(); //用背景色清空画布
setorigin(320, 240); //修改原点为画布中心
setcolor(BLUE); //设置画笔颜色
line(-320,0,320,0); //画x轴
line(0,240,0,-240); //画y轴
double x1, y1;
int xs,ys,x2,y2,xf,yf; //xs,ys为缩放后的坐标;x2,y2为关于原点对称的坐标
//xf,yf为关于x轴对称的坐标;
s = 40;
for (double t =0.00001;t<=6.8899 ;t=t+0.0001 )
{
x1 = t;
y1 =(f(x1));
xs = int(x1 * s);
ys = int(y1 * s); // 将原点坐标进行缩放
xf = xs;
yf = -ys; //将缩放后的坐标点关于x轴对称
moveto(xf, yf);
lineto(xf, yf); //画出第一象限的函数图
x2 = -xf;
y2 = -yf;
moveto(x2, y2);
lineto(x2, y2); //画出第三象限函数图
}
getchar();
closegraph();
return 0;
}
绘制二次函数y=x^2
#include <graphics.h>
#include<stdio.h>
#include <conio.h>
#include <math.h>
#define f(x) (x*x)
double s; //缩放比例
int main()
{
initgraph(640, 480); //创建一个尺寸为 640x480 的绘图窗口;
setbkcolor(WHITE); //设置背景色为白色
cleardevice(); //用背景色清空画布
setorigin(320, 240); //修改原点为画布中心
setcolor(BLUE); //设置画笔颜色
line(-320, 0, 320, 0); //画x轴
line(0, 240, 0, -240); //画y轴
double x1, y1;
int xs, ys, x2, y2, xf, yf; //xs,ys为缩放后的坐标;x2,y2为关于原点对称的坐标
//xf,yf为关于x轴对称的坐标;
s = 40;
for (double t = 0.00001;t <= 6.8899;t = t + 0.0001)
{
x1 = t;
y1 = (f(x1));
xs = int(x1 * s);
ys = int(y1 * s); // 将原点坐标进行缩放
xf = xs;
yf = -ys; //将缩放后的坐标点关于x轴对称
moveto(xf, yf);
lineto(xf, yf); //画出第一象限的函数图
x2 = -xf;
y2 = yf;
moveto(x2, y2);
lineto(x2, y2); //画出第二象限函数图
}
getchar();
closegraph();
return 0;
}