OpenGL实现不规则区域填充算法

所属分类: 软件编程 / C 语言 阅读数: 95
收藏 0 赞 0 分享

本文实例为大家分享了OpenGL实现不规则区域填充算法,供大家参考,具体内容如下

一、简单递归

利用Dfs实现简单递归填充。
核心代码:

// 简单深度搜索填充 (四连通)
void DfsFill(int x, int y)
{
 if (x < 0 || y < 0 || x>23 || y>23)
 {
 return;
 }
 if (a[x][y] == 0)
 {
 a[x][y] = 2;
 DfsFill(x - 1, y);
 DfsFill(x + 1, y);
 DfsFill(x, y - 1);
 DfsFill(x, y + 1);
 }
}

二、扫描线种子填充算法(四连通)

1. 种子点(x,y)入栈。
2. 栈顶元素(x,y)出栈作为种子点。
3. 从种子点(x,y)开始沿着扫描线向左右两个方向逐个像素进行填充,直到到达边界像素为止。
4. 将上述填充区段的左右端点的横坐标分别记为xleft和xright.
5. 在与当前扫描线相邻的上下两条扫描线的[xleft,xright]范围内进行检查,看看是否全部为边界像素或已填充像素,若存在着非边界且未填充的像素,那么将该区段的最右端像素作为种子点入栈。

void ScanFill(int x, int y)
{
 if (a[x][y]!=0)
 {
 return;
 }
 Pos first(x, y);
 s.push(first);
 while (!s.empty())
 {
 int rightX = 0;
 int leftX = 0;
 Pos cur = s.top();
 s.pop();
 a[cur.x][cur.y] = 2;
 // 遍历当前行
 for (int i = 1; i < 24; i++)
 {
 if (cur.x + i < 24)
 {
 if (a[cur.x + i][cur.y] == 0)
  a[cur.x + i][cur.y] = 2;
 else
 {
  rightX = cur.x + i - 1;
  break;
 }
 }
 if (i==23)
 {
 rightX = 23;
 }
 }
 for (int i = 1; i < 24; i++)
 {
 if (cur.x - i > -1) 
 {
 if (a[cur.x - i][cur.y] == 0)
  a[cur.x - i][cur.y] = 2;
 else
 {
  leftX = cur.x - i + 1;
  break;
 }
 }
 if (i == 0)
 {
 leftX = 0;
 }
 }

 cout << leftX <<","<<rightX << endl;

 // 判断上行
 int upRightX = -1;
 for (int i = leftX;i<=rightX;i++)
 {
 if (a[i][cur.y+1]==0 && cur.y+1<24)
 {
 upRightX = i;
 }
 }
 if (upRightX!=-1)
 {
 Pos temPos(upRightX,cur.y+1);
 s.push(temPos);
 }

 // 判断下行
 int downRightX = -1;
 for (int i = leftX; i <= rightX; i++)
 {
 if (a[i][cur.y - 1] == 0 && cur.y - 1 >=0)
 {
 downRightX = i;
 }
 }
 if (downRightX != -1)
 {
 Pos temPos(downRightX, cur.y - 1);
 s.push(temPos);
 }

 }
}

完整代码:

#include <cmath>
#include <stack>
#include "gl/glut.h"
#include "iostream"
using namespace std;

#define PI 3.14

struct Pos
{
 int x;
 int y;
 Pos(int mx, int my) :x(mx), y(my) {};
 Pos() :x(0), y(0) {};
};

stack<Pos> s;
int a[24][24] = { 0 };

void init(void)
{
 glClearColor(1.0, 1.0, 1.0, 1.0);
 glMatrixMode(GL_PROJECTION);//设置投影矩阵
 gluOrtho2D(0.0, 600.0, 0.0, 600.0);//二维视景区域
 glPointSize(12.0f);
}
// 画棋子
void Drawtri(int x,int y,int color)
{
 double n = 200;//分段数
 float R = 10;//半径
 int i;
 if (color == 1)
 {
 glColor3f(1.0, 0.0, 0.0);
 }
 else if (color == 2)
 {
 glColor3f(0.0, 1.0, 0.0);
 }
 glBegin(GL_POLYGON);
 glVertex2f(x, y);
 for (i = 0; i <= n; i++)
 glVertex2f(R*cos(2 * PI / n * i)+x, R*sin(2 * PI / n * i)+y);
 glEnd();
 glPopMatrix();
}

// 绘制格线
void playMap()
{
 glColor3f(0.0, 0.0, 0.0);
 glBegin(GL_LINES);
 for (int i = 0; i < 600; i += 25)
 {
 glVertex2f(i, 0);
 glVertex2f(i, 600);
 }
 for (int j = 0; j < 600; j += 25)
 {
 glVertex2f(0, j);
 glVertex2f(600, j);
 }
 glEnd();
 for (int k = 0; k < 24; k++)
 {
 for (int l = 0; l < 24; l++)
 {
 if (a[k][l] == 1)
 {
 Drawtri(k * 25 + 12, l * 25 + 12,1);
 }
 else if (a[k][l] == 2)
 {
 Drawtri(k * 25 + 12, l * 25 + 12, 2);
 }
 }
 }
}

