AET在多边形扫描转换中的使用 Scan conversion polygon and fill red color inside

</pre><pre name="code" class="cpp">
 

#include<glut.h>
#include <stdio.h>
#include<Windows.h>
/* initialization: */ 

void myinit(void)  
{     
/* attributes */    
      glClearColor(1.0, 1.0, 1.0, 0.0); /* 设置窗口背景颜色为白色 */  
      glColor3f(1.0, 0.0, 0.0);          
      glMatrixMode(GL_PROJECTION);   /* 指定设置投影参数 */  
      glLoadIdentity();              /* 调用单位矩阵,去掉以前的投影参数 */  
      gluOrtho2D(0.0, 100.0, 0.0, 100.0);   /* 设置投影参数 */  
      glMatrixMode(GL_MODELVIEW);  
}  


void AETFillColor()
{
     const int POINTNUM=5;      //多边形点数.

	/******定义结构体用于活性边表AET和新边表NET***********************************/
	typedef struct XET
	{
		float x;
		float dx,ymax;
		struct XET* next;
	}AET,NET;
	/******定义点结构体point******************************************************/
	struct point
	{
		float x;
		float y;
	}polypoint[POINTNUM]={20,20,40,30,55,90,10,50,20,50};//多边形顶点

	/******计算最高点的y坐标**************************************/
	int MaxY=0;
	int MinY = 100;

	int i;
	for( i = 0;i < POINTNUM; i++ )
	{
		if( polypoint[i].y > MaxY )
		{
			MaxY = polypoint[i].y;
		}

		if( polypoint[i].y < MinY )
		{
			MinY = polypoint[i].y;
		}

	}

	/*******初始化AET表,即初始化活跃边表***********************************************************/
	AET *pAET = new AET;
	pAET->next = NULL;

	/******初始化NET表,即初始化边表************************************************************/
	NET *pNET[1024];
	for( i = MinY;i <= MaxY; i++ )
	{
		pNET[i] = new NET;
		pNET[i]->next = NULL;
	}

	/******扫描并建立NET表,即建立边表*********************************************************/
	for( i = MinY; i <= MaxY; i++ )
	{
		for( int j = 0;j < POINTNUM; j++ )
		{
			if( polypoint[j].y == i)
			{                    
				if( polypoint[ (j-1+POINTNUM) % POINTNUM ].y > polypoint[j].y )
				{
					NET *p=new NET;
					p->x = polypoint[j].x;
					p->ymax = polypoint[ (j-1+POINTNUM) % POINTNUM ].y;
					p->dx = ( polypoint[ (j-1+POINTNUM)%POINTNUM ].x-polypoint[j].x ) / ( polypoint[ (j-1+POINTNUM) % POINTNUM ].y - polypoint[j].y );
					p->next = pNET[i]->next;
					pNET[i]->next = p;
				}

				if( polypoint[ (j+1+POINTNUM ) % POINTNUM].y > polypoint[j].y )
				{
					NET *p = new NET;                        
					p->x = polypoint[j].x;
					p->ymax = polypoint[ (j+1+POINTNUM) % POINTNUM ].y;
					p->dx = ( polypoint[ (j+1+POINTNUM) % POINTNUM ].x-polypoint[j].x ) / ( polypoint[ (j+1+POINTNUM) % POINTNUM ].y- polypoint[j].y );
					p->next = pNET[i]->next;
					pNET[i]->next = p;
				}
			}
		}
	}

	/******建立并更新活性边表AET*****************************************************/
	for( i = MinY; i <= MaxY; i++ ) 						
	{					          									
		//计算新的交点x,更新AET********************************************************/
		NET *p = pAET->next;
		while( p != NULL )
		{
			p->x = p->x + p->dx;
			p = p->next;
		}

		//更新后新AET先排序*************************************************************/
		AET *tq = pAET;
		p = pAET->next;
		tq->next = NULL;

		while( p != NULL )
		{
			while( tq->next != NULL && p->x >= tq->next->x )
			{
				tq = tq->next;
			}

			NET *s = p->next;
			p->next = tq->next;
			tq->next = p;
			p = s;
			tq = pAET;
		}

		//先从AET表中删除ymax==i的结点****************************************/
		AET *q = pAET;
		p = q->next;
		while( p != NULL )
		{
			if( p->ymax == i)
			{
				q->next = p->next;
				delete p;
				p = q->next;
			}
			else
			{
				q = q->next;
				p = q->next;
			}
		}

		//将NET中的新点加入AET,并用插入法按X值递增排序**********************************/
		p = pNET[i]->next;
		q = pAET;
		while( p != NULL )
		{
			while( q->next != NULL && p->x >= q->next->x)
			{
				q = q->next;
			}
				
			NET *s = p->next;
			p->next = q->next;
			q->next = p;
			p = s;
			q = pAET;
		}

		/******配对填充颜色***************************************************************/
		p = pAET->next;
		while( p != NULL && p->next != NULL )
		{
			for(float j = p->x;j <= p->next->x; j++)
			{
				glBegin(GL_POINTS);  
                glVertex3f(j,i,0);   
                glEnd(); 
			}  

			p = p->next->next;//考虑端点情况
		}    
	}

	NET *phead = NULL;
	NET *pnext = NULL;
	//释放边表
	//释放活跃边表
	phead = pAET;
	while( phead != NULL )
	{
		pnext = phead->next;
		delete phead;
		phead = pnext;
	}
	}
	void display( void )  
{  
    glClear(GL_COLOR_BUFFER_BIT);  /*用当前背景色填充窗口 */    
	glColor3f(1.0f,0.0f,0.0f);     /*设置当前的绘图颜色为红色*/ 
	AETFillColor();
    glFlush();                    /* 清空缓存,执行程序 */             
 }  
int main(int argc, char** argv)  
{    
    glutInit(&argc,argv);  
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);   /* 初始化窗口显示模式 */  
    glutInitWindowSize(500,500);                    /* 设置窗口尺寸 500 x 500 pixel  */  
    glutInitWindowPosition(0,0);                    /* 设置窗口位置 */  
    glutCreateWindow("Draw polygon");                        /* 命名窗口 */  
    glutDisplayFunc(display);                      
    myinit();                    
    glutMainLoop();                 
}

http://blog.csdn.net/xiaowei_cqu/article/details/7712451    AET表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值