Đăng ký Đăng nhập
Trang chủ Công nghệ thông tin Thiết kế - Đồ họa Bài tập đồ họa máy tính (2)...

Tài liệu Bài tập đồ họa máy tính (2)

.PDF
16
412
79

Mô tả:

Bài tập đồ họa máy tính (2)
1.hình vuông! #include #include #include using namespace std; void MPCircle(int xc,int yc, int R){ int x,y,c,p; c=getcolor(); p=1-R; x=0; putpixel(xc,yc,8); for(y=R;x<=y;x++){ 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); if(p<0) p+=2*x+3; else{ p+=2*(x-y)+5; // y--; bỏ đi } } } int main( ) { initwindow( 640 , 480 , "WinBGIm" ); MPCircle(200,200,100); cin.get(); closegraph( ); return( 0 ); } Câu 2 : Viết chương trình nhập n số hình tròn hoặc hình vuông từ bàn phím . Hãy vẽ đường tròn hoặc hình vuông tại giữa màn hình bằng giải thuật đệ quy. #include #include #include #include #include void Nhap(int &n) { printf("n="); scanf("%d",&n); } void Init() { int mh=DETECT,mode=0; initgraph(&mh,&mode,"D:\\TC\\BGI"); if(graphresult()!=0) exit(1); } // Ve nhieu Hinh vuong dong tam(x,y) va Canh=2R void MultiRectangle(int x,int y,int n,int R) { if(n==0) return; else { setcolor(random(14)+1); rectangle(x-n*R,y-n*R,x+n*R,y+n*R); delay(100); MultiRectangle(x,y,n-1,R); } } // Ve nhieu hinh tron dong tam(x,y) va Ban kinh 2R void MultiCircle(int x,int y,int n,int R) { if(n==0) return; else { setcolor(random(14)+1); circle(x,y,R*n); delay(100); MultiCircle(x,y,n-1,R); } } void main() { int n; int R=20; // Nhap Nhap(n); // Khoi tao do hoa Init(); int x,y; // Lay toa do tam man hinh x=getmaxx()/2; y=getmaxy()/2; // Khoi tao random randomize(); // Ve MultiRectangle(x,y,n,R); getch(); // Dong do hoa closegraph(); } 3.vẽ đường tròn midpoint. #include #include #include #include void Mycircle(int xo,int yo,int R) { int x,y,p,c; setviewport(xo,yo,xo+R,yo+R,0); p=1-R; x=0;y=R;c=getcolor(); while(x<=y) { putpixel(x,y,c);putpixel(-x,y,c); putpixel(x,-y,c);putpixel(-x,-y,c); putpixel(y,x,c);putpixel(y,-x,c); putpixel(-y,x,c);putpixel(-y,-x,c); if(p<0) p+=2*x+3; else {p+=2*(x-y)+5;y--;} x++; } } void main() { int gd=0,gm,R,x,y; initgraph(&gd,&gm,"F:\\learn\\TC\\BGI"); printf("nhap x=");scanf("%d",&x); printf("nhap y=");scanf("%d",&y); printf("nhap R=");scanf("%d",&R); setcolor(3); Mcircle(x,y,R); getch(); closegraph(); } 4.thuật toán Bresenham vẽ đường thẳng: #include #include #include void hoanvi(int *,int *,int *,int *); void Bresenham1(int,int ,int ,int ); void Bresenham2(int,int ,int ,int ); void Bresenham3(int,int ,int ,int ); void Bresenham4(int,int ,int ,int ); void Bresenham5(int,int ,int ,int ); void main() { int gd=0,gm,xa,ya,xb,yb,xmax,ymax; float m; clrscr(); printf("\nnhap toa do A: "); scanf("%d%d",&xa,&ya); printf("\nnhap toa do B:"); scanf("%d%d",&xb,&yb); if((xb-xa)!=0) m=(yb-ya)/float(xb-xa); printf("\nm= %f",m); getch(); initgraph(&gd,&gm,"D:\\TC\\BGI"); setcolor(4); if(xa>xb) hoanvi(&xa,&ya,&xb,&yb); if(m>0&&m<1) Bresenham1(xa,ya,xb,yb); if(m>1) Bresenham2(xa,ya,xb,yb); if(m>-1&&m<0) Bresenham3(xa,ya,xb,yb); if(m<-1) Bresenham4(xa,ya,xb,yb); if(xa==xb||ya==yb||m==1) Bresenham5(xa,ya,xb,yb); getch(); closegraph(); } void hoanvi(int*xa,int *ya,int *xb,int *yb) { int tam; tam=*xa; *xa=*xb; *xb=tam; tam=*ya; *ya=*yb; *yb=tam; } // ********* 01******************* void Bresenham2(int xa,int ya,int xb,int yb) { int x,y,dx,dy,e,ekt,et,c; dx = xb-xa; dy=yb-ya; ekt=dx+dx; e=ekt-dy; et=e-dy; c=getcolor(); for(x=xa,y=ya;x<=xb;y++){ putpixel(x,y,c); if(e<0) e+=ekt; else { x++; e+=et; } } outtextxy(200,200,"truonghop2"); } //************* -1=xb;y--){ putpixel(x,y,c); if(e<0) e+=ekt; else { x++; e+=et; } } outtextxy(200,200,"truonghop4"); } // *********** TH khac **************** void Bresenham5(int xa,int ya,int xb,int yb) { int x,y,c; c=getcolor(); if(xa==xb){ for(y=ya;y<=yb;y++) putpixel(xa,y,c); } if(ya==yb){ for(x=xa;x<=xb;x++) putpixel(x,ya,c); } if((xb-xa)/(yb-ya)==1) { for(x=xa,y=ya;x<=xb;x++,y++) putpixel(x,y,c); } outtextxy(200,200,"truonghop5"); } 5.vẽ đường thẳng = Bresenham: //ve duong thang #include #include #include void Bresline(int xa,int ya,int xb,int yb,int c){ int dx,dy,edy,e,esi,x,y; dx=xb-xa; dy=yb-ya; edy=dy+dy;//2*delta(y) e=edy-dx;//e1 esi=e-dx;//2*delta(y)-2*delta(x) x=xa; y=ya; while(x<=xb){ putpixel(x,y,c); if(e<0) e+=edy; else { e+=esi; y++; } x++; } } void main(){ int gd=0,gm=0,xa,ya,xb,yb,c; printf("Nhap toa do diem A: \n"); printf("Xa= ");scanf("%d",&xa); printf("\nYa= ");scanf("%d",&ya); printf("\nNhap toa do diem B: \n"); printf("Xa= ");scanf("%d",&xb); printf("\nYa= ");scanf("%d",&yb); printf("\n Nhap mau: ");scanf("%d",&c); initgraph(&gd,&gm,"F:\\learn\\tc\\bgi"); Bresline(xa,ya,xb,yb,c); line(xa+5,ya,xb+5,yb); getch(); closegraph(); } Câu 2: Viết hàm vẽ đường tròn tâm I(x,y), bán kính R. Đây là code bài vẽ đường tròn bằng thuật toán MICHENER ,cho phép nhập toạ độ tâm và bán kính,sao đó so sánh với đường tròn được vẽ bằng hàm có sẵn trong đồ hoạ. #include #include #include #include void InitGraph() { int gd=DETECT,gm,err; initgraph(&gd,&gm,"C:\\TC\\BGI"); } void Michener(int xc,int yc,int R,int c) { int x,y,d; d=3-2*R; for(x=0,y=R;x<=y;x++) { putpixel(xc+x,yc+y,c); putpixel(xc-x,yc+y,c); putpixel(xc+x,yc-y,c); putpixel(xc-x,yc-y,c); putpixel(xc+y,yc+x,c); putpixel(xc-y,yc+x,c); putpixel(xc+y,yc-x,c); putpixel(xc-y,yc-x,c); if(d<0) d+=4*x+6; else { d+=4*x-4*y+10; y--; } } } void main() { int xo,yo,R; InitGraph(); printf("nhap toa do tam duong tron xO:\t"); scanf("%d",&xo); printf("nhap toa do tam duong tron yO:\t"); scanf("%d",&yo); printf("nhap ban kinh duong tron:"); scanf("%d",&R); settextstyle(2,HORIZ_DIR,4); outtextxy(xo+R+10,yo+R+10,"hinh tron tam O ban kinh R"); Michener(xo,yo,R,1); getch(); cleardevice(); //dung ham co san do hoa de so sanh setcolor(12); circle(xo,yo,R); outtextxy(xo+R+10,yo+R+10,"hinh tron tam O ban kinh R ve bang ham co san"); getch(); closegraph(); } Câu 1: Dùng thuật toán Breseham để vẽ đoạn thẳng nối 2 điểm A(3,7); B(6,17). Giải: Ta có: Δx = 6 - 3 = 3; Δy = 17 - 7 = 10; Trường hợp này hệ số góc m>1. Ta đổi vai trò của x,y. e1 = 2Δx - Δy = -4; ekt = 2Δx = 6; et = 2Δx - 2Δy = -14; Áp dụng: y 7 8 9 10 11 12 13 14 15 16 17 x33444555566 e -4 2 -12 -6 0 -14 -8 -2 4 -10 -4 Code thi giữa kỳ đồ họa máy tính #include #include #include #include #include void Nhap(int &n) { printf("n="); scanf("%d",&n); } void Init() { int mh=DETECT,mode=0; initgraph(&mh,&mode,"D:\\TC\\BGI"); if(graphresult()!=0) exit(1); } // Ve nhieu Hinh vuong dong tam(x,y) va Canh=2R void MultiRectangle(int x,int y,int n,int R) { if(n==0) return; else { setcolor(random(14)+1); rectangle(x-n*R,y-n*R,x+n*R,y+n*R); delay(100); MultiRectangle(x,y,n-1,R); } } // Ve nhieu hinh tron dong tam(x,y) va Ban kinh 2R void MultiCircle(int x,int y,int n,int R) { if(n==0) return; else { setcolor(random(14)+1); circle(x,y,R*n); delay(100); MultiCircle(x,y,n-1,R); } } void main() { int n; int R=20; // Nhap Nhap(n); // Khoi tao do hoa Init(); int x,y; // Lay toa do tam man hinh x=getmaxx()/2; y=getmaxy()/2; // Khoi tao random randomize(); // Ve MultiRectangle(x,y,n,R); getch(); // Dong do hoa closegraph(); } Code sin cos tan Code này có thể vẽ ở bất kỳ đâu ko như code của mấy bác kia khó áp dụng #include #include #include #include #include #include int maxX, maxY; void initGraph() { int gd = DETECT, gm; initgraph(&gd, &gm, "..\\BGI"); maxX = getmaxx(); maxY = getmaxy(); } // sx,sy dx,dy toa do hinh chu nhat ma do thi se ve trong d o void paint_Sin_Graph(int sx, int sy, int dx, int dy, int ch uky = 1, int color = WHITE) { int i, xpos, ypos, yconst, range = (dy - sy) / 2; // range tan so khuyeck dai tuong ung voi nua chieu doc float angle = 0, speed; xpos = sx; yconst = sy + ((dy - sy) / 2); //lay tao do diem giua cua khung speed = ((2 * M_PI) / (dx - sx)) * chuky; //ve 1 chuky 2pi tuong ung voi chieu ngang, n chu ky th i nhan them cho so chu ky // ve truc toa do setcolor(RED); line(sx, sy, sx, dy); line(xpos, yconst, dx, yconst);; outtextxy(sx - 15, sy, "1"); outtextxy(sx - 15, dy, "-1"); outtextxy(xpos - 15, yconst, "0"); setcolor(color); for (xpos = sx; xpos < dx; xpos++) { angle += speed; ypos = yconst + (sin( angle)) *range; putpixel(xpos, ypos, color); delay(10); } settextjustify(1, 1); //nham dua vi tri dong chu vao giua outtextxy(sx + (dx sx) / 2, dy + 2, "do thi ham sin"); } // sx,sy dx,dy toa do hinh chu nhat ma do thi se ve trong d o void paint_Cos_Graph(int sx, int sy, int dx, int dy, int ch uky = 1, int color = WHITE) { int i, xpos, ypos, yconst, range = (dy - sy) / 2; // range tan so khuyeck dai tuong ung voi nua chieu doc float angle = 0, speed; xpos = sx; yconst = sy + ((dy - sy) / 2); //lay tao do diem giua cua khung speed = ((2 * M_PI) / (dx - sx)) * chuky; //ve 1 chuky 2pi tuong ung voi chieu ngang, n chu ky th i nhan them cho so chu ky // ve truc toa do setcolor(RED); line(sx, sy, sx, dy); line(xpos, yconst, dx, yconst); outtextxy(sx - 15, sy, "1"); outtextxy(sx - 15, dy, "-1"); outtextxy(xpos - 15, yconst, "0"); setcolor(color); for (xpos = sx; xpos < dx; xpos++) { angle += speed; ypos = yconst + (cos( angle)) *range; putpixel(xpos, ypos, color); delay(10); } settextjustify(1, 1); //nham dua vi tri dong chu vao giua outtextxy(sx + (dx sx) / 2, dy + 2, "do thi ham cos"); } // sx,sy dx,dy toa do hinh chu nhat ma do thi se ve trong d o void paint_Tan_Graph(int sx, int sy, int dx, int dy, int ch uky = 1, int color = WHITE) { int i, xpos, ypos, yconst, range = (dy - sy) / 10; // range tan so khuyeck dai do tan chay tu -votan>+votan nen tan so khuyech dai cang nho cang tot float angle = 0, speed; xpos = sx; yconst = sy + ((dy - sy) / 2); //lay tao do diem giua cua khung speed = ((2 * M_PI) / (dx - sx)) * chuky; //ve 1 chuky 2pi tuong ung voi chieu ngang, n chu ky thi nhan them cho so chu ky // ve truc toa do setcolor(RED); line(sx, sy, sx, dy); line(xpos, yconst, dx, yconst); outtextxy(xpos - 15, yconst, "0"); setcolor(color); for (xpos = sx; xpos < dx; xpos++) { angle += speed; ypos = yconst + (tan( angle)) *range; if (ypos > dy || ypos < sx) continue; putpixel(xpos, ypos, color); delay(10); } settextjustify(1, 1); //nham dua vi tri dong chu vao giua outtextxy(sx + (dx - sx) / 2, dy, "do thi ham tan"); } void main() { initGraph(); paint_Sin_Graph(20, 40, maxX, maxY / 2, 4); getch(); cleardevice(); paint_Cos_Graph(20, 20, maxX / 2, maxY - 20, 2); getch(); cleardevice(); paint_Tan_Graph(20, 20, maxX / 2, maxY / 2, 2); getch(); closegraph(); } code hàm sin #include #include #include //============= void main(){ int gd=DETECT, gm,x,y; initgraph(&gd,&gm,"..\\BGI"); //ve bang tung diem outtextxy(10,10,"Ve bang POINT:"); for(x=0;x<=628;x++){// -pi -> pi y=240+(int)(sin((x-314.0)/100.0)*200.0); putpixel(x,y,15); } getch(); cleardevice(); //ve bang doan thang outtextxy(10,10,"Ve bang LINE:"); x=0; y=240+(int)(sin((x-314.0)/100.0)*200.0); moveto(x,y); for(x=1;x<=628;x++){// -pi -> pi y=240+(int)(sin((x-314.0)/100.0)*200.0); lineto(x,y); } getch(); closegraph(); } code hàm sin #include #include #include #include #include void InitGraph(){ int gd=DETECT,gm,err; initgraph(&gd,&gm,"\\BorlandC\\BGI"); err=graphresult(); if(err!=grOk){ printf("Error: %s",grapherrormsg(err)); getch(); exit(1); } } /* Man hinh co do phan giai toi da thuong la 480x640. Ve ham sin trong khoang tu -Pi -> Pi. Neu thich ve nhiu cung sin hon thi sua lai ck = so cung : ) */ void Sin_By_PutPixel(int c){ int i,j,x,y,ck; ck=1; // chu ki la 1 j=480/(2*ck); for(i=0;i<640;i++){ x=i; y= j + sin(x*M_PI/(320/ck))*j; putpixel(x,y,c); } } void Sin_By_LineTo(){ int i,j,x,y,ck,range; range=10;// Ve voi do dai la 10 ck=1; // chu ki la 1 j=480/(2*ck); moveto(0,j);// Dua con tro man hinh den vi tri bat da u ve for(i=0;i<640/range;i++){ x=i*range; y= j + sin(x*M_PI/(320/ck))*j; lineto(x,y); } } void main(){ InitGraph(); outtextxy(1,1,"Dung Ham PutPixel() De Ve Ham Sin"); Sin_By_PutPixel(GREEN); getch(); cleardevice(); outtextxy(1,1,"Dung Ham LineTo() De Ve Ham Sin"); Sin_By_LineTo(); getch(); closegraph(); }
- Xem thêm -

Tài liệu liên quan