#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
float a,b,c,d;
int round (float x){
if (x > 0){
return int (x + 0.5);
}else{
return int (x - 0.5);
}
}
void startGraphics (){
int gd=0,gm=0;
initgraph(&gd,&gm,"");
}
//Tinh gia tri f(x), voi f(x) cho truoc.
float f(float x){
return (a*x*x*x + b*x*x + c*x+d);
}
void Draw (float x_min, float x_max){
int x0,y0,x1,y1,x2,y2;
float x,dx,k;
x0=getmaxx()/2;//Dat goc toa do len man hinh
y0=getmaxy()/2;//Dat goc toa do len man hinh
k=(float)getmaxx()/30; //Chia truc Ox lam 30 phan bang nhau khoang cach k.
/*Tao ra 1 don vi ao cua truc Oxy*/
dx=.001; //Chon buoc tang cua dx
setcolor(10);
line(0,y0,2*x0,y0); //Ve truc Ox
line(x0,0,x0,2*y0); //Ve truc Ox
x=x_min;
setcolor(4);
x1=x0+round(x*k);
y1=y0-round(f(x)*k);
moveto(x1,y1);
while (x < x_max)
{
x=x+dx;
x2=x0+round(x*k);
y2=y0-round(f(x)*k);
//putpixel(x2,y2,14);
lineto (x2,y2);
}
}
int main (){
a=1,b=-1,c=-1,d=2;
float x_min = -10, x_max = 20;
startGraphics();
Draw (x_min,x_max);
getch ();
closegraph();
return 0;
}