计算机图形学E4——OpenGL 区域种子填充
其他计算机图形学实验见 链接
系统递归栈太慢了,用stack或者queue会好一点,本质一样,都是bfs
//DFS 系统递归栈//#include<iostream>
//#include<vector>
//#include<GL/glut.h>
//using namespace std;
//int window_width = 800, window_height = 600;
//
//struct point
//{// int x, y;
// point()
// {}
// point(int xx, int yy)
// :x(xx), y(yy) {}
//};
//vector<point> vertice;
//
//float newcolor[3] = { 1, 0, 0 };
//float boundarycolor[3] = { 0, 0, 1 };
//
//void draw_a_point(int x, int y)
//{// glBegin(GL_POINTS);
// glColor3fv(newcolor);
// glVertex2f(x, y);
// glEnd();
// glFlush();
//}
//
//bool is_equal(float* a, float* b)
//{// return a[0] == b[0] && a[1] == b[1] && a[2] == b[2];
//}
//
//
//void BoundaryFill4(int x, int y)
//{// float color[3];
// glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, color);
// cout << x << "," << y << ":" << color[0] << color[1] << color[2] << endl;
// if (!is_equal(color, newcolor) && !is_equal(color, boundarycolor))
// {// draw_a_point(x, y);
// BoundaryFill4(x, y + 1);
// BoundaryFill4(x, y - 1);
// BoundaryFill4(x + 1, y);
// BoundaryFill4(x - 1, y);
// }
//}
//
//
//void mymouse(int button, int state, int x, int y)
//{// if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
// {// draw_a_point(x, window_height - y);
//
// point p(x, window_height - y);
// vertice.push_back(p);
// cout << "顶点" << vertice.size() << ": (" << x << ", " << y << ")" << endl;
// }
//
// if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
// {// glClearColor(1, 1, 1, 1);//设置绘制窗口颜色为白色
// glColor3fv(boundarycolor);
//
// glBegin(GL_LINES);
// for (int i = 0; i < vertice.size(); i++)
// {// if (i == vertice.size() - 1)//画完最后一个点,使其闭合
// {// glVertex2f(vertice[0].x, vertice[0].y);
// glVertex2f(vertice[i].x, vertice[i].y);
// }
// else
// {// glVertex2f(vertice[i].x, vertice[i].y);
// glVertex2f(vertice[i + 1].x, vertice[i + 1].y);
// }
// }
// glEnd();
// glFlush();
// vertice.clear();
// }
//
// if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
// {// cout << "center: (" << x << ", " << y << ")" << endl;
// BoundaryFill4(x, window_height - y);
// }
//}
//void KeyBoards(unsigned char key, int x, int y)
//{// if (key == 32)
// {// BoundaryFill4(x, window_height - y);
// glFlush();
// }
//}
//
//
//int main(int argc, char* argv[])
//{// cout << "左键画点,右键画点结束形成多边形" << endl;
// glutInit(&argc, argv);
// glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// glutInitWindowPosition(50, 50);
// glutInitWindowSize(window_width, window_height);
// glutCreateWindow("PolyFill");
// glMatrixMode(GL_PROJECTION);/*设置为投影类型模式和其他观察参数*/
// //glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();/*设置为投影类型模式和其他观察参数*/
// //gluOrtho2D(0, window_width, window_height, 0);/*设置为投影类型模式和其他观察参数,观察窗口的大小要与画布大小一致,所以直接设置全局变量即可*/
// gluOrtho2D(0, window_width, 0, window_height);
//
// glClearColor(1, 1, 1, 1);//设置绘制窗口颜色为白色
// glClear(GL_COLOR_BUFFER_BIT);
//
// glutMouseFunc(&mymouse);
// //glutKeyboardFunc(&KeyBoards);
// glutMainLoop();
// return 0;
//}//BFS
#include<iostream>
#include<vector>
#include<GL/glut.h>
#include<queue>
#include<stack>
using namespace std;
int window_width = 800, window_height = 600;struct point
{int x, y;point(){}point(int xx, int yy):x(xx), y(yy) {}
};
vector<point> vertice;
float newcolor[3] = { 1, 0, 0 };
float boundarycolor[3] = { 0, 0, 1 };void draw_a_point(int x, int y);
bool is_equal(float* a, float* b);
void BoundaryFill4(int x, int y);
void BoundaryFill4_Stack(int x, int y);
void mymouse(int button, int state, int x, int y);
void KeyBoards(unsigned char key, int x, int y);int main(int argc, char* argv[])
{cout << "点击鼠标左键画点;" << endl << "点击鼠标右键结束画点,形成多边形;" << endl << "点击鼠标中键确定区域填充种子点位置。" << endl;glutInit(&argc, argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);glutInitWindowPosition(50, 50);glutInitWindowSize(window_width, window_height);glutCreateWindow("区域种子填充");glMatrixMode(GL_PROJECTION);/*设置为投影类型模式和其他观察参数*///glMatrixMode(GL_MODELVIEW);glLoadIdentity();/*设置为投影类型模式和其他观察参数*///gluOrtho2D(0, window_width, window_height, 0);/*设置为投影类型模式和其他观察参数,观察窗口的大小要与画布大小一致,所以直接设置全局变量即可*/gluOrtho2D(0, window_width, 0, window_height);//上面的不可以!!!glClearColor(1, 1, 1, 1);//设置绘制窗口颜色为白色glClear(GL_COLOR_BUFFER_BIT);glutMouseFunc(&mymouse);//glutKeyboardFunc(&KeyBoards);glutMainLoop();return 0;
}void draw_a_point(int x, int y)
{glBegin(GL_POINTS);glColor3fv(newcolor);glVertex2f(x, y);glEnd();glFlush();
}
bool is_equal(float* a, float* b)
{return a[0] == b[0] && a[1] == b[1] && a[2] == b[2];
}
void BoundaryFill4(int x, int y)
{queue<point> q;q.push(point(x, y));while (!q.empty()){point now = q.front();q.pop();int nowx = now.x, nowy = now.y;float color[3];glReadPixels(nowx, nowy, 1, 1, GL_RGB, GL_FLOAT, color);if (!is_equal(color, newcolor) && !is_equal(color, boundarycolor)){draw_a_point(nowx, nowy);q.push(point(nowx, nowy + 1));q.push(point(nowx, nowy - 1));q.push(point(nowx + 1, nowy));q.push(point(nowx - 1, nowy));}}
}
void BoundaryFill4_Stack(int x, int y)
{//queue<point> q;stack < point> q;q.push(point(x, y));while (!q.empty()){point now = q.top();q.pop();int nowx = now.x, nowy = now.y;float color[3];glReadPixels(nowx, nowy, 1, 1, GL_RGB, GL_FLOAT, color);if (!is_equal(color, newcolor) && !is_equal(color, boundarycolor)){draw_a_point(nowx, nowy);q.push(point(nowx, nowy + 1));q.push(point(nowx, nowy - 1));q.push(point(nowx + 1, nowy));q.push(point(nowx - 1, nowy));}}
}
void mymouse(int button, int state, int x, int y)
{if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){draw_a_point(x, window_height - y);point p(x, window_height - y);vertice.push_back(p);cout << "顶点" << vertice.size() << ": (" << x << ", " << y << ")" << endl;}if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN){glClearColor(1, 1, 1, 1);//设置绘制窗口颜色为白色glColor3fv(boundarycolor);glBegin(GL_LINES);for (int i = 0; i < vertice.size(); i++){if (i == vertice.size() - 1)//画完最后一个点,使其闭合{glVertex2f(vertice[0].x, vertice[0].y);glVertex2f(vertice[i].x, vertice[i].y);}else{glVertex2f(vertice[i].x, vertice[i].y);glVertex2f(vertice[i + 1].x, vertice[i + 1].y);}}glEnd();glFlush();vertice.clear();}if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN){cout << "center: (" << x << ", " << y << ")" << endl;//BoundaryFill4(x, window_height - y);BoundaryFill4_Stack(x, window_height - y);}
}
void KeyBoards(unsigned char key, int x, int y)
{if (key == 32){BoundaryFill4(x, window_height - y);glFlush();}
}
计算机图形学E4——OpenGL 区域种子填充相关推荐
- c语言图形学扫描线填充算法,《计算机图形学》OpenGL实现扫描线填充算法
顾名思义啊,就是在OpenGL中用扫描填充算法画一个稍微复杂的图形: #include #include #include #include #define COLOR_NEW 1.0,0.0,0.0 ...
- 计算机图形学及OpenGL简介摘记
计算机图形学(Computer Graphics)是研究怎样用计算机表示.生成.处理和显示图形的一门学科.图形的具体应用范围很广,但是按基本的处理技术划分只有两类.一类是线条,如工程图.地图.曲线图表 ...
- 平移计算机图形学代码注释,求代码注释:计算机图形学的OpenGL画四面体。高手来吧。争取每句都注释下。谢谢...
求代码注释:计算机图形学的OpenGL画四面体.高手来吧.争取每句都注释下.谢谢 答案:3 信息版本:手机版 解决时间 2018-12-20 05:00 已解决 2018-12-20 01:58 求 ...
- 计算机图形学E5——OpenGL 扫描线填充
其他计算机图形学实验见 链接 #include<iostream> #include<gl/glut.h> #include<algorithm> #include ...
- 计算机图形学入门OpenGL
计算机图形学openGL 计算机图形学:(Computer Graphics, 简称CG) 简单得说:计算机图形学是一种使用数学算法将二维或者三维图形转化为计算机显示器所能显示的二维栅格形式的科学. ...
- 计算机图形学 第4章 多边形填充
目录 前驱知识 多边形的扫描转换 有效边表填充算法 原理 边界像素处理原则 怎么算交点 有效边 桶表与边表 桶表表示法 边缘填充算法 填充过程 在这里插入图片描述 区域填充算法/种子填充算法 种子填充 ...
- 计算机图形学E1——OpenGL 方中有圆,圆中有方,无穷尽焉
其他计算机图形学实验见 链接 使用OpenGL绘制如图所示图形 有穷的: #include <GL/glut.h> #include <math.h> #include< ...
- 【计算机图形学】OpenGL递归实现光线追踪
计算机图形学课程设计:基于面向对象的光线跟踪算法设计与实现 目录 一.前言 二.项目实现与说明 1. 数据结构设计 1.1 光线 Ray 1.2 材质 Material 1.3 光照 Light 1. ...
- 【学习日志】2022.08.19 计算机图形学、OpenGL、疼迅云游戏引擎面试、GLAD、GLFW、virtual
计算机图形学知识图谱 .学习路线 OpenGL 概述 简介 - LearnOpenGL CN (learnopengl-cn.github.io) 当前比较流行的搭配是 freeglut + glew ...
最新文章
- Go借助PProf的一次性能优化
- 一款美轮美奂的JavaScript 小项目
- linux全自动安装光盘,CentOS7全自动安装光盘制做详解
- 面向对象PHP之静态延迟绑定
- 用python庆祝生日_python, 实现朋友家人年历生日自动提醒
- oracle脱机什么意思,脱机的是什么意思
- php给网页加水印_php实现图片添加水印功能
- 重返商用计算市场,紫光计算机要做数字化转型生意
- 通达信资金净流入公式_通达信主力净流入指标公式
- 声学计算机软件,常用声学仿真软件汇总
- vb改变字形的代码是什么_VB里什么代码可以把文本框中的字改成斜体
- ONES(光盘刻录软件)单文件版V2.1.358 | ones刻录软件下载 | ones刻录软件怎么使用
- cad看图软件看图软件哪个好?
- 现场抽奖小程序_抽奖小程序助手转盘活动微信抽奖系统
- 施密特将辞任Alphabet执行董事长 转任技术顾问 | 聚焦
- Hbase的数据切分
- Kinect v2保存图像和深度图序列
- 零知开源分享-L298N模块使用 电机驱动 TT马达
- 启动Mysql时报错:mysqld_safe mysqld from pid file /usr/local/mysql/data/Linux.pid ended
- django-DIL模板自定义过滤器,自定义标签,自定义包含标签