Đăng ký Đăng nhập
Trang chủ Báo cáo thực hành đồ họa máy tính...

Tài liệu Báo cáo thực hành đồ họa máy tính

.DOC
19
729
126

Mô tả:

I. Buổi thực hành 1: 1. Cài đặt và sử dụng thư viện graphics.h: 1.1. Download 2 file sau đây về máy: + File graphics.h để ở thư mục C:\Dev-Cpp\include + File libbgi.a để ở thư mục C:\Dev-Cpp\lib 1.2. Khởi động Dev C++, vào File-->New-->Project...-->Empty Project (Nhớ chọn 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 được tạo ra trong Project. Trước khi viết code lưu lại bằng cách 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:
TRƯỜNG ĐẠI HỌC BK ĐÀ NẴNG 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 Sinh viên Lớp Nhóm MSSV : Nguyễn Văn Nguyên : Trịnh Hoàng Long : 11T2 : 11B : 102110212 Đà Nẵng, 10/04/2014 I. Buổi thực hành 1: 1. Cài đặt và sử dụng thư viện graphics.h: 1.1. Download 2 file sau đây về máy: + File graphics.h để ở thư mục C:\Dev-Cpp\include + File libbgi.a để ở thư mục C:\Dev-Cpp\lib 1.2. Khởi động Dev C++, vào File-->New-->Project...-->Empty Project (Nhớ chọn 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 được tạo ra trong Project. Trước khi viết code lưu lại bằng cách 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: II. Buổi thực hành 2: 1.Vẽ các đường tròn đồng tâm và tô màu chúng: Sử dụng thuật toán 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; iR-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: III. Buổi thực hành 3: 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: 2.Thư viện Affine.h: Thầy cho về tự tìm hiểu ở phái dưới-Phần “Bài tập trên trang chủ” Bài tập trên trang chủ: I. Khái quát hệ thống đồ họa: 1. Viết chương trình vẽ đồ thị hàm số y=sin(x) với -x 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 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(); } Demo: 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-1)&&(m<0)) { outtextxy(0,0,"Truong hop -11) { 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 #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){ 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))); } b. Đường cong C: #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))); } c. Đường cong Rồng: #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 #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:
- Xem thêm -

Tài liệu liên quan