-
Notifications
You must be signed in to change notification settings - Fork 1
/
Polygon Filling Scanline Algorithm (windows only).c
85 lines (75 loc) · 1.34 KB
/
Polygon Filling Scanline Algorithm (windows only).c
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
/**
* @author: Ashish A Gaikwad <ash.gkwd@gmail.com>
* Polygon Filling Scanline Algorithm in C.
* Runs only on windows due to dependency on 'graphics.h' header file.
*/
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void main()
{
int n, i, j, k, dy, dx;
int x, y, temp, gd = DETECT, gm = DETECT;
int a[20][2], xi[20];
float slope[20];
clrscr();
printf("\nEnter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\nEnter the cordinates of polygon : ");
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,"");
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))
{
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++)
{
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();
}
}
}