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

多边形的扫描转换

这是我的第一篇csdn博客,算是出于对计算机图形学的热情,在这门课程已经结束一个学期之际,重新联系写图形学算法,当然这都是“黑发不知勤学早”的结果了,之前在找相关算法的时候感觉网上其他博主写的都太过教科书化,毕竟算法的实现跟理论还是差那边么一点点的,于是就把我个人一丢丢小思路结合理论写一下,希望对新入坑的同学起到帮助,如果有不完美之处也请指正。

多边形扫描转换算法主要是从图形的顶点表示入手的。

扫描线算法

扫描线多边形区域填充算法是按照扫描线顺序,计算扫描线与多边形相交区间,再用要求的颜色填充满要填充的区域。

理论思想

求交,计算扫描线于多边形各边的交点。

排序,把所有交点按照x值的递增顺序排序

配对,将第一个与第二个,第三个与第四个(以此类推)配对

填色,把相交区间之间填充上指定颜色。

数据定义

    X

     dx

   ymax

    next


x:活性边的x

dxy值加一对应的x值增量

ymax:  此活性边对应的y的最大值

next :  下一点的地址

数据结构

活性边表


图示



#include <GL/freeglut.h>  
#include <windows.h>
using namespace std;



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;
				}
				
			}
		}
	}
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.0, 0.0, 0.0);
	glBegin(GL_POINTS);
	for (i = 0; i <= MaxY; i++) {
		AET *p = new AET;
		p = pAET->next;
		AET *n = new AET;
		//将新边表中的活性边按照从左到右的顺序排序
		if (pNET[i]->next && pNET[i]->next->next) {
			if (pNET[i]->next->dx > 0) {
				NET *t = new NET;
				t = pNET[i]->next;
				n = pNET[i]->next->next;
				t->next = NULL;
				n->next = NULL;
				pNET[i]->next = n;
				n->next = t;
			}
		}
		//更新活性边表中的活性边x坐标的值
		while (p) {
			p->x = p->x + p->dx;
			p = p->next;
		}
		p = pAET->next;
        n = pAET;
		//删掉扫描线高度等同于ymax的废弃点
		while (p){
			if (p->ymax == i) {
				n->next = p->next;
				free(p);
				p = n->next;
			}
			else {
				p = p->next;
				n = n->next;
			}
		}
		//插入新点,按照顺序插入
		p = pAET->next;
		n = pAET;
		NET *a = new NET;
		a = pNET[i]->next;
		if (a) {
			NET *b = new NET;
			b = a;
			while (b->next) {
				b = b->next;
			}
			if (!pAET->next) {
				pAET->next = a;
			}
			else {
				while (p) {
					if (a->x < p->x) {
						b->next = p;
						n->next = a;
						break;
					}
					if (!p->next) {
						p->next = a;
						break; 
					}
					n = n->next;
					p = p->next;
				}
			}
		}
		//填充2
		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();
}


#include #include #include #include #include //////////////////////////////////////////////////////////////functions///////////////////////////////////////////////// void swap(float &m,float &n) { float temp=n; n=m; m=temp; } int sign(float a,float b)//sign() { int t; if(a>b) { t=1;} else if(adx) { swap(dx,dy); Flag=1; } else Flag=0; float Nerror=2*dy-dx; for(int i=1;i=0) { if(Flag) { x=x+sx;} else y=y+sy; Nerror=Nerror-2*dx; } if(Flag) { y=y+sy;} else x=x+sx; Nerror=Nerror+2*dy; } } ///////////////////////////////////四连通种子填充/////////////////////////////////////////////////// void BoundaryFill4(HDC hdc,int x,int y,int FilledColor,int BoundaryColor) { int CurrentColor; CurrentColor=GetPixel(hdc,x,y); if(CurrentColor!=BoundaryColor&&CurrentColor!=FilledColor) { SetPixel(hdc,x,y,FilledColor); BoundaryFill4(hdc,x+1,y,FilledColor,BoundaryColor); BoundaryFill4(hdc,x-1,y,FilledColor,BoundaryColor); BoundaryFill4(hdc,x,y+1,FilledColor,BoundaryColor); BoundaryFill4(hdc,x,y-1,FilledColor,BoundaryColor); } } ////////////////////////////////////////扫描线填充/////////////////////////////////////////////////// //DrawLine()函数:在(x1,y)和(x2,y)两点之间画一条颜色为FilledColor的横线(用来扫描填充) void drawline(HDC hdc, int x1, int x2,int y0, int FilledColor) { for(int n=x1+1;n<x2;n++) { SetPixel(hdc,n,y0,FilledColor); } } //Scan()函数:扫描线函数,将扫描线与图形的交点坐标存在数组中 //数组中同行的点即为该行扫描线与图形的交点(一般为2个) //数组中的行代表扫描线的纵坐标 void scan(HDC hdc, int boundarycolor) { int currentcolor; int a[300][2]={0}; for (int j=0;j<300;j++) { for(int i=300;i<700;i++) { currentcolor=GetPixel(hdc,i,j); if((currentcolor==boundarycolor)&&(GetPixel(hdc,i+1,j)!=boundarycolor)&&(i500)) {a[j][1]=i;} } } //利用循环调用DrawLine函数逐行填充两交点之间的点 for(int k=0;k<300;k++) { if((a[k][0]!=0)&&(a[k][1]!=0)) drawline(hdc,a[k][0],a[k][1],k,RGB(255,0,0));} } ///////////////////////////////////////////////边界填充////////////////////////////////////// //Contrary()取反函数:如果点的颜色为白,则将点置为填充色;如果点的颜色为填充色,则将点置为白色 //忽略了边界色,即不对边界点作颜色处理 void contrary(HDC hdc, int x, int y,int FilledColor) { for(int h=x;h<280;h++) { if(GetPixel(hdc,h,y)==RGB(255,255,255)) { SetPixel(hdc,h,y,FilledColor); } else if(GetPixel(hdc,h,y)==FilledColor) { SetPixel(hdc,h,y,RGB(255,255,255)); } } } //borderline()边线函数: 先找出图形的边界 左边和右边,从右到左的顺序调用contrary()函数进行填充 void borderline(HDC hdc, int boundarycolor) { for(int j=280;j<499;j++) { for(int i=80;i100)) { contrary(hdc,i,j,RGB(0,0,255)); } } } for(int m=280;m<499;m++) { for(int n=80;n<280;n++) { int currentcolor=GetPixel(hdc,n,m); if((currentcolor==boundarycolor)&&(GetPixel(hdc,n+1,m)!=boundarycolor)&&(GetPixel(hdc,n-1,m)!=boundarycolor)&&(n<101)) { contrary(hdc,n,m,RGB(0,0,255)); } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值