// 简单深度搜索填充 (四连通)
void DfsFill(int x, int y)
{
 if (x < 0 || y < 0 || x>23 || y>23)
 {
 return;
 }
 if (a[x][y] == 0)
 {
 a[x][y] = 2;
 DfsFill(x - 1, y);
 DfsFill(x + 1, y);
 DfsFill(x, y - 1);
 DfsFill(x, y + 1);
 }
}

// 扫描线种子填充算法(四连通)
void ScanFill(int x, int y)
{
 if (a[x][y]!=0)
 {
 return;
 }
 Pos first(x, y);
 s.push(first);
 while (!s.empty())
 {
 int rightX = 0;
 int leftX = 0;
 Pos cur = s.top();
 s.pop();
 a[cur.x][cur.y] = 2;
 // 遍历当前行
 for (int i = 1; i < 24; i++)
 {
 if (cur.x + i < 24)
 {
 if (a[cur.x + i][cur.y] == 0)
  a[cur.x + i][cur.y] = 2;
 else
 {
  rightX = cur.x + i - 1;
  break;
 }
 }
 if (i==23)
 {
 rightX = 23;
 }
 }
 for (int i = 1; i < 24; i++)
 {
 if (cur.x - i > -1) 
 {
 if (a[cur.x - i][cur.y] == 0)
  a[cur.x - i][cur.y] = 2;
 else
 {
  leftX = cur.x - i + 1;
  break;
 }
 }
 if (i == 0)
 {
 leftX = 0;
 }
 }

 cout << leftX <<","<<rightX << endl;

 // 判断上行
 int upRightX = -1;
 for (int i = leftX;i<=rightX;i++)
 {
 if (a[i][cur.y+1]==0 && cur.y+1<24)
 {
 upRightX = i;
 }
 }
 if (upRightX!=-1)
 {
 Pos temPos(upRightX,cur.y+1);
 s.push(temPos);
 }

 // 判断下行
 int downRightX = -1;
 for (int i = leftX; i <= rightX; i++)
 {
 if (a[i][cur.y - 1] == 0 && cur.y - 1 >=0)
 {
 downRightX = i;
 }
 }
 if (downRightX != -1)
 {
 Pos temPos(downRightX, cur.y - 1);
 s.push(temPos);
 }

 }
}

void displayFcn(void)
{
 glClear(GL_COLOR_BUFFER_BIT);
 playMap();
 glFlush();
}


void mouse(GLint button, GLint action, GLint x, GLint y)
{
 int curX, curY;
 if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
 {
 curX = x / 25;
 curY = (600 - y) / 25;
 a[curX][curY] = 1;
 glutPostRedisplay();//重绘窗口
 }
 if (button == GLUT_RIGHT_BUTTON && action == GLUT_DOWN)
 {
 curX = x / 25;
 curY = (600 - y) / 25;
 ScanFill(curX, curY);
 
 glutPostRedisplay();//重绘窗口
 }
}


void main(int argc, char** argv)
{
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
 glutInitWindowPosition(300, 100);
 glutInitWindowSize(600, 600);
 glutCreateWindow("mouse");

 init();
 glutDisplayFunc(displayFcn);

 glutMouseFunc(mouse);

 glutMainLoop();

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

从汇编看c++中变量类型的深入分析

本篇文章是对c++中的变量类型进行了详细的分析介绍。需要的朋友参考下
收藏 0 赞 0 分享

从汇编看c++的默认析构函数的使用详解

本篇文章是对c++中默认析构函数的使用进行了详细的分析介绍。需要的朋友参考下
收藏 0 赞 0 分享

基于c++中的默认拷贝函数的使用详解

本篇文章对c++中默认拷贝函数的使用进行了详细的分析介绍。需要的朋友参考下
收藏 0 赞 0 分享

解析c++中的默认operator=操作的详解

本篇文章是对c++中的默认operator=操作的应用进行了详细的分析介绍。需要的朋友参考下
收藏 0 赞 0 分享

解析c++中参数对象与局部对象的析构顺序的详解

本篇文章是对c++中参数对象与局部对象的析构顺序进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

深入c++中临时对象的析构时机的详解

本篇文章对c++中临时对象的析构时机进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

解析内存对齐 Data alignment: Straighten up and fly right的详解

对于所有直接操作内存的程序员来说,数据对齐都是很重要的问题.数据对齐对你的程序的表现甚至能否正常运行都会产生影响
收藏 0 赞 0 分享

深入内存对齐的详解

本篇文章是对内存对齐进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

深入C语言把文件读入字符串以及将字符串写入文件的解决方法

本篇文章是对C语言把文件读入字符串以及将字符串写入文件的方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

深入Windows下的回车是回车换行(\r\n)还是换行回车(\n\r)的详解

本篇文章对Windows下的回车是回车换行(\r\n)还是换行回车(\n\r)进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享
查看更多