Filled-Area Primitives
Region filling is the process of filling image or region.
• Convex and Concave Polygons
P1
P2
P1
P2
Polygon Drawing
• int Point[10]; OR int Point[ ]={3,12,9,15,17,11,15,4,6,5};
• Point[0]=3
• Point[1]=12
• Point[2]=9
• Point[3]=15
• Point[4]=17
• Point[5]=11
• Point[6]=15
• Point[7]=4
• Point[8]=6
• Point[9]=5
drawpoly(5, Point)
OR
line (3, 12, 9, 15)
line (9, 15, 17, 11)
line (17, 11 15, 4)
line (15, 4, 6, 5)
line (6, 5, 3, 12)
• int a[20][2]
• printf("nntEnter the no. of edges of polygon : ");
• scanf("%d",&n);
• printf("nntEnter the cordinates of polygon :nnn ");
• for(i=0;i<n;i++)
• {
• printf("tX%d Y%d : ",i,i);
• scanf("%d %d",&a[i][0],&a[i][1]);
• }
• a[n][0]=a[0][0];
• a[n][1]=a[0][1];
• /*- draw polygon -*/
• for(i=0;i<n;i++)
• {
• line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
• }
POLYGON FILLING
• Means highlighting all the pixels which lie inside the
polygon.
• Colour other than background colour.
• Polygons are easier to fill.
• There are two basic approaches
1. Seed Fill
i. Boundary Fill Algorithm
ii. Flood Fill Algorithm
2. Scanline Algorithm
1.Seed Fill
(X , Y)
(X , Y - 1)
(X -1 , Y - 1)
(X + 1 , Y + 1)
(X - 1 , Y + 1) (X , Y + 1)
(X , Y - 1)
(X - 1 , Y) (X + 1 , Y)
(X , Y)
(X + 1 , Y - 1)
(X - 1 , Y) (X + 1 , Y)
(X , Y + 1)
• Disadvantage:
• 1. Very slow algorithm
• 2. May be fail for large polygons
• 3. Initial pixel required more knowledge about surrounding pixels.
Scan-Line Method for Filling Polygons
(2 , 9)
(7 , 7)
(13 , 11)
(13 , 5)
(7 , 1)
(2 , 3)
m=(9-7)/(2-7) = -2/5
=> 1/m = -5/2------for EF
• #include <stdio.h>
• #include <conio.h>
• #include <graphics.h>
• main()
• {
• int n,i,j,k,gd,gm,dy,dx;
• int x,y,temp;
• int a[20][2],xi[20];
• float slope[20];
• clrscr();
• printf("nntEnter the no. of edges of polygon : ");
• scanf("%d",&n);
• printf("nntEnter the cordinates of polygon :nnn ");
• for(i=0;i<n;i++)
• {
• printf("tX%d Y%d : ",i,i);
• scanf("%d %d",&a[i][0],&a[i][1]);
• }
• a[n][0]=a[0][0];
• a[n][1]=a[0][1];
• detectgraph(&gd,&gm);
• initgraph(&gd,&gm,"c:tcbgi");
• /*- draw polygon -*/
• for(i=0;i<n;i++)
• {
• line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
• }
• getch();
• for(i=0;i<n;i++)
• {
• dy=a[i+1][1]-a[i][1];
• dx=a[i+1][0]-a[i][0];
• if(dy==0) slope[i]=1.0;
• if(dx==0) slope[i]=0.0;
• if((dy!=0)&&(dx!=0)) /*- calculate inverse slope -*/
• {
• slope[i]=(float) dx/dy;
• }
• }
• for(y=0;y< 480;y++)
• {
• k=0;
• for(i=0;i<n;i++)
• {
• if( ((a[i][1]<=y)&&(a[i+1][1]>y))||
• ((a[i][1]>y)&&(a[i+1][1]<=y)))
• {
• xi[k]=(int)(a[i][0]+slope[i]*(y-a[i][1]));
• k++;
• }
• }
• for(j=0;j<k-1;j++) /*- Arrange x-intersections in order -*/
• for(i=0;i<k-1;i++)
• {
• if(xi[i]>xi[i+1])
• {
• temp=xi[i];
• xi[i]=xi[i+1];
• xi[i+1]=temp;
• }
• }
• setcolor(35);
• for(i=0;i<k;i+=2)
• {
• line(xi[i],y,xi[i+1]+1,y);
• getch();
• }
• }
• }
Nonzero Winding Number Rule
>Unsuitable for Line based Z-
Buffer
>These methods can also
apply to any closed curves
>Suitable for Line based Z-
Buffer
>This method is difficult to
apply to any closed curves

