Java 代码区域填充算法_OpenGL实现不规则区域填充算法

本文实例为大家分享了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]范围内进行检查,看看是否全部为边界像素或已填充像素,若存在着非边界且未填充的像素,那么将该区段的最右端像素作为种子点入栈。

7fa1872bb463c1d8fdd5332c5ca9a24e.png

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 <

// 判断上行

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

#include

#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 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 <

// 判断上行

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();

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值