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;
}

10 Responses to “C Program to fill any given polygon using scan-line area filling algorithm”

  1. That is not a scan-line filling algorithm. It is a flood fill algorithm. If you are going to rip the code off of another site at least label it correctly.

    Reply
  2. Sanchit Shinde

    You are a fraud! This is not scan line polygon algorithm I wasted two hours of my life figuring out how it works and turns out it is flood fill. Burn in hell. Hope you get 123123423908423904 errors in each program you write

    Reply
  3. Suraj Pachakar

    Algorithm givn wrng. Its actuali flud fil algorithm. You are a faek. C******a!!

    Reply

Leave a Reply