do hoa
1. Vẽ Elip
/*------------------------------
FILE : ELIP.CPP
AUTHOR: Mr.Hai
UPDATE: 03/03/2010
------------------------------*/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
// Ham khoi tao che do do hoa
void InitGraph()
{
int gd=0, gm=0;
initgraph(&gd,&gm,"d:\\tc\\bgi");
//setbkcolor(RED);
//setcolor(YELLOW);
}
// Ham ve 4 diem anh
void Put4Pixel(int a,int b,int x,int y,int c)
{
putpixel(a+x,b+y,c);
putpixel(a+x,b-y,c);
putpixel(a-x,b+y,c);
putpixel(a-x,b-y,c);
}
// Ham ve duong elip tam I(x0,y0), ban kinh 1 bang a, ban kinh 2 bang b
void DrawElip(int x0,int y0, int a, int b, int c)
{
double a1,b1,d1,d2;
int x=0;
int y=b;
a1=a*a;
b1=b*b;
d1=b1-a1*b+a1/4;
d2=b1*(x+1/2)*(x+1/2)+a1*(y-1)*(y-1)-a1*b1;
//kiem tra gradient F van nam trong mien 1
while(2*b1*(x+1)<a1*(2*y-1))
{
// trong mien 1
Put4Pixel(x0,y0,x,y,c);
if(d1<0) //chon S
d1=d1+b1*(2*x+3);
else //chon P
{
d1=d1+b1*(2*x+3)+a1*(2-2*y);
y--;
}
x++;
delay(20);
}
while(y>=0)
{
// trong mien 2
Put4Pixel(x0,y0,x,y,c);
if(d2>=0) //chon L
d2=d2+a1*(3-2*y);
else //chon R
{
d2=d2+a1*(3-2*y)+b1*(2*x+2);
x++;
}
y--;
delay(20);
}
}
void main()
{
int x0,y0,a,b;
printf("
Nhap toa do tam elip I(x0,y0) = "); scanf("%d%d",&x0,&y0);
printf("
Nhap ban kinh a = ");scanf("%d",&a);
printf("
Nhap ban kinh b = ");scanf("%d",&b);
InitGraph();
DrawElip(x0,y0,a,b,WHITE);
getch();
closegraph();
}
2.Vẽ Đoạn Thẳng
/*------------------------------
FILE : LINE.CPP
AUTHOR: Mr.Hai
UPDATE: 18/03/2010
------------------------------*/
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
// Ham khoi tao che do do hoa
void InitGraph()
{
int gd=0, gm=0;
initgraph(&gd,&gm,"d:\\tc\\bgi");
//setbkcolor(RED);
//setcolor(YELLOW);
}
// Ham ve doan thang tu A(xa,ya) den B(xb,yb) voi he so goc la m
void DrawLine(int xa,int ya,int xb,int yb,int c)
{
int x,y,Dx,Dy,d,c1,c2,tg;
x = xa;
y = ya;
if(xa>xb) // dam bao Dx luon duong
{
tg=xa; xa=xb; xb=tg;
tg=ya; ya=yb; yb=tg;
}
Dx = xb - xa;
Dy = yb - ya;
d = 2*Dy - Dx;
c1 = 2*Dy ;
c2 = 2*(Dy - Dx);
// TH: m=0
if(Dy == 0)
for(x=xa;x<=xb;x++)
{
putpixel(x,ya,c);
delay(20);
}
// TH: m=1
if(Dy == Dx)
for(x=xa,y=ya;x<=-xb;x++,y++)
{
putpixel(x,y,c);
delay(20);
}
// TH: m=-1
if(Dy == -Dx)
for(x=xa,y=ya;x<=xb;x++,y--)
{
putpixel(xa,ya,c);
delay(20);
}
// TH: m=vo cung
if(Dx == 0)
{
int y1=ya<yb ? ya:yb;
int y2=ya>yb ? ya:yb;
for(y=y1;y<=y2;y++)
{
putpixel(xa,y,c);
delay(20);
}
}
// TH: 0<m<1
if((Dx>0) && (Dy<Dx))
{
for(x=xa;x<=xb;x++)
{
putpixel(x,y,c);
if(d<0)
d+=c1;
else
{
y++;
d+=c2;
}
delay(20);
}
}
// TH: m>1
if(Dy>Dx)
{
int x1a=ya; int y1a=xa;
int x1b=yb; int y1b=xb;
int D1x=Dy; int D1y=Dx;
y=y1a;
d=2*D1y-D1x;
c1=2*D1y;
c2=2*(D1y-D1x);
for(x=x1a;x<=x1b;x++)
{
putpixel(y,x,c);
if(d<0)
d+=c1;
else
{
y++;
d+=c2;
}
delay(20);
}
}
// TH: -1<m<0
if((-Dx<Dy)&&(Dy<0))
{
int x1a=-xb; int y1a=yb;
int x1b=-xa; int y1b=ya;
int D1x=Dx; int D1y=-Dy;
y=y1a;
d=2*D1y-D1x;
c1=2*D1y;
c2=2*(D1y-D1x);
for(x=x1a;x<=x1b;x++)
{
putpixel(-x,y,c);
if(d<0)
d+=c1;
else
{
y++;
d+=c2;
}
delay(20);
}
}
// TH: m<-1
if(Dy<-Dx)
{
xa=-yb; ya=-xb;
xb=ya ; yb=-xa;
int D1x=-Dy; int D1y=Dx;
d=2*D1y-D1x;
c1=2*D1y;
c2=2*(D1y-D1x);
for(x=xa;x<=xb;x++)
{
putpixel(-y,x,c);
if(d<0)
d+=c1;
else
{
y++;
d+=c2;
}
delay(20);
}
}
}
void main()
{
int xa,ya,xb,yb;
printf("Nhap tao do diem thu nhat: A(xa,ya) = "); scanf("%d%d",&xa,&ya);
printf("Nhap tao do diem thu hai : B(xb,yb) = "); scanf("%d%d",&xb,&yb);
InitGraph();
DrawLine(xa,ya,xb,yb,WHITE);
getch();
closegraph();
}
3.Vẽ Đường Tròn
/*------------------------------
FILE: CIRCLE.CPP
AUTHOR: Mr.Hai
UPDATE: 02/03/2010
------------------------------*/
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
void InitGraph()
{
int gd=0,gm=0;
initgraph(&gd,&gm,"d:\\tc\\bgi");
//setbkcolor(RED);
//setcolor(WHITE);
}
void DrawCircle(int x,int y,int r,int c)
{
int a,b,d;
a = 0;
b = r;
d = 1-r;
while(a<=b)
{
putpixel( a+x, b+y,c);
putpixel( a+x,-b+y,c);
putpixel(-a+x, b+y,c);
putpixel(-a+x,-b+y,c);
putpixel( b+x, a+y,c);
putpixel( b+x,-a+y,c);
putpixel(-b+x, a+y,c);
putpixel(-b+x,-a+y,c);
if(d<0)
{
d+= 2*a+3;
}
else
{
d+= 2*a - 2*b + 5;
b--;
}
a++;
delay(20); // xem qua trinh ve duong tron
}
}
void main()
{
int m,n,p;
printf("Nhap toa do tam duong tron: I(m,n) = "); scanf("%d%d",&m,&n);
printf("Nhap ban kinh: r = "); scanf("%d",&p);
InitGraph();
DrawCircle(m,n,p,WHITE);
getch();
closegraph();
}
void dda(int x1,int y1,int x2,int y2,int color=YELLOW)
{
float x=x1,y=y1,temp=(abs(x2-x1)>=abs(y2-y1))?abs(x2-x1):abs(y2-y1);
putpixel(int(x+.05),int(y+0.5),color);
for(int i=0;i<temp;i++)
{
x+=(x2-x1)/temp;
y+=(y2-y1)/temp;
putpixel(int(x+.05),int(y+0.5),color);
}
}
nài thì hình tròn
PHP Code:
void put8pixel(int x,int y,int xc,int yc,int c)
{
putpixel( x+xc , y+yc, c);
putpixel(-x+xc , y+yc, c);
putpixel( x+xc ,-y+yc, c);
putpixel(-x+xc ,-y+yc, c);
putpixel( y+xc , x+yc, c);
putpixel(-y+xc , x+yc, c);
putpixel( y+xc ,-x+yc, c);
putpixel(-y+xc ,-x+yc, c);
}
void hinhtron(int xc,int yc,int r,int c=8)
{
int x=0,y=r,p=3-2*r;
put8pixel(x,y,xc,yc,c);
while (x<=y)
{
if(p<0)p+=4*x+++6;
else p+= 4*(x++-y--)+10;
put8pixel(x,y,xc,yc,c);
}
}
nài thì hình chữ nhật
PHP Code:
void hcn(int x1,int y1,int x2,int y2,int color)// hinh chu nhat thiet lap 1 hinh chu nhat nhu ham window
{
for(int i=y1;i<=y2;i++)
for(int j=x1;j<=x2;j++)
putpixel(j,i,color);
Bạn đang đọc truyện trên: AzTruyen.Top