Scan Line

#include 
#include 
#include 


/* Defining the structure to store edges
-----------------------------------------*/
struct edge
{
	int x1;
	int y1;
	int x2;
	int y2;
	int flag;
};

struct edge ed[10],temped;
float dx,dy,m[10],x_int[10],inter_x[10],xx[300], xxx[300];
int ymax = 0, ymin = 499, yy,temp;
int i, j, k;
int n, kk;
int x[] = {100, 200, 300};
int y[] = {100, 300, 100};

void scanline(void)
{
	n = 3;	// number of vertices

	// find Ymax and Ymin


for(i = 0; i  ymax)
		ymax = y[i];
	if(y[i] < ymin)
		ymin = y[i];
	ed[i].x1 = x[i];
	ed[i].y1 = y[i];
}
	// Store the edge information

	for(i=0;iy2, if not interchange y1 and y2
   	   with corresponding x1 and x2 ---------------*/
	for(i=0;i<n;i++)
	{
		if(ed[i].y1 < ed[i].y2)
		{
			temp = ed[i].x1;
			ed[i].x1=ed[i].x2;
			ed[i].x2=temp;
			temp = ed[i].y1;
			ed[i].y1=ed[i].y2;
			ed[i].y2=temp;
		}
	}


/* sorting of edges in the order of y1,y2,x1
--------------------------------------------- */
  for(i=0;i<n-1;i++)
  {
     for(j=0;j<n-1;j++)
     {
    	 if(ed[j].y1<ed[j+1].y1)
    	 {
    		 temped = ed[j];
    		 ed[j]=ed[j+1];
    		 ed[j+1] = temped;
    	 }
    	 if(ed[j].y1==ed[j+1].y1)
    	 {
    		 if(ed[j].y2<ed[j+1].y2)
    		 {
    			 temped = ed[j];
    			 ed[j]=ed[j+1];
    			 ed[j+1] = temped;
    		 }
    		 if(ed[j].y2==ed[j+1].y2)
    		 {
    			 if(ed[j].x1<ed[j+1].x1)
    			 {
    				 temped = ed[j];
    				 ed[j]=ed[j+1];
    				 ed[j+1] = temped;
    			 }
    		 }
    	 }
      }
   }
  // calculating 1/slope of each edge  and storing top x
  // co-ordinate of the edge
  for(i=0;iymin)
  {

	  //  Marking active edges

	  for(i=0;i ed[i].y2 && yy <= ed[i].y1)
		  {
			  ed[i].flag = 1;
		  }
		  else
		  {
			  ed[i].flag = 0;
		  }
	  }

	  // Finding the x intersections

	  j=0;
	  for(i=0;i<n;i++)
	  {
		  if(ed[i].flag==1)
		  {
			  if(yy==ed[i].y1)
			  {
				  x_int[j]=ed[i].x1;
				  j++;
				  if(ed[i-1].y1==yy && ed[i-1].y1<yy)
				  {
					  x_int[j]=ed[i].x1;
					  j++;
				  }
				  if(ed[i+1].y1==yy && ed[i+1].y1<yy)
				  {
					  x_int[j]=ed[i].x1;
					  j++;
				  }
			  }
			  else
			  {
				  x_int[j] = inter_x[i]+(-m[i]);
				  inter_x[i]=x_int[j];
				  j++;
			  }
		  }
	  }

	  // Sorting the x intersections

	  for(i=0;i<j;i++)
	  {
		  for(k=0;kx_int[k+1])
			  {
				  temp =x_int[k];
				  x_int[k] = x_int[k+1];
				  x_int[k+1] = temp;
			  }
		  }
	  }
	  // Extracting pairs of x values to draw lines

	  for(i=0;iymin)
  {
  	glBegin(GL_LINES);			// Draw lines
  		glVertex2f(xx[i],yy);
  	  	glVertex2f(xxx[i],yy);
  	glEnd();
   	yy--;
  	i++;
  }
}
void display()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0, 1.0, 1.0);
	glBegin(GL_LINES);			// Draw triangle
	  	glVertex2f(100,100);
	  	glVertex2f(200,300);
	  	glVertex2f(300,100);
	glEnd();
	scanline();
	glFlush();
}
void myinit()
{
	glClearColor(0.0,0.0,0.0,1.0);
	glColor3f(1.0,0.0,0.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0,499.0,0.0,499.0);
}
int main(int argc, char** argv)
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	glutInitWindowSize(500,500);
	glutInitWindowPosition(0,0);
	glutCreateWindow("Filling a Polygon using Scan-line Algorithm");
	myinit();
	glutDisplayFunc(display);

	glutMainLoop();
	return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s