CA301_CG_Filled Area Primitives-New.pptx

  • 1.
    Filled-Area Primitives Region fillingis the process of filling image or region.
  • 3.
    • Convex andConcave Polygons P1 P2 P1 P2
  • 5.
  • 6.
    • int Point[10];OR int Point[ ]={3,12,9,15,17,11,15,4,6,5}; • Point[0]=3 • Point[1]=12 • Point[2]=9 • Point[3]=15 • Point[4]=17 • Point[5]=11 • Point[6]=15 • Point[7]=4 • Point[8]=6 • Point[9]=5 drawpoly(5, Point) OR line (3, 12, 9, 15) line (9, 15, 17, 11) line (17, 11 15, 4) line (15, 4, 6, 5) line (6, 5, 3, 12)
  • 7.
    • int a[20][2] •printf("nntEnter the no. of edges of polygon : "); • scanf("%d",&n); • printf("nntEnter the cordinates of polygon :nnn "); • for(i=0;i<n;i++) • { • printf("tX%d Y%d : ",i,i); • scanf("%d %d",&a[i][0],&a[i][1]); • } • a[n][0]=a[0][0]; • a[n][1]=a[0][1]; • /*- draw polygon -*/ • for(i=0;i<n;i++) • { • line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]); • }
  • 8.
    POLYGON FILLING • Meanshighlighting all the pixels which lie inside the polygon. • Colour other than background colour. • Polygons are easier to fill. • There are two basic approaches 1. Seed Fill i. Boundary Fill Algorithm ii. Flood Fill Algorithm 2. Scanline Algorithm
  • 9.
  • 11.
    (X , Y) (X, Y - 1) (X -1 , Y - 1) (X + 1 , Y + 1) (X - 1 , Y + 1) (X , Y + 1) (X , Y - 1) (X - 1 , Y) (X + 1 , Y) (X , Y) (X + 1 , Y - 1) (X - 1 , Y) (X + 1 , Y) (X , Y + 1)
  • 19.
    • Disadvantage: • 1.Very slow algorithm • 2. May be fail for large polygons • 3. Initial pixel required more knowledge about surrounding pixels.
  • 20.
    Scan-Line Method forFilling Polygons
  • 29.
    (2 , 9) (7, 7) (13 , 11) (13 , 5) (7 , 1) (2 , 3) m=(9-7)/(2-7) = -2/5 => 1/m = -5/2------for EF
  • 31.
    • #include <stdio.h> •#include <conio.h> • #include <graphics.h> • main() • { • int n,i,j,k,gd,gm,dy,dx; • int x,y,temp; • int a[20][2],xi[20]; • float slope[20]; • clrscr(); • printf("nntEnter the no. of edges of polygon : "); • scanf("%d",&n); • printf("nntEnter the cordinates of polygon :nnn "); • for(i=0;i<n;i++) • { • printf("tX%d Y%d : ",i,i); • scanf("%d %d",&a[i][0],&a[i][1]); • } • a[n][0]=a[0][0]; • a[n][1]=a[0][1]; • detectgraph(&gd,&gm); • initgraph(&gd,&gm,"c:tcbgi"); • /*- draw polygon -*/ • for(i=0;i<n;i++) • { • line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]); • } • getch();
  • 32.
    • for(i=0;i<n;i++) • { •dy=a[i+1][1]-a[i][1]; • dx=a[i+1][0]-a[i][0]; • if(dy==0) slope[i]=1.0; • if(dx==0) slope[i]=0.0; • if((dy!=0)&&(dx!=0)) /*- calculate inverse slope -*/ • { • slope[i]=(float) dx/dy; • } • } • for(y=0;y< 480;y++) • { • k=0; • for(i=0;i<n;i++) • { • if( ((a[i][1]<=y)&&(a[i+1][1]>y))|| • ((a[i][1]>y)&&(a[i+1][1]<=y))) • { • xi[k]=(int)(a[i][0]+slope[i]*(y-a[i][1])); • k++; • } • } • for(j=0;j<k-1;j++) /*- Arrange x-intersections in order -*/ • for(i=0;i<k-1;i++) • { • if(xi[i]>xi[i+1]) • { • temp=xi[i]; • xi[i]=xi[i+1]; • xi[i+1]=temp; • } • }
  • 33.
    • setcolor(35); • for(i=0;i<k;i+=2) •{ • line(xi[i],y,xi[i+1]+1,y); • getch(); • } • } • }
  • 36.
  • 46.
    >Unsuitable for Linebased Z- Buffer >These methods can also apply to any closed curves >Suitable for Line based Z- Buffer >This method is difficult to apply to any closed curves