[ create a new paste ] login | about

Link: http://codepad.org/irVeM9cR    [ raw code | fork ]

hieunv1996 - C++, pasted on Mar 5:
#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 sin (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=0.05; //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);
	// Khoi tao diem bat dau tren man hinh do hoa.
	// Va doi don vi thanh don vi mh do hoa. 
	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);
		printf ("\n(%d,%d)",x2,y2);
		//putpixel(x2,y2,14);
		lineto (x2,y2);
	}	
}

int main (){
	a=1,b=2,c=-1,d=1;
	float x_min = -10, x_max = 10;
	startGraphics();
	Draw (x_min,x_max);
	getch ();
	closegraph();
	return 0;
}


Create a new paste based on this one


Comments: