Thông tin tài liệu
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 : Nguyễn Văn Nguyên Sinh viên : Trịnh Hoàng Long Lớp : 11T2 Nhóm : 11B MSSV : 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 <cstdlib> #include <iostream> #include <graphics.h> 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 <cstdlib> #include <iostream> #include <graphics.h> #include <math.h> 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<xc+R; i++) { for (int j=yc-R; j<yc+R; j++) { float d= sqrt((float)(xc-i)*(xc-i)+ (float)(yc-j)*(yc-j)); if (d<50) putpixel(i,j,11); else if (d<R && d>R-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<stdio.h> #include<graphics.h> #include<dos.h> //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<conio.h> #include<graphics.h> #include<stdio.h> #include<math.h> 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<conio.h> #include<graphics.h> #include<stdio.h> #include<math.h> #include<stdlib.h> 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: . 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 Đà Nẵng, 10/04/2014 I. Buổi thực hành 1: 1 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 <cstdlib> #include. 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<conio.h> #include<graphics.h> #include<stdio.h> #include<math.h> int
Ngày đăng: 13/05/2014, 14:58
Xem thêm: báo cáo thực hành đồ họa máy tính, báo cáo thực hành đồ họa máy tính