Tuesday, October 13, 2009

Simple C program for Scan Line Polygon Filling Algorithm

#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("\n\n\tEnter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");

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:\\tc\\bgi");


/*- 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();
}

}

}

28 comments:

shashank said...

nice one!!!

rocky singh said...

thanks angad for making this program i was long time looking for it.

Angad said...

I'm glad I could be of some help !

Amandeep F Jiddewar said...

Thank You very much Angad
It was very helpful a day before my exam!

sruthi.k said...

hi angad...v r very thankful to u because ur graphics program helped us a lot...and i compiled all of ur programs in graphics..they r so good... one request:can u send the program for checking the given graph is eulerian or not, by using adjacency matrix.

Raj Ahuja said...

hi angad, is this program based on DDA polygon drawing, or any different algo.???

Anonymous said...

thank you for the program..

aarika said...

hi angad,ur program really helped me a lot.i would b grateful if u could explain the logic of this program

Amal said...

thanks dude!

harsh said...

mast ha bhai yaar agar edge list se bana de to bhi achcha hoga

Divesh said...

hi angad,
if(dy==0) slope[i]=MAX_INTERGER;
if(dx==0) slope[i]=0;
but
if(dy==0) slope[i]=1.0;
how ??

Anonymous said...

ur algo is absolutely correct, & thnks 4 dat...
i can't run ur scanfill progm.

Anonymous said...

ur algo is absolutely correct, & thnks 4 dat...
i can't run ur scanfill progm.
help me in adak32@gmail.com

Anonymous said...

why dy==o then slope=1????

Anonymous said...

that was nice

Anonymous said...

hello angad
can u explain me
for(y=0;y< 480;y++)
{
k=0;
for(i=0;iy))||
((a[i][1]>y)&&(a[i+1][1]<=y)))
{
xi[k]=(int)(a[i][0]+slope[i]*(y-a[i][1]));
k++;
}
}
above loop

Anonymous said...

thank u very much. it helped me a lot :)

gargee said...

thanx a lot this program helped me for my final exam

vamsi said...

I planing to write algorithm part of my project work question is following
problem is finding intersection of between two ellipses
(x-x1)^2/a^2+(y-y1)^2/b^2=1,(x-x2)^2/c^2+(y-y2)^2/d^2=1 (x1,y1),(x2,y2) point of ellipse and a,b,c,d are major and minor axis of ellipses
please provide any idea of how to find out intersection of two ellipses

Naren said...

output?

Unknown said...

Can anyone. Explain this please
hello angad
can u explain me
for(y=0;y< 480;y++)
{
k=0;
for(i=0;iy))||
((a[i][1]>y)&&(a[i+1][1]<=y)))
{
xi[k]=(int)(a[i][0]+slope[i]*(y-a[i][1]));
k

Unknown said...

Are output nahi aa raha hai
lagta HAI galat hai....

Unknown said...

color fill nahi ho raha hai........kisne likha hai ye programmm faltu

Unknown said...

@Amey_Borole This program works fine... you need to check your configuration first... do not blame writer.... This is absolutely fine...

Shubhu said...

Thanks dude for the code

Unknown said...

Wht r the co ordinates to enter

Tutorialspoint said...

Nice blog


Tutorialspoint
Quality Management Online Training
Scala Online Training
Design Patterns Online Training
Customer Service Online Training
Scrum Online Training
SAP HANA Online Training

Unknown said...

Which polygon is created in this code??? is it 3-sided or more??