ƯỜ
Ạ Ọ
Ẵ
NG Đ I H C BK ĐÀ N NG TR KHOA CÔNG NGH THÔNG TIN Ệ ----------
Ự
BÁO CÁO TH C HÀNH Đ H A MÁY TÍNH
Ồ Ọ
ễ
Giáo viên : Nguy n Văn Nguyên Sinh viên : Tr nh Hoàng Long ị L pớ : 11T2 Nhóm : 11B MSSV
: 102110212
10/04/2014
Đà N ng, ẵ
I. Bu iổ th c hành ự 1. Cài đ t và s d ng th vi n graphics.h: ặ
th m c C:\Dev-Cpp\include
1: ư ệ ử ụ 1.1. Download 2 file sau đây v máy: ề ể ở ư ụ th m c C:\Dev-Cpp\lib
ể ở ư ụ ớ ọ 1.2. Kh i đ ng Dev C++, vào File-->New-->Project...-->Empty Project (Nh ch n
ư ợ ặ ấ ụ ặ ả ộ ọ
i b ng cách t code l u l c khi vi ớ ượ ạ ư ạ ằ ướ ế
c t o ra trong Project. Tr ặ ặ ụ ọ
+ File graphics.h đ + File libbgi.a đ ở ộ C++ Project) -->OK Đ t 1 cái tên cho phù h p và l u vào đâu đó. Nh n chu t ph i lên cái project v a t o. -->New File ho c ch n m c New File ừ ạ trong menu Project M t file m i đ ộ Nh n Ctrl + S ho c File -->Save. ấ Nh n Alt + P ho c m c Project Options trong menu Project--->Ch n th ẻ ấ Parameters --> Gõ chính xác nh ng dòng sau vào khung Linker: ữ
-lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32
2. Dùng hàm line() đ v 1 ngôi nhà đ n gi n: ể ẽ
ơ
ả
Code:
#include
#include
#include
using namespace std;
void init(){ int gd = DETECT,gm; initgraph(&gd,&gm,""); } void ve(){ line(200,100,400,100); line(100,200,500,200); line(200,100,100,200); line(400,100,500,200); line(100,200,100,400); line(100,400,500,400); line(500,400,500,200); line(300,400,300,280); line(250,280,350,280); line(250,280,250,400);
line(350,280,350,400); line(150,280,200,280); line(200,280,200,330); line(200,330,150,330); line(150,330,150,280); line(400,280,450,280); line(450,280,450,330); line(450,330,400,330); line(400,330,400,280); } int main(){ init(); ve(); system("PAUSE"); return EXIT_SUCCESS; }
Demo:
S d ng thu t toán
ng tròn đ ng tâm và tô màu chúng:
ử ụ ậ
ồ
II. Bu iổ th c hành 2: ự 1.V các đ ẽ
ườ
Michener.
Code:
#include
#include
#include
#include
using namespace std;
void Mcircle(int R,int x0,int y0){
int x,y,p,c;
p = 1 - R;
c=getcolor();
x= 0;y=R;
while(x<=y){
putpixel(x+x0,y+y0,c);
putpixel(-x+x0,y+y0,c);
putpixel(x+x0,-y+y0,c);
putpixel(-x+x0,-y+y0,c);
putpixel(y+x0,x+y0,c);
putpixel(y+x0,-x+y0,c);
putpixel(-y+x0,x+y0,c);
putpixel(-y+x0,-x+y0,c);
if(p<0) p+=2*x + 3;
else { p+=2*(x-y)+5;y--;}
x++;
}
}
void totron(int xc, int yc, int R, int c)
{
for (int i=xc-R; i
{ for (int j=yc-R; jR-15) putpixel(i,j,c); }
}
}
int main(int argc, char *argv[])
{
int c=1;
initwindow(500,400);
for (int R=50;R<200;R=R+15){
Mcircle(R,250,200);
totron(250,200,R,c);
c++;}
system("PAUSE");
return EXIT_SUCCESS;
}
Demo:
3:
III. Bu iổ th c hành
ự
1.Thu t toán xén hình:
ậ
Code:
#include
#include
#include
//Kieu cua so va bien w toan cuc
struct wind
{
float l,t,r,b;
}w;
// Kieu diem
struct ptype
{
float x,y;
};
// Kieu ma
struct code
{
int l,t,r,b;
};
// Ham ma hoa diem p sang ma c
struct code Encode(struct ptype p)
{
struct code c;
c.l=p.x < w.l;
c.t=p.y < w.t;
c.r=p.x > w.r;
c.b=p.y > w.b;
return c;
};
//Ham kiem tra p co thuoc w hay ko
int InW(struct ptype p)
{
struct code c;
c= Encode(p);
return !(c.l||c.t||c.r||c.b);
}
// Thuat toan
int clip(ptype &p1, ptype &p2)
{
ptype tmpp;
code c1,c2,tmpc;
int in1,in2;
float m;
while(1)
{
c1= Encode(p1);
c2= Encode(p2);
in1=InW(p1);
in2=InW(p2);
if(in1 && in2){return 1;}
if((c1.l && c2.l)||(c1.t && c2.t)||(c1.r && c2.r)||(c1.b && c2.b)) return 0;
if(in1)
{
tmpp =p1; p1=p2; p2=tmpp;
tmpc=c1; c1=c2; c2=tmpc;
}
if(p1.x == p2.x)//doan thang dung
{
if(c1.t) p1.y = w.t;
else p1.y = w.b;
}
else
{
m=(p2.y - p1.y)/(p2.x - p1.x);
if(c1.l)
{
p1.y += m*(w.l - p1.x);
p1.x =w.l;
}
else if(c1.t)
{
p1.x +=(w.t - p1.y)/m;
p1.y = w.t;
}
else if(c1.r)
{
p1.y -=m*(w.r -p1.x);
p1.x = w.r;
}
else
{
p1.x -=(p1.y-w.b)/m;
p1.y = w.b;
}
}
}
}
main()
{
int gd=0,gm;
struct ptype p1,p2;
initgraph(&gd,&gm,"");
w.l=100;
w.t=150;
w.r=300;
w.b=350;
rectangle(int (w.l), int (w.t), int (w.r), int (w.b));
p1.x=90;
p1.y=100;
p2.x=300;
p2.y=400;
line(int (p1.x), int (p1.y), int (p2.x), int (p2.y));
clip(p1,p2);
setcolor(RED);
line(int (p1.x), int (p1.y), int (p2.x), int (p2.y));
getch();
closegraph();
}
Demo:
ư ệ
tìm hi u phái d
2.Th vi n Affine.h:
Th y cho v t
ề ự
ầ
ể ở ướ i-Ph n “Bài t p trên trang ch ”
ủ ầ ậ
ậ
ồ ọ
t ch
x£
£ p 1. Vi
Bài t p trên trang ch :
ủ
I. Khái quát h th ng đ h a:
ệ ố
ươ
ế
ớ p
ng trình v đ th hàm s y=sin(x) v i -
ẽ ồ ị
ố
a. V b ng l nh putpixel(x,y,c):
ẽ ằ
ệ
#include
#include
#include
#include
int main(){
float x,y;
initwindow( 800 , 600 , "09T4.no1" );
moveto(100,100);
for (x=-3.14;x<=3.14;x=x+0.001){
y=30*sin(x);
putpixel(int(100+30*x),int(100+y),6);
}
getch();
} Demo:
b. V b ng l nh lineto(x,y):
ệ
ẽ ằ
#include
#include
#include
#include
#include
Demo:
int main(){
float x,y,n=0.01;
initwindow( 800 , 600 , "09T4.no1" );
moveto(3,100);
for (x=-3.14;x<=3.14;x=x+n){
y=sin(x);
delay(5);
lineto(100+30*x,100+30*y);
}
getch();
}
ơ ả :
2. Các thu t toán c b n
ậ
ậ
a. Thu t toán Bresenham:
#include
#include
#include
void veduongthang(int x1,int y1,int x2,int y2,int mau)
{
int x,y,dx,dy,e,et,ekt,c=mau;float m;
dx=x2-x1;
dy=y2-y1;
if (dx==0)
{ outtextxy(0,0,"Truong hop dx==0");
for(x=x1,y=(y1
{ outtextxy(0,0,"Truong hop dy==0");
for(x=(x1
else if (dx<0) veduongthang(x2,y2,x1,y1,mau);
else { m=(float)dy/dx;
if(m==1)
{ outtextxy(0,0,"Truong hop m==1");
for(x=x1,y=y1;x<=x2;x++,y++)
{
putpixel(x,y,c);
}
}
else if(m==-1)
{ outtextxy(0,0,"Truong hop m==-1");
for(x=x1,y=y1;x<=x2;x++,y--)
putpixel(x,y,c); }
else if(m<-1)
{ outtextxy(0,0,"Truong hop m<-1");
ekt=dx+dx;
e=ekt+dy;
et=e+dy;
for (x=x2,y=y2;y<=y1;y++)
{
putpixel(x,y,c);
if (e<0) e+=ekt;
else
{
x--;
e+=et;
}
}
}
else if((m>-1)&&(m<0))
{ outtextxy(0,0,"Truong hop -1
ekt=-dy-dy;
e=ekt-dx;
et=e-dx; for (x=x1,y=y1;x<=x2;x++) {
putpixel(x,y,c);
if (e<0) e+=ekt;
else
{
y--;
e+=et;
} } }
else if (m>1)
{
outtextxy(0,0,"Truong hop m>1");
e=2*dx-dy;
et=2*dx-2*dy;
ekt=2*dx;
for(x=x1,y=y1;y<=y2;y++)
{
putpixel(x,y,c);
if(e<0) e+=ekt;
else {
x++;
e+=et;
}
} }
else {
outtextxy(0,0,"Truong hop 0
{
putpixel(x,y,c);
if(e<0) e+=ekt;
else
{
y++;
e+=et;
}
}
}
}
}
int main()
{
initwindow(800,600,"WinBGIm");
int x1,y1,x2,y2,mau;
printf("Nhap vao toa do x1:");
scanf("%d",&x1);
printf("Nhap vao toa do y1:");
scanf("%d",&y1);
printf("Nhap vao toa do x2:");
scanf("%d",&x2);
printf("Nhap vao toa so y2:");
scanf("%d",&y2);
mau=5;
setcolor(mau);
veduongthang(x1,y1,x2,y2,mau);
mau=3;
setcolor(mau);
delay(3000);
line(x1,y1,x2,y2);
getch();
closegraph();
}
b. Thu t toán Michener:
ậ
#include
#include
#include
#include
#include
void veduongtron(int x,int y,int R,int mau);
int main()
{
initwindow(800,600,"WinBGIm");
int x,y,R,mau;
printf("Nhap vao toa do cua x ");
scanf("%d",&x);
printf("Nhap vao toa do cua y ");
scanf("%d",&y);
printf("Nhap vao ban kinh ");
scanf("%d",&R);
int gd=DETECT,gm=0;
initgraph(&gd,&gm,"C:\\TC\\BGI");
mau=6;
setcolor(mau);
veduongtron(x,y,R,mau);
delay(3000);
mau=4;
setcolor(mau);
circle(x,y,R);
getch();
closegraph();
}
void veduongtron(int x,int y,int R,int mau)
{int a,b,d;
putpixel(x,y,15);
d=3-2*R;
for(a=0,b=R;a<=b;a++)
{putpixel(x+a,y+b,mau);
putpixel(x-a,y+b,mau);
putpixel(x+a,y-b,mau);
putpixel(x-a,y-b,mau);
putpixel(x+b,y+a,mau);
putpixel(x-b,y+a,mau);
putpixel(x-b,y-a,mau);
putpixel(x+b,y-a,mau);
if(d<0) d+=4*a+6;
else {
d+=4*(a-b)+10;
b--;
}
} }
3. Hình h c Fractal
ọ
a. Đ ng cong Knock:
ườ
void K(int n,float l, float d){
b. Đ ng cong C:
if(n>0) {
K(n-1,l/3,d);
d+=60;
K(n-1,l/3,d);
d-=120;
d+=60;
K(n-1,l/3,d);
}
else linerel(int(l*cos(d*RADS)),int(l*sin(d*RADS)));
}
ườ
c. Đ ng cong R ng:
#define FACT 0.7071
void C(int n,float l,float d){
if (n>0){
d+=45;
C(n-1,l*FACT,d);
d-=90;
C(n-1,l*FACT,d);
d+=45;
}
else linerel(int(l*cos(d*RADS)),int(l*sin(d*RADS)));
}
ườ
ồ
#define FACT 0.7071
void Dragon(int n,float l,int d,int S){
if(n>0){
d+=45*S;
Dragon(n-1,l*FACT,d,1);
d-=90*S;
Dragon(n-1,l*FACT,d,-1);
}
else {setcolor(rand()
%5+13);linerel(int(l*cos(d*RADS)),int(l*sin(d*RADS))); }
} Demo:
ề
4. Phép bi n đ i 2 chi u
ế ổ
a. Th vi n Affine:
ư ệ
#include
typedef float Point[2];
typedef float Affine[3][3];
void Change(Point A,Affine &B){
B[0][0]=A[0]; B[0][1]=A[1]; B[0][2]=1;
}
void Cover(Affine &A,Affine B){
A[0][0]=B[0][0]; A[0][1]=B[0][1];
}
void MatMul(Affine A,Affine B,Affine &C,int m,int n){
int i,j,k;
for(i=0;i
void Quay(Affine &T,float fi){
T[0][0]=cos(fi); T[0][1]=sin(fi); T[0][2]=0.0;
T[1][0]=-sin(fi); T[1][1]=cos(fi); T[1][2]=0.0;
T[2][0]=0.0; T[2][1]=0.0; T[2][2]=1.0;
}
void Tinhtien(Affine &T,float x,float y){
T[0][0]=1; T[0][1]=0; T[0][2]=0;
T[1][0]=0; T[1][1]=1; T[1][2]=0;
T[2][0]=x; T[2][1]=y; T[2][2]=1;
}
void Nghieng(Affine &T,float h,float g){
T[0][0]=1; T[0][1]=g; T[0][2]=0;
T[1][0]=h; T[1][1]=1; T[1][2]=0;
T[2][0]=0; T[2][1]=0; T[2][2]=1;
}
void Codan(Affine &T,float Sx,float Sy){
T[0][0]=Sx; T[0][1]=0; T[0][2]=0;
T[1][0]=0; T[1][1]=Sy; T[1][2]=0;
T[2][0]=0; T[2][1]=0; T[2][2]=1;
}
void Tich(Affine A,Affine B,Affine &C){
Affine Tr1,Tr;
Tr1[0][0]=A[2][0]; Tr1[0][1]=A[2][1]; Tr1[0][2]=1;
MatMul(A,B,C,2,2);
MatMul(Tr1,B,Tr,1,2);
C[2][0]=Tr[0][0]+B[2][0];C[2][1]=Tr[0][1]+B[2][1];
}
void QuayTamO(Affine &T,float fi,float x,float y){
Affine T1,T2,Q,tam;
tam[0][2]=0;
Tinhtien(T1,-x,-y);
Tinhtien(T2,x,y);
Quay(Q,fi);
Tich(T1,Q,tam);
Tich(tam,T2,T);
}
b. V qu t b ng Affine:
ẽ ạ ằ
#include
#include
#include
#include
#include "affine.h"
#define RAD 0.01745329
int x,y,goc=0;
int tm1,tm2,tm3,tm4,c[6];
Affine A1,B1,C1,D1;
void Vemayquat(int x,int y,int mau1,int mau2)
{
//Ve may quat
setcolor(mau1);
rectangle(x,y,x+140,y-20);
setfillstyle(9,1);
floodfill(x+1,y-1,mau1);
rectangle(x+50,y-20,x+90,y-80);
setfillstyle(1,3);
floodfill(x+52,y-21,mau1);
rectangle(x+65,y-80,x+75,y-180);
setfillstyle(4,7);
floodfill(x+66,y-81,mau1);
rectangle(x+68,y-26,x+72,y-32);
setfillstyle(1,15);
floodfill(x+69,y-27,mau1);
rectangle(x+68,y-39,x+72,y-44);
setfillstyle(1,1);
floodfill(x+69,y-40,mau1);
rectangle(x+68,y-51,x+72,y-57);
setfillstyle(1,14);
floodfill(x+69,y-52,mau1);
rectangle(x+68,y-64,x+72,y-70);
setfillstyle(1,4);
floodfill(x+69,y-65,mau1);
//Khung tron quat
circle(x+70,y-260,80);
circle(x+70,y-260,79);
}
void Vecanhquat(int x,int y,int mau1,int mau2)
{
//Code ve canh quat dang test
setcolor(mau1);
c[0]=x+70; c[1]=y-260;
B1[0][0]=x+70; B1[0][1]=y-190; B1[0][2]=1;
//Canh quat thu nhat
QuayTamO(A1,goc*RAD,c[0],c[1]);
MatMul(B1,A1,C1,1,3);
line(c[0],c[1],C1[0][0],C1[0][1]);
QuayTamO(A1,(goc+30)*RAD,c[0],c[1]);
MatMul(B1,A1,D1,1,3);
line(c[0],c[1],D1[0][0],D1[0][1]);
line(C1[0][0],C1[0][1],D1[0][0],D1[0][1]);
c[2]=C1[0][0]; c[3]=C1[0][1];
c[4]=D1[0][0]; c[5]=D1[0][1];
setfillstyle(6,mau2);
fillpoly(3,c);
//Canh quat thu hai
QuayTamO(A1,(goc+120)*RAD,c[0],c[1]);
MatMul(B1,A1,C1,1,3);
line(c[0],c[1],C1[0][0],C1[0][1]);
QuayTamO(A1,(goc+150)*RAD,c[0],c[1]);
MatMul(B1,A1,D1,1,3);
line(c[0],c[1],D1[0][0],D1[0][1]);
line(C1[0][0],C1[0][1],D1[0][0],D1[0][1]);
c[2]=C1[0][0]; c[3]=C1[0][1];
c[4]=D1[0][0]; c[5]=D1[0][1];
setfillstyle(6,mau2);
fillpoly(3,c);
//Canh quat thu ba
QuayTamO(A1,(goc+240)*RAD,c[0],c[1]);
MatMul(B1,A1,C1,1,3);
line(c[0],c[1],C1[0][0],C1[0][1]);
QuayTamO(A1,(goc+270)*RAD,c[0],c[1]);
MatMul(B1,A1,D1,1,3);
line(c[0],c[1],D1[0][0],D1[0][1]);
line(C1[0][0],C1[0][1],D1[0][0],D1[0][1]);
c[2]=C1[0][0]; c[3]=C1[0][1];
c[4]=D1[0][0]; c[5]=D1[0][1];
setfillstyle(6,mau2);
fillpoly(3,c);
}
int main()
{
initwindow(980,480,"OTO");
outtextxy(10,360,"THUC HANH DO HOA VE MAY QUAT");
line(0,350,getmaxx(),350);
x=getmaxx()/2-50;
y=getmaxy()/2+110;
Vemayquat(x,y,GREEN,14); while(!kbhit()){
Vecanhquat(x,y,GREEN,14);
delay(50);
Vecanhquat(x,y,BLACK,BLACK);
goc+=20;
}
getch();
closegraph();
return 0;
}
Demo:
{ for (int j=yc-R; j
} }
int main(int argc, char *argv[]) { int c=1; initwindow(500,400); for (int R=50;R<200;R=R+15){
Mcircle(R,250,200); totron(250,200,R,c); c++;} system("PAUSE"); return EXIT_SUCCESS; } Demo:
3:
III. Bu iổ th c hành
ự
1.Thu t toán xén hình:
ậ
Code:
#include
#include
#include
//Kieu cua so va bien w toan cuc
struct wind
{
float l,t,r,b;
}w;
// Kieu diem
struct ptype
{ float x,y; }; // Kieu ma struct code { int l,t,r,b; }; // Ham ma hoa diem p sang ma c struct code Encode(struct ptype p) { struct code c; c.l=p.x < w.l; c.t=p.y < w.t; c.r=p.x > w.r; c.b=p.y > w.b; return c; }; //Ham kiem tra p co thuoc w hay ko int InW(struct ptype p) { struct code c; c= Encode(p); return !(c.l||c.t||c.r||c.b); } // Thuat toan int clip(ptype &p1, ptype &p2) { ptype tmpp; code c1,c2,tmpc; int in1,in2; float m; while(1) { c1= Encode(p1); c2= Encode(p2); in1=InW(p1); in2=InW(p2); if(in1 && in2){return 1;} if((c1.l && c2.l)||(c1.t && c2.t)||(c1.r && c2.r)||(c1.b && c2.b)) return 0; if(in1) { tmpp =p1; p1=p2; p2=tmpp; tmpc=c1; c1=c2; c2=tmpc; }
if(p1.x == p2.x)//doan thang dung { if(c1.t) p1.y = w.t; else p1.y = w.b; } else { m=(p2.y - p1.y)/(p2.x - p1.x); if(c1.l) { p1.y += m*(w.l - p1.x); p1.x =w.l; } else if(c1.t) { p1.x +=(w.t - p1.y)/m; p1.y = w.t; } else if(c1.r) { p1.y -=m*(w.r -p1.x); p1.x = w.r; } else { p1.x -=(p1.y-w.b)/m; p1.y = w.b; } } } } main() { int gd=0,gm; struct ptype p1,p2; initgraph(&gd,&gm,""); w.l=100; w.t=150; w.r=300; w.b=350; rectangle(int (w.l), int (w.t), int (w.r), int (w.b)); p1.x=90; p1.y=100; p2.x=300; p2.y=400; line(int (p1.x), int (p1.y), int (p2.x), int (p2.y));
clip(p1,p2); setcolor(RED); line(int (p1.x), int (p1.y), int (p2.x), int (p2.y)); getch(); closegraph(); }
Demo:
ư ệ
tìm hi u phái d
2.Th vi n Affine.h: Th y cho v t ề ự ầ
ể ở ướ i-Ph n “Bài t p trên trang ch ” ủ ầ ậ
ậ
ồ ọ
t ch
x£
£ p 1. Vi
Bài t p trên trang ch : ủ I. Khái quát h th ng đ h a: ệ ố ươ
ế
ớ p ng trình v đ th hàm s y=sin(x) v i -
ẽ ồ ị
ố
a. V b ng l nh putpixel(x,y,c):
ẽ ằ
ệ
#include
int main(){ float x,y; initwindow( 800 , 600 , "09T4.no1" ); moveto(100,100); for (x=-3.14;x<=3.14;x=x+0.001){ y=30*sin(x); putpixel(int(100+30*x),int(100+y),6); } getch(); } Demo:
b. V b ng l nh lineto(x,y): ệ
ẽ ằ
#include
Demo:
int main(){ float x,y,n=0.01; initwindow( 800 , 600 , "09T4.no1" ); moveto(3,100); for (x=-3.14;x<=3.14;x=x+n){ y=sin(x); delay(5); lineto(100+30*x,100+30*y); } getch(); }
ơ ả : 2. Các thu t toán c b n
ậ
ậ
a. Thu t toán Bresenham:
#include
#include
#include
void veduongthang(int x1,int y1,int x2,int y2,int mau)
{
int x,y,dx,dy,e,et,ekt,c=mau;float m;
dx=x2-x1;
dy=y2-y1;
if (dx==0)
{ outtextxy(0,0,"Truong hop dx==0");
for(x=x1,y=(y1 { outtextxy(0,0,"Truong hop dy==0");
for(x=(x1 else if (dx<0) veduongthang(x2,y2,x1,y1,mau);
else { m=(float)dy/dx; if(m==1)
{ outtextxy(0,0,"Truong hop m==1"); for(x=x1,y=y1;x<=x2;x++,y++)
{
putpixel(x,y,c);
} }
else if(m==-1)
{ outtextxy(0,0,"Truong hop m==-1"); for(x=x1,y=y1;x<=x2;x++,y--)
putpixel(x,y,c); } else if(m<-1)
{ outtextxy(0,0,"Truong hop m<-1"); ekt=dx+dx;
e=ekt+dy;
et=e+dy;
for (x=x2,y=y2;y<=y1;y++)
{
putpixel(x,y,c);
if (e<0) e+=ekt;
else {
x--;
e+=et;
} }
} else if((m>-1)&&(m<0))
{ outtextxy(0,0,"Truong hop -1 ekt=-dy-dy;
e=ekt-dx;
et=e-dx; for (x=x1,y=y1;x<=x2;x++) { putpixel(x,y,c);
if (e<0) e+=ekt;
else
{
y--;
e+=et;
} } } else if (m>1)
{
outtextxy(0,0,"Truong hop m>1");
e=2*dx-dy;
et=2*dx-2*dy;
ekt=2*dx;
for(x=x1,y=y1;y<=y2;y++) {
putpixel(x,y,c);
if(e<0) e+=ekt;
else {
x++;
e+=et;
}
} } else {
outtextxy(0,0,"Truong hop 0 {
putpixel(x,y,c);
if(e<0) e+=ekt;
else
{
y++;
e+=et;
}
} }
} }
int main()
{
initwindow(800,600,"WinBGIm");
int x1,y1,x2,y2,mau;
printf("Nhap vao toa do x1:");
scanf("%d",&x1);
printf("Nhap vao toa do y1:");
scanf("%d",&y1);
printf("Nhap vao toa do x2:");
scanf("%d",&x2);
printf("Nhap vao toa so y2:");
scanf("%d",&y2);
mau=5;
setcolor(mau);
veduongthang(x1,y1,x2,y2,mau); mau=3;
setcolor(mau);
delay(3000);
line(x1,y1,x2,y2); getch();
closegraph();
} #include initwindow(800,600,"WinBGIm");
int x,y,R,mau; printf("Nhap vao toa do cua x ");
scanf("%d",&x);
printf("Nhap vao toa do cua y ");
scanf("%d",&y);
printf("Nhap vao ban kinh ");
scanf("%d",&R); int gd=DETECT,gm=0;
initgraph(&gd,&gm,"C:\\TC\\BGI");
mau=6;
setcolor(mau);
veduongtron(x,y,R,mau); delay(3000);
mau=4;
setcolor(mau);
circle(x,y,R);
getch();
closegraph();
}
void veduongtron(int x,int y,int R,int mau)
{int a,b,d; putpixel(x,y,15);
d=3-2*R;
for(a=0,b=R;a<=b;a++) {putpixel(x+a,y+b,mau);
putpixel(x-a,y+b,mau);
putpixel(x+a,y-b,mau);
putpixel(x-a,y-b,mau);
putpixel(x+b,y+a,mau);
putpixel(x-b,y+a,mau);
putpixel(x-b,y-a,mau);
putpixel(x+b,y-a,mau);
if(d<0) d+=4*a+6;
else { d+=4*(a-b)+10;
b--; }
} } void K(int n,float l, float d){ if(n>0) {
K(n-1,l/3,d);
d+=60;
K(n-1,l/3,d);
d-=120;
d+=60;
K(n-1,l/3,d);
}
else linerel(int(l*cos(d*RADS)),int(l*sin(d*RADS)));
}
ườ #define FACT 0.7071
void C(int n,float l,float d){
if (n>0){
d+=45;
C(n-1,l*FACT,d);
d-=90;
C(n-1,l*FACT,d);
d+=45;
}
else linerel(int(l*cos(d*RADS)),int(l*sin(d*RADS)));
}
ườ #define FACT 0.7071
void Dragon(int n,float l,int d,int S){
if(n>0){
d+=45*S;
Dragon(n-1,l*FACT,d,1);
d-=90*S;
Dragon(n-1,l*FACT,d,-1);
}
else {setcolor(rand()
%5+13);linerel(int(l*cos(d*RADS)),int(l*sin(d*RADS))); }
} Demo: void Change(Point A,Affine &B){
B[0][0]=A[0]; B[0][1]=A[1]; B[0][2]=1;
} void Cover(Affine &A,Affine B){
A[0][0]=B[0][0]; A[0][1]=B[0][1];
}
void MatMul(Affine A,Affine B,Affine &C,int m,int n){
int i,j,k;
for(i=0;i void Quay(Affine &T,float fi){
T[0][0]=cos(fi); T[0][1]=sin(fi); T[0][2]=0.0;
T[1][0]=-sin(fi); T[1][1]=cos(fi); T[1][2]=0.0;
T[2][0]=0.0; T[2][1]=0.0; T[2][2]=1.0;
} void Tinhtien(Affine &T,float x,float y){
T[0][0]=1; T[0][1]=0; T[0][2]=0;
T[1][0]=0; T[1][1]=1; T[1][2]=0;
T[2][0]=x; T[2][1]=y; T[2][2]=1;
} void Nghieng(Affine &T,float h,float g){
T[0][0]=1; T[0][1]=g; T[0][2]=0;
T[1][0]=h; T[1][1]=1; T[1][2]=0;
T[2][0]=0; T[2][1]=0; T[2][2]=1;
} void Codan(Affine &T,float Sx,float Sy){
T[0][0]=Sx; T[0][1]=0; T[0][2]=0;
T[1][0]=0; T[1][1]=Sy; T[1][2]=0; T[2][0]=0; T[2][1]=0; T[2][2]=1;
} void Tich(Affine A,Affine B,Affine &C){
Affine Tr1,Tr;
Tr1[0][0]=A[2][0]; Tr1[0][1]=A[2][1]; Tr1[0][2]=1;
MatMul(A,B,C,2,2);
MatMul(Tr1,B,Tr,1,2);
C[2][0]=Tr[0][0]+B[2][0];C[2][1]=Tr[0][1]+B[2][1];
} void QuayTamO(Affine &T,float fi,float x,float y){
Affine T1,T2,Q,tam;
tam[0][2]=0;
Tinhtien(T1,-x,-y);
Tinhtien(T2,x,y);
Quay(Q,fi);
Tich(T1,Q,tam);
Tich(tam,T2,T);
} floodfill(x+69,y-27,mau1);
rectangle(x+68,y-39,x+72,y-44);
setfillstyle(1,1);
floodfill(x+69,y-40,mau1);
rectangle(x+68,y-51,x+72,y-57);
setfillstyle(1,14);
floodfill(x+69,y-52,mau1);
rectangle(x+68,y-64,x+72,y-70);
setfillstyle(1,4);
floodfill(x+69,y-65,mau1);
//Khung tron quat
circle(x+70,y-260,80);
circle(x+70,y-260,79);
}
void Vecanhquat(int x,int y,int mau1,int mau2)
{
//Code ve canh quat dang test
setcolor(mau1);
c[0]=x+70; c[1]=y-260;
B1[0][0]=x+70; B1[0][1]=y-190; B1[0][2]=1;
//Canh quat thu nhat
QuayTamO(A1,goc*RAD,c[0],c[1]);
MatMul(B1,A1,C1,1,3);
line(c[0],c[1],C1[0][0],C1[0][1]);
QuayTamO(A1,(goc+30)*RAD,c[0],c[1]);
MatMul(B1,A1,D1,1,3);
line(c[0],c[1],D1[0][0],D1[0][1]);
line(C1[0][0],C1[0][1],D1[0][0],D1[0][1]);
c[2]=C1[0][0]; c[3]=C1[0][1];
c[4]=D1[0][0]; c[5]=D1[0][1];
setfillstyle(6,mau2);
fillpoly(3,c);
//Canh quat thu hai
QuayTamO(A1,(goc+120)*RAD,c[0],c[1]);
MatMul(B1,A1,C1,1,3);
line(c[0],c[1],C1[0][0],C1[0][1]);
QuayTamO(A1,(goc+150)*RAD,c[0],c[1]);
MatMul(B1,A1,D1,1,3);
line(c[0],c[1],D1[0][0],D1[0][1]);
line(C1[0][0],C1[0][1],D1[0][0],D1[0][1]);
c[2]=C1[0][0]; c[3]=C1[0][1];
c[4]=D1[0][0]; c[5]=D1[0][1];
setfillstyle(6,mau2); fillpoly(3,c);
//Canh quat thu ba
QuayTamO(A1,(goc+240)*RAD,c[0],c[1]);
MatMul(B1,A1,C1,1,3);
line(c[0],c[1],C1[0][0],C1[0][1]);
QuayTamO(A1,(goc+270)*RAD,c[0],c[1]);
MatMul(B1,A1,D1,1,3);
line(c[0],c[1],D1[0][0],D1[0][1]);
line(C1[0][0],C1[0][1],D1[0][0],D1[0][1]);
c[2]=C1[0][0]; c[3]=C1[0][1];
c[4]=D1[0][0]; c[5]=D1[0][1];
setfillstyle(6,mau2);
fillpoly(3,c);
}
int main()
{ initwindow(980,480,"OTO");
outtextxy(10,360,"THUC HANH DO HOA VE MAY QUAT");
line(0,350,getmaxx(),350);
x=getmaxx()/2-50;
y=getmaxy()/2+110; Vemayquat(x,y,GREEN,14); while(!kbhit()){ Vecanhquat(x,y,GREEN,14);
delay(50);
Vecanhquat(x,y,BLACK,BLACK);
goc+=20;
}
getch();
closegraph();
return 0; }
Demo:b. Thu t toán Michener:
ậ
3. Hình h c Fractal
ọ
a. Đ ng cong Knock:
ườ
b. Đ ng cong C:
c. Đ ng cong R ng:
ồ
ề
4. Phép bi n đ i 2 chi u
ế ổ
a. Th vi n Affine:
ư ệ
#include
b. V qu t b ng Affine:
ẽ ạ ằ
#include