图形学实验 区域填充 OpenGL实现

实验二 区域填充算法

运行环境

配置和工程文件下载地址: 点击下载实验工程

Windows10

visual studio 2017

配置32和64位 OpenGl环境

实验代码

// 实验二 7个点的区域填充.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#define GLUT_DISABLE_ATEXIT_HACK 
#pragma comment(lib, "Ws2_32.lib")
#include 
#include 
#include 
#include 
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; iMaxY)
			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; jpolypoint[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(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);
}

int main(int argc, char** argv)
{
	glutInit(&argc, argv);                //I初始化 GLUT.
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);    //设置显示模式:单个缓存和使用RGB模型
	glutInitWindowPosition(50, 100);        //设置窗口的顶部和左边位置
	glutInitWindowSize(400, 300);        //设置窗口的高度和宽度
	glutCreateWindow("An Example OpenGL Program");    //创建显示窗口

	init();                                //调用初始化过程
	glutDisplayFunc(PolyScan);        //图形的定义传递给我window.
	glutMainLoop();                        //显示所有的图形并等待
	return 0;
}

实验结果

图形学实验 区域填充 OpenGL实现_第1张图片

你可能感兴趣的:(同学问的题目)