Đồ họa máy tính Mô phỏng đồ họa máy tính

18 226 0
Đồ họa máy tính   Mô phỏng đồ họa máy tính

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Code một số các phương pháp vẽ hình trên nền đồ họa C++ hay dev C .Bộ bài tập lập trình cơ bản về đồ họa trong C++ , C .Bài tập lớn về đồ họa máy tính cũng như chi tiết hướng dẫn làm bài tập đồ họa máy tính cho người mới học . Chi tiết liên hệ anh9306gmail.com.

BÀI TẬP THỰC HÀNH ĐỒ HỌA MÁY TÍNH_37DHTIN2 TRANSLATION – 2D #include #include #include #include void main() { int ch; int tx,ty; int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); setcolor(6); printf("\n\n\tDoi tuong: "); rectangle(100,150,150,100); setcolor(2); outtextxy(100,10,"TRANSLATION"); outtextxy(100,20," "); printf("\n\ttx: "); scanf("%d",&tx); printf("\tty: "); scanf("%d",&ty); getch(); cleardevice(); outtextxy(100,10,"TRANSLATION"); outtextxy(100,20," "); rectangle(100,150,150,100); delay(1000); rectangle(100+tx,150+ty,150+tx,100+ty); getch(); closegraph(); } Rotation, Scaling, Translation #include #include #include #include void translate(); void scale(); void rotate(); void main() { int ch; int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); setcolor(6); outtextxy (100,88,"Object."); rectangle(100,150,150,100); printf(" -MENU -"); printf("\n 1)Translate\n 2)Scale\n 3)Rotate"); printf("\nHay nhan chon: "); scanf("%d",&ch); cleardevice(); switch(ch) { case 1: translate(); break; case 2: scale(); break; case 3: rotate(); break; default: printf("Nhap sai lua chon"); break; } getch(); closegraph(); } void translate() { int tx,ty; setcolor(2); outtextxy(240,10,"TRANSLATION"); outtextxy(238,20," "); printf("\nNhap tx: "); scanf("%d",&tx); printf("\nNhap ty: "); scanf("%d",&ty); cleardevice(); rectangle(100,150,150,100); printf("\nAfter Translation"); rectangle(100+tx,150+ty,150+tx,100+ty); } void scale() { int sx,sy; setcolor(2); outtextxy(240,10,"SCALING"); outtextxy(238,20," "); printf("\nNhap sx: "); scanf("%d",&sx); printf("\nNhap sy: "); scanf("%d",&sy); cleardevice(); rectangle(100,150,150,100); printf("\nAfter Scaling"); rectangle(100*sx,150*sy,150*sx,100*sy); } void rotate() { float theta; int x1,x2,x3,x4; int y1,y2,y3,y4; int ax1,ax2,ax3,ax4,ay1,ay2,ay3,ay4; int refx,refy; printf("\nNhap he so theta: "); scanf("%f",&theta); theta=theta*(3.14/180); cleardevice(); setcolor(2); outtextxy(240,10,"ROTATE"); outtextxy(238,20," -"); refx=100; refy=100; x1=100; y1=100; x2=150; y2=100; x3=150; y3=150; x4=100; y4=150; ax1=refy+(x1-refx)*cos(theta)-(y1-refy)*sin(theta); ay1=refy+(x1-refx)*sin(theta)+(y1-refy)*cos(theta); ax2=refy+(x2-refx)*cos(theta)-(y2-refy)*sin(theta); ay2=refy+(x2-refx)*sin(theta)+(y2-refy)*cos(theta); ax3=refy+(x3-refx)*cos(theta)-(y3-refy)*sin(theta); ay3=refy+(x3-refx)*sin(theta)+(y3-refy)*cos(theta); ax4=refy+(x4-refx)*cos(theta)-(y4-refy)*sin(theta); ay4=refy+(x4-refx)*sin(theta)+(y4-refy)*cos(theta); rectangle(100,150,150,100); line(ax1,ay1,ax2,ay2); line(ax2,ay2,ax3,ay3); line(ax3,ay3,ax4,ay4); line(ax4,ay4,ax1,ay1); } CohenSutherlandClipping #include #include #include #include #include #define TRUE #define FALSE typedef unsigned int outcode; outcode CompOutCode(float x,float y); enum { TOP = 0x1, BOTTOM = 0x2, RIGHT = 0x4, LEFT = 0x8 }; float xmin,xmax,ymin,ymax; void clip(float x0,float y0,float x1,float y1) { outcode outcode0,outcode1,outcodeOut; int accept = FALSE,done = FALSE; outcode0 = CompOutCode(x0,y0); outcode1 = CompOutCode(x1,y1); { if(!(outcode0|outcode1)) { accept = TRUE; done = TRUE; } else if(outcode0 & outcode1) done = TRUE; else { float x,y; outcodeOut = outcode0?outcode0:outcode1; if(outcodeOut & TOP) { x = x0+(x1-x0)*(ymax-y0)/(y1-y0); y = ymax; } else if(outcodeOut & BOTTOM) { x = x0+(x1-x0)*(ymin-y0)/(y1-y0); y = ymin; } else if(outcodeOut & RIGHT) { y = y0+(y1-y0)*(xmax-x0)/(x1-x0); x = xmax; } else { y = y0+(y1-y0)*(xmin-x0)/(x1-x0); x = xmin; } if(outcodeOut==outcode0) { x0 = x; y0 = y; outcode0 = CompOutCode(x0,y0); } else { x1 = x; y1 = y; outcode1 = CompOutCode(x1,y1); } } }while(done==FALSE); if(accept) line(x0,y0,x1,y1); outtextxy(150,20,"POLYGON AFTER CLIPPING"); rectangle(xmin,ymin,xmax,ymax); } outcode CompOutCode(float x,float y) { outcode code = 0; if(y>ymax) code|=TOP; else if(yxmax) code|=RIGHT; else if(x

Ngày đăng: 02/12/2015, 23:32

Tài liệu cùng người dùng

Tài liệu liên quan