关闭

多边形的扫描转换与区域填充算法

标签: 计算机图形学
989人阅读 评论(0) 收藏 举报
分类:

1算法步骤:

X——扫描线算法填充多边形的原理见下图。每一条扫描线被多边形分成几段,每一段要么在多边形内,要么在多边形外,在内的填充(用线型、点或颜色),在外的则舍弃。


3-1  X----扫描线算法填充多边形

 

2)算法步骤

1)按多边形的各顶点y坐标大小排序,确定多边形所占有的最大扫描线数,得到多边形顶点的最小和最大y值(yminymax);

2)y=yminy=ymax,每次用一条扫描线进行填充;

3)对一条扫描线填充的过程可分为四个步骤:

a.求交:一般情况交点是偶数,特殊情况见下面介绍的存在问题;

b.排序:若交点多于两个,则按x大小排序(小的在前),并建立焦点表;

c.交点配对:在扫描线上从单点到双点(即第一个与第二个,第三个与第四个等等)按序构成交点对;

d.区间填色:在扫描线上对每一交点配对进行填充。

3)存在问题:当扫描线与多边形顶点相交时,交点的取舍问题。

解决方法:当扫描线与多边形的顶点相交时,

若共享顶点的两条边分别落在扫描线的两边,交点只算一个;

若共享顶点的两条边在扫描线的同一边,这时交点作为零个或两个。 

如下图3-2所示。


3-2 与多边形顶点相交的交点的处理和与扫描线相交的多边形顶点的交点数



代码如下:

#include <GL/glut.h>
#include <windows.h>

const int POINTNUM = 7;

typedef struct XET{
	float x;
	float dx,ymax;
	XET* next;
}AET,NET;

struct point{
	float x;
	float y;
}

polypoint[POINTNUM] = {250,50,550,150,550,400,250,250,100,350,100,100,120,30};

void PolyScan(){
	int MaxY = 0;
	int i;
	for(i = 0;i < POINTNUM;i++){
		if(polypoint[i].y > MaxY)
			MaxY = polypoint[i].y;
	}
	AET *pAET = new AET;
	pAET->next=NULL;
	
	NET *pNET[1024];
	for(i = 0;i <= MaxY;i++){
		pNET[i] = new NET;
		pNET[i]->next = NULL;
	}
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.0,0.0,0.0);
	glBegin(GL_POINTS);
	for(i = 0;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;
				}
			}
		}
	}
	for(i = 0;i <= MaxY;i++){
		NET *p = pAET->next;
		while(p){
			p->x = p->x + p->dx;
			p = p->next;
		}
		AET *tq = pAET;
		p = pAET->next;
		tq->next = NULL;
		while(p){
			while(tq->next && p->x >= tq->next->x)
				tq = tq->next;
			NET *s = p->next;
			p->next = tq->next;
			tq ->next = p;
			p = s;
			tq = pAET;
		}
		AET *q = pAET;
		p = q->next;
		while(p){
			if(p->ymax == i){
				q->next = p->next;
				delete p;
				p = q->next;
			}
			else{
				q = q->next;
				p = q->next;
			}
		}
		p = pNET[i]->next;
		q = pAET;
		while(p){
			while(q->next && 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 && p->next){
			for(float j = p->x;j <= p->next->x;j++){
				glVertex2i(static_cast<int>(j),i);
			}
			p = p->next->next;
		}
	}
	glEnd();
	glFlush();
}

void init(void){
	glClearColor(1.0,1.0,1.0,0.0);
	glMatrixMode(GL_PROJECTION);
	gluOrtho2D(0.0,600.0,0.0,450.0);
}

void main(int argc,char* argv){
	glutInit(&argc,&argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowPosition(50,100);
	glutInitWindowSize(400,300);
	glutCreateWindow("An Example OpenGL Program");

	init();
	glutDisplayFunc(PolyScan);
	glutMainLoop();
}


3
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3828次
    • 积分:288
    • 等级:
    • 排名:千里之外
    • 原创:26篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条