溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

OpenGL實現不規(guī)則區(qū)域填充算法

發(fā)布時間:2020-08-29 03:06:24 來源:腳本之家 閱讀:453 作者:弱花3kou 欄目:編程語言

本文實例為大家分享了OpenGL實現不規(guī)則區(qū)域填充算法,供大家參考,具體內容如下

一、簡單遞歸

利用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. 將上述填充區(qū)段的左右端點的橫坐標分別記為xleft和xright.
5. 在與當前掃描線相鄰的上下兩條掃描線的[xleft,xright]范圍內進行檢查,看看是否全部為邊界像素或已填充像素,若存在著非邊界且未填充的像素,那么將該區(qū)段的最右端像素作為種子點入棧。

OpenGL實現不規(guī)則區(qū)域填充算法

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);//二維視景區(qū)域
 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();

}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI