Page 107 - DCAP313_LAB_ON_COMPUTER_GRAPHICS
P. 107

Unit 6: Implementing Polygon Filling Algorithm



            C Program to Fill Any Given Polygon Using Flood Filling Algorithm                     Notes
                     #include <graphics.h>
                     #include <stdio.h>
                     #include <stdlib.h>
                     #include <conio.h>
                     struct Node
                   {
                      int x;
                      int y;
                      struct Node* next;
                     }  ;
                     void fill (int pt[][2], int clr);
                     void floodfill4 (int x, int y, int oldclr, int newclr);
                     void insert (int x, int y, struct Node** last);
                     void main()
                   {
                      int i, j;
                      int pt[3][2];
                      int clr;
                      printf (“This program demonstrates filling a polygon.\n”);
                      printf (“Enter the x- and y-coordinates for three points:\n”);
                      for (i = 0; i < 3; i++)
                        for (j=0; j<2; j++)
                         scanf (“%d”, &pt[i][j]);
                      printf (“Enter the fill-colour: (Any number from 1 to 14)”);
                      scanf (“%d”, &clr);
                      fill (pt, clr);
                   }
                     void fill (int pt[][2], int clr)
                   {
                      int gd = DETECT, gm;
                      int seedx, seedy;
                      initgraph (&gd, &gm, “c\\tc\\bgi”);
                      setcolor (WHITE);
                      line (pt[0][0], pt[0][1], pt[1][0], pt[1][1]);
                      line (pt[1][0], pt[1][1], pt[2][0], pt[2][1]);
                      line (pt[2][0], pt[2][1], pt[0][0], pt[0][1]);
                      getch();
                      seedx = (pt[0][0] + pt[1][0] + pt[2][0])/3;
                      seedy = (pt[0][1] + pt[1][1] + pt[2][1])/3;
                      floodfill4 (seedx, seedy, BLACK, clr);
                      getch();


                                             LOVELY PROFESSIONAL UNIVERSITY                                   101
   102   103   104   105   106   107   108   109   110   111   112