Chủ Nhật, 30 tháng 8, 2015

thuật toán xén hình - kỹ thuật đồ họa




 CÁC THUẬT TOÁN XÉN HÌNH
Nhóm lớp cao đẳng tin k10:
1.Nguyễn Đức Dũng
2.Trần Thị Hồng
3.Nguyễn Thị Hoa
4.Đỗ Hoành Lực
5.Nguyễn Văn Minh
6.Phùng Quang Quân
7.Vi Thị Hải Hà
      1.Giới thiệu:
o Thao tác loại bỏ các phần hình ảnh nằm ngoài một vùng cho trước được gọi là xén hình.
o Vùng được dùng để xén hình gọi là cửa sổ xén (clip window).                                                             










+ Cài đặt thuật toán  xén đoạn thẳng Cohen-Sutherland:


#include<stdio.h> #include<graphics.h> #include<conio.h> #include<dos.h> #include<math.h> #include<stdlib.h> typedef int code[4]; int a1,b1,a2,b2; int x,i,xmin,ymin,xmax,ymax,gd,gm; code b; void macode(int x,int y,code b) { for(i=1;i<=4;i++) b[i]=0; if(x<xmin) b[1]=1; if(x>xmax) b[2]=1; if(y<ymin) b[3]=1; if(y>ymax) b[4]=1; } //XAY DUNG HAM XEN THEO THUAT TOAN COHEN SUTHERLAND OUTCODE xen(int x1,int y1,int x2,int y2) { code c1,c2; int chon,tong1,tong2,mu2; int tgx,tgy; do { macode(x1,y1,c1);macode(x2,y2,c2); tong1=0;tong2=0;mu2=1; for(i=1;i<=4;i++) { tong1=tong1+mu2*c1[i]; tong2=tong2+mu2*c2[i]; mu2=mu2*2; } //kiem tra xem xay ra truong hop nao if (tong1+tong2==0)//truong hop dt nam trong HCN { chon=1; setlinestyle(0,0,0); line(x1,y1,x2,y2); } if((tong1 & tong2)!=0)//doan thang can xen nam ve mot phia cua canh HCN xen chon=2; if(((tong1+tong2)!=0) && ((tong1 & tong2)==0)) { chon=3; if(tong1==0)//thay doi vai tro cua diem 1 cho diem 2 { tgx=x1;x1=x2;x2=tgx; tgy=y1;y1=y2;y2=tgy; } macode(x1,y1,b); if(b[1]==1) { y1=y1+(xmin-x1)*(y2-y1)/(x2-x1); x1=xmin; } if(b[2]==1) { y1=y1+(xmax-x1)*(y2-y1)/(x2-x1); x1=xmax; } if(b[3]==1) { x1=x1+(ymin-y1)*(x2-x1)/(y2-y1); y1=ymin; } if(b[4]==1) { x1=x1+(ymax-y1)*(x2-x1)/(y2-y1); y1=ymax; } } }while((chon!=1) && (chon!=2));
return 0; } void main() { clrscr(); gd=DETECT; initgraph(&gd,&gm,"C:\\TC\\BGI"); if(graphresult()!=0) { printf("LOI DO HOA "); getch(); exit(1); } printf("Nhap vao tao do HCN min xmin:ymin "); scanf("%d%d",&xmin,&ymin); printf("Nhap vao toa do HCN max xmax:ymax "); scanf("%d%d",&xmax,&ymax); printf("Nhap vao toa do diem 1 "); scanf("%d%d",&a1,&b1); printf("Nhap vao toa do diem 2 "); scanf("%d%d",&a2,&b2); setbkcolor(BLACK); setcolor(RED); rectangle(xmin,ymin,xmax,ymax); setcolor(WHITE); setlinestyle(1,0,0); line(a1,b1,a2,b2); setlinestyle(0,0,0); xen(a1,b1,a2,b2); getch(); closegraph(); }





Cài đặt thuật toán xén đa giác

#include <stdio.h> #include <graphics.h> #include <conio.h> #include <math.h> #include <process.h> #define TRUE 1 #define FALSE 0 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); do { 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,"hinh sau khi da duoc xen"); rectangle(xmin,ymin,xmax,ymax); } outcode CompOutCode(float x,float y) { outcode code = 0; if(y>ymax) code|=TOP; else if(y<ymin) code|=BOTTOM; if(x>xmax) code|=RIGHT; else if(x<xmin) code|=LEFT; return code; } void main( ) { float x1,y1,x2,y2; /* request auto detection */ int gdriver = DETECT, gmode, n,poly[14],i; clrscr( ); printf("Enter the no of sides of polygon:"); scanf("%d",&n); printf("\nEnter the coordinates of polygon\n"); for(i=0;i<2*n;i++) { scanf("%d",&poly[i]); } poly[2*n]=poly[0]; poly[2*n+1]=poly[1]; printf("Enter the rectangular coordinates of clipping window\n"); scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax); /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); outtextxy(150,20,"hinh da giac truoc khi xen"); drawpoly(n+1,poly); rectangle(xmin,ymin,xmax,ymax); getch( ); cleardevice( ); for(i=0;i<n;i++) clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]); getch( ); restorecrtmode( ); }




3.Đánh giá:
cả 2 kĩ thuật đều có thể thực hiện trong các trường hợp bất kì đoạn thẳng nào hoặc hình đa giác nào.

4.Tài liệu tham khảo:
text.123doc.org

5.Liên kết với trang web:
thanhdo.edu.vn
https://www.facebook.com/thanhdo.university?fref=ts











5 nhận xét:

  1. anh chị ơi cho em hỏi. hình trc khi xén nó hiển thị ui. hình sau khi xen làm sao cho nó hiển thị lên cửa sổ đc ạ

    Trả lờiXóa
    Trả lời
    1. có người để hỏi mà k thèm hỏi!

      Xóa
    2. này kiểu gì ak. bao nhiêu nguoi gọi không nghe. lm cho m.n lo lắng là sao

      Xóa
    3. làm sau để coi đc hình sau khi xén ạ

      Xóa
  2. Nhận xét này đã bị tác giả xóa.

    Trả lờiXóa