#include<conio.h>
#include<graphics.h>
#include<math.h>
void khoitao()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI"); }
void swap(int &x,int &y)
{
int tam = x;
x = y;
y = tam;
}
void line1(int x1,int y1,int x2,int y2,int color)
{
int x,y,p,step,c1,c2,dx,dy;
if(x1>x2){
swap(x1,x2);
swap(y1,y2);
}
x = x1; y = y1;
dx = x2 -x1;
dy = abs(y1-y2);
step = (y1>y2)?-1:1;
c1 = 2 * dy;
c2 = 2*(dy - dx);
p = 2*dy -dx;
Trang 2{ putpixel(x,y,color);
if(p<0) p+=c1;
else{ p+=c2;
y+=step;
} }
}
void line2(int x1,int y1,int x2,int y2,int color)
{
int x,y,p,step,c1,c2,dx,dy;
if(y1>y2){
swap(x1,x2);
swap(y1,y2);
}
x = x1; y = y1;
dx = y2 -y1;
dy = abs(x1-x2);
step = (x1>x2)?-1:1;
c1 = 2 * dy;
c2 = 2*(dy - dx);
p = 2*dy -dx;
for(y=y1;y<=y2;y++)
{ putpixel(x,y,color);
if(p<0) p+=c1;
else{ p+=c2;
x+=step;
} }
Trang 3void line(int x1,int y1,int x2,int y2,int color)
{
x1 = getmaxx()/2+x1;
y1 = getmaxy()/2-y1;
x2 = getmaxx()/2+x2;
y2 = getmaxy()/2-y2;
int dx = abs(x2-x1);
int dy = abs(y1 -y2);
if(dx>dy) line1(x1,y1,x2,y2,color);
else line2(x1,y1,x2,y2,color);
}
void hetoado()
{
int color = 15;
line(0,getmaxy()/2-10,0,10-getmaxy()/2,color);
line(getmaxx()/2+10,0,10-getmaxx()/2,0,color);
}
void tamgiac(int x1,int y1,int x2,int y2,int x3,int y3,int color)
{
line(x1,y1,x2,y2,color);
line(x1,y1,x3,y3,color);
line(x2,y2,x3,y3,color);
}
void quaydiem(int x,int y,int xq,int yq,float goc,int &x1,int &y1)
{
float al = goc*RADS;
x1 = (int)(x*cos(al)-y*sin(al) + (1-cos(al))*xq+sin(al)*yq); y1 = (int)(x*sin(al)+y*cos(al) - sin(al)*xq+(1-cos(al))*yq);
Trang 4outtextxy(xq+getmaxx()/2,getmaxy()/2-yq,"O tam quay");
}
void quayTG(int x1,int y1,int x2,int y2,int x3,int y3,int xq,int yq,float goc,int color)
{
int x11,y11,x22,y22,x33,y33;
quaydiem(x1,y1,xq,yq,goc,x11,y11);
quaydiem(x2,y2,xq,yq,goc,x22,y22);
quaydiem(x3,y3,xq,yq,goc,x33,y33);
tamgiac(x11,y11,x22,y22,x33,y33,color);
}
void hcn(int x1,int y1,int x2,int y2,int color)
{
line(x1,y1,x1,y2,color);
line(x1,y1,x2,y1,color);
line(x1,y2,x2,y2,color);
line(x2,y2,x2,y1,color);
}
void quayHCN(int x1,int y1,int x2,int y2,int xq,int yq,int goc,int color)
{
int x11,y11,x22,y22,x33,y33,x44,y44;
quaydiem(x1,y1,xq,yq,goc,x11,y11); //a
quaydiem(x2,y1,xq,yq,goc,x22,y22); //b
quaydiem(x2,y2,xq,yq,goc,x33,y33);//c
quaydiem(x1,y2,xq,yq,goc,x44,y44); //d
line(x11,y11,x22,y22,color);
line(x22,y22,x33,y33,color);
line(x33,y33,x44,y44,color);
Trang 5}
void biendang(int x,int y,float a,float b,int &x1,int &y1)
{
x1 = b*y +x;
y1 = a*x +y;
}
void bdhcn(int x1,int y1,int x2,int y2,float a,float b,int color)
{
int x11,y11,x22,y22,x33,y33,x44,y44;
biendang(x1,y1,a,b,x11,y11); //a
biendang(x2,y1,a,b,x22,y22); //b
biendang(x2,y2,a,b,x33,y33);//c
biendang(x1,y2,a,b,x44,y44); //d
line(x11,y11,x22,y22,color);
line(x22,y22,x33,y33,color);
line(x33,y33,x44,y44,color);
line(x44,y44,x11,y11,color);
}
void bdtg(int x1,int y1,int x2,int y2,int x3,int y3,float a,float b,int color)
{
int x11,y11,x22,y22,x33,y33;
biendang(x1,y1,a,b,x11,y11); //a
biendang(x2,y2,a,b,x22,y22); //b
biendang(x3,y3,a,b,x33,y33);//c
tamgiac(x11,y11,x22,y22,x33,y33,color);
}
Trang 6void put8pixel(int xc,int yc,int x,int y,int color)
{
putpixel(xc-x,yc-y,color);
putpixel(xc-x,yc+y,color);
putpixel(xc+x,yc-y,color);
putpixel(xc+x,yc+y,color);
putpixel(xc-y,yc-x,color);
putpixel(xc-y,yc+x,color);
putpixel(xc+y,yc-x,color);
putpixel(xc+y,yc+x,color);
}
void tron(int xc,int yc,int r,int color)
{
int x,y,p;
x = 0;
y = r;
p = 3 + 2*r;
xc = getmaxx()/2 +xc;
yc = getmaxy()/2 -yc;
while(x<=y){
put8pixel(xc,yc,x,y,color);
if(p<0) p+=4*x+6;
else{ p+=4*(x-y)+10;
y ;
} x++;
}
}
void to(int x,int y,int mauto,int maubien)
Trang 7int mauht;
mauht= getpixel(x,y);
if(mauht!=mauto && mauht!=maubien) {
putpixel(x,y,mauto);
to(x-1,y,mauto,maubien);
to(x,y+1,mauto,maubien);
to(x+1,y,mauto,maubien);
to(x,y-1,mauto,maubien);
}
}
void tomau(int x,int y,int mauto,int maubien)
{
x=getmaxx()/2+x;
y=getmaxy()/2-y;
to(x,y,mauto,maubien);
}
void sin()
{
int x1,y1,x2,y2,k;
float step,x,y;
step = 0.0001;
k =20;
x1 = getmaxx()/2;
y1 = getmaxy()/2;
for(x=-10;x<=10;x+=step)
{
y = sin(x);
Trang 8x2 = x1+ceil(x*k);
y2 = y1+ceil(y*k);
putpixel(x2,y2,4);
}
}
void put4pixel(int xc,int yc,int x,int y,int color)
{
putpixel(xc+x,yc+y,color);
putpixel(xc+x,yc-y,color);
putpixel(xc-x,yc+y,color);
putpixel(xc-x,yc-y,color);
}
void elip(int xc,int yc,int a,int b,int color)
{
xc = getmaxx()/2+xc;
yc = getmaxy()/2-yc;
double z1,z2,p;
int x,y;
x =0;
y=b;
z1 = (double)(b*b)/(a*a);
z2 = (double)1/z1;
p = 2*z1 - 2*b+1;
while(z1*(double)x/y<=1)
{ put4pixel(xc,yc,x,y,color);
if(p<0) p+=2*z1*(2*x+3); else{ p+=2*z1*(2*x+3)+4*(1-y);
y ;
Trang 9} x++;
}
x = a;
y = 0;
p = 2*z2 - 2*a+1;
while(z2*(double)y/x<=1)
{ put4pixel(xc,yc,x,y,color);
if(p<0) p+=2*z2*(2*y+3);
else{ p+=2*z2*(2*y+3)+4*(1-x);
x ;
} y++;
}
}
void main()
{
clrscr();
int x1=50,y1=20,x2=50,y2=100,x3=100,y3=20,xq = 50, yq = -140; float goc = 100;
khoitao();
hetoado();
tamgiac(x1,y1,x2,y2,x3,y3,5);
tomau((x1+x2+x3)/3,(y1+y2+y3)/3,6,5);
quayTG(x1,y1,x2,y2,x3,y3,xq,yq,goc,2);
bdtg(x1,y1,x2,y2,x3,y3,1,1.6,2);
int a1 = 100,b1=-40,a2 =200,b2 = -80;
Trang 10quayHCN(a1,b1,a2,b2,xq,yq,120,4); bdhcn(a1,b1,a2,b2,-0.5,-0.8,2);
tron(-100,130,30,4);
tomau(-100,130,2,4);
elip(-200,-100,50,20,5);
tomau(-200,-100,9,5);
sin();
getch();
}