C Program to fill any given polygon using scan-line area filling algorithm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#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, "..\\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();
 
closegraph();
return;
}
 
void floodfill4 (int x, int y, int oldclr, int newclr)
{
struct Node* first, *last, *tmp;
 
first = (struct Node*) malloc (sizeof (struct Node));
if (first == NULL)
{
closegraph();
fprintf (stderr, "floodfill4: Out of memory.\n");
exit (2);
}
if (oldclr == newclr)
{
free (first);
return;
}
 
first->x = x;
first->y = y;
first->next = NULL;
last = first;
 
while (first != NULL)
{
putpixel (x, y, newclr);
 
if (getpixel (x, y-1) == oldclr)
{
putpixel (x, y-1, newclr);
insert (x, y-1, &last);
}
 
 
if (getpixel (x, y+1) == oldclr)
{
putpixel (x, y+1, newclr);
insert (x, y+1, &last);
}
 
if (getpixel (x-1, y) == oldclr)
{
putpixel (x-1, y, newclr);
insert (x-1, y, &last);
}
 
if (getpixel (x+1, y) == oldclr)
{
putpixel (x+1, y, newclr);
insert (x+1, y, &last);
}
 
tmp = first;
first = first->next;
x = first->x;
y = first->y;
free (tmp);
}
}
 
void insert (int x, int y, struct Node** last)
{
struct Node* p;
p = (struct Node*) malloc (sizeof (struct Node));
if (p == NULL)
{
closegraph();
fprintf (stderr, "\n insert: Out of memory.\n");
exit (2);
}
 
p->x = x;
p->y = y;
p->next = NULL;
(*last)->next = p;
*last = (*last)->next;
}
Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

3 thoughts on “C Program to fill any given polygon using scan-line area filling algorithm

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in