title: "openGL实现图形学扫描线种子填充算法"

date: 2018-06-11T19:41:30+08:00

tags: ["图形学"]

categories: ["C++"]


先上效果图

白色的起始种子点

代码



#include <GL/glut.h>
#include <cmath>
#include <set>
#include <vector>
#include <unistd.h> //sleep函数
#include <iostream>
#include <algorithm> //find函数,查找vector中元素
#include <stack> using namespace std; //规格化为0.05的倍数
inline GLdouble normal(GLdouble x)
{
return (round(x * 20) / 20);
} typedef struct Point {
GLdouble x, y; Point(GLdouble a = 0, GLdouble b = 0)
{
x = a, y = b;
} // set会对插入的元素自动排序,需要重载运算符.定义排序规则
//
//
// 重载运算符的要求
// 1. 若A<B为真,则B<A为假
// 2. 若A<B,B<C --> A<C
// 3. A<A永远为假
// set中判断元素是否相等
// if(!(A<B||B<A)) --> A=B
bool operator<(const Point &a) const
{
return ((x - a.x) < -0.01 || ((x - a.x) < 0.01 && (y - a.y) < -0.01));
} bool operator==(const Point &a) const
{
return (abs(x - a.x) < 0.01 && abs(y - a.y) < 0.01);
}
} point; void drawGrid(); void initGraphBorder(); void drawGraphices(); void myDisplay(); void DDA(point A, point B); void initGraphBorder(); point first_seed;
unsigned long m;
static int n = 40;
static GLfloat pointSize = 12.5;
set<point> graphBorder; // 图形边界数组<set>可以快速查找
//set虽然可以快速查找,但却会打乱顺序,不方便实现填充动画
vector<point> graphFill; // 要填充的数组
vector<point> graphVertex; //存储图形定点数组,按顺序
stack<point> seed; //存储种子 GLdouble p = float(2.0 / n); // 每个格子的大小 //画网格坐标
void drawGrid()
{
glColor3d(1, 1, 1); //网格用白色表示
glLineWidth(1);
glBegin(GL_LINES); for (int i = 0; i <= n; i++) {
//画竖线
glVertex2d(-1 + 1.0 / n + i * 2.0 / n, -1);
glVertex2d(-1 + 1.0 / n + i * 2.0 / n, 1);
//画横线
glVertex2d(-1, -1 + 1.0 / n + i * 2.0 / n);
glVertex2d(1, -1 + 1.0 / n + i * 2.0 / n);
} glEnd();
} //DDA算法
void DDA(point A, point B)
{
cout << "连接(" << A.x << ',' << A.y << ") 到 (" << B.x << ',' << B.y << ")\n"; if (A.x > B.x) {
swap(A, B);
} A.x = normal(A.x);
A.y = normal(A.y);
B.x = normal(B.x);
B.y = normal(B.y);
double delta_x = B.x - A.x;
double delta_y = B.y - A.y;
double k = delta_y / delta_x;
double x = A.x, y = A.y; if (k > -1 && k < 1) {
//x是最大位移
//cout << k << endl;
while (true) {
if ((x - B.x) > 0.01)break; graphBorder.insert(point(normal(x), normal(y)));
x += p;
y += (p * k);
}
}
else if (k >= 1) {
//Y是最大位移
while (true) {
if ((y - B.y) > 0.01) break; graphBorder.insert(point(normal(x), normal(y)));
y += p;
x += (p / k);
}
}
else {
while (true) {
if ((B.y - y) > 0.01) break; graphBorder.insert(point(normal(x), normal(y)));
y -= p;
x -= (p / k);
}
}
} //初始化图形边界数组
void initGraphBorder()
{
// graphVertex存储图形的顶点
for (auto it = graphVertex.begin(); it != graphVertex.end(); it++) {
if (it == graphVertex.end() - 1) {
//最后一个点连接第一个点
DDA(*it, *graphVertex.begin());
}
else {
//连接it1和it2xr
DDA(*it, *(it + 1));
}
}
} /*********************************
* 1. 初始化:堆栈置空,将种子seed(x,y)入栈
* 2. 出栈: 若栈空则结束,否则将栈顶元素出栈,以y作为当前扫描线
* 3. 填充并确定新种子点所在区域:从当前种子点出发,沿y扫描线向左右方向填充
* 直到遇到边界像素。标记当前区段的左右端点坐标为Xl, Xr.
* 4. 确定新种子点:检查[Xl-1,Xr]和[Xl+1,Xr]区域,若存在非边界,未填充的
* 像素,则把每一区间的最右像素作为种子点压栈。返回第2步
**********************************/ // 沿扫描线的区域填充
// 从种子堆栈<stack>seed中取出种子点
// 把要填充的点加入<vector>graphFill
void floodFillSet()
{
while (!seed.empty()) {
point s = seed.top(); //当前种子点
seed.pop();
cout << "当前种子点 " << s.x << " " << s.y << endl;
//填充种子点及左右连续区域
point t = s, xl, xr; //填充左侧连续区域,并找到区域最左边界
while (true) {
//到达边界退出
if (t.x < -1)break;
graphFill.push_back(t);
t.x -= p;
//查找是否已填充
auto it1 = find(graphFill.begin(), graphFill.end(), t);
//查找是否是边界
auto it2 = graphBorder.find(t); if (it1 != graphFill.end() || it2 != graphBorder.end()) { //已填充或已达边界
xl = t;
xl.x += p;
break;
}
} t = s; //填充右侧区域
while (true) {
if (t.x > 1)break; graphFill.push_back(t);
t.x += p;
//查找是否已填充
auto it1 = find(graphFill.begin(), graphFill.end(), t);
//查找是否是边界
auto it2 = graphBorder.find(t); if (it1 != graphFill.end() || it2 != graphBorder.end()) { //已填充或已达边界
xr = t;
xr.x -= p;
break;
}
} //在下一行找种子点
for (int d : { 1, -1 }) {//先上再下
bool status = false; //标记一段空白区域的开始
t = xl;
t.y += (p * d); //移到下一行或上一行 while ((t.x - xr.x) < 0.07) {
auto it1 = find(graphFill.begin(), graphFill.end(), t);
auto it2 = graphBorder.find(t); //找空白区域开始点
//如果当前点是空白区域,且空白区域未开始 则s=true
if (!status && it1 == graphFill.end() && it2 == graphBorder.end()) {
status = true;
} //区域开始且遇到边界或填充区域
if (status && (it1 != graphFill.end() || it2 != graphBorder.end())) { //到达边界或已填充颜色,且左侧有空白区域
status = false;
//添加左侧一点为种子点
seed.push(point(t.x - p, t.y));
} // 到达最右区间xr,即便不是边界,只要s=true也应标注最右种子点
if (status && abs(t.x - xr.x) < 0.01) { //注意浮点数比较大小
status = false;
seed.push(t);
} t.x += p;
}
}
}
} //画待填充的图形边界
void drawGraphices()
{
glColor3f(0.5, 0.5, 0.2);
glBegin(GL_POINTS); //C++11的新特性,可以遍历vector map list or {1,2,3}
for (auto it : graphBorder) {
glVertex2d(it.x, it.y);
} glEnd();
} //画填充像素
void drawFloodFill(unsigned long m)
{
glColor3f(0.4, 0.8, 0.1);
glBegin(GL_POINTS); for (unsigned long i = 0; i < m && i < graphFill.size(); i++) {
glVertex2d(graphFill[i].x, graphFill[i].y);
//myDisplay();
} glEnd();
} //绘图
void myDisplay()
{
glClearColor(0.1f, 0.3f, 0.33f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
//画图形边界
drawGraphices();
//画m个填充像素
drawFloodFill(m);
glColor3d(1, 0, 0);
glPointSize(pointSize);
//加个白点,起始种子点
glColor3d(1, 1, 1);
glBegin(GL_POINTS);
glVertex2d(first_seed.x, first_seed.y);
glEnd();
//画网格
drawGrid();
glFlush();
} int main(int argc, char *argv[])
{
//存入要填充的图形顶点 8
//(-0.7,0.6) (0.7,0.6) (0.7,0) (0.3,0) (0.3,-0.5) (-0.3,-0.5)
// (-0.3,0) (-0.7,0)
cout << "请输入顶点坐标,(-1,1) 输入>1 结束 \n";
double x, y; while (cin >> x) {
if (x > 1)
break;
else
cin >> y; graphVertex.emplace_back(point(x, y));
} cout << "输入初始种子点\n";
cin >> x >> y;
first_seed.x = x;
first_seed.y = y;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
//初始化边界集
initGraphBorder();
//扫描线种子填充
seed.push(first_seed);
floodFillSet();
glutCreateWindow("扫描线种子填充");
glutDisplayFunc(&myDisplay); for (m = 0; m < graphFill.size(); m++) {
usleep(10000);
myDisplay();
} glutMainLoop();
return 0;
}

输入样例

-0.7 0.7
-0.35 0.7
-0.35 0
0.35 0
0.35 0.7
0.7 0.7
0.7 -0.7
-0.7 -0.7
2
0 -0.2

openGL实现图形学扫描线种子填充算法的更多相关文章

  1. CGA填充算法之种子填充算法

    CGA填充算法之种子填充算法 平面区域填充算法是计算机图形学领域的一个很重要的算法,区域填充即给出一个区域的边界 (也可以是没有边界,只是给出指定颜色),要求将边界范围内的所有象素单元都修改成指定的颜 ...

  2. 种子填充算法描述及C++代码实现

    项目需要看了种子填充算法,改进了算法主要去除面积小的部分.种子填充算法分为两种,简单的和基于扫描线的方法,简单的算法如下描述(笔者针对的是二值图像): (1)从上到下,从左到有,依次扫描每个像素: ( ...

  3. JAVA实现种子填充算法

    种子填充算法原理在网上很多地方都能找到,这篇是继上篇扫描线算法后另一种填充算法,直接上实现代码啦0.0 我的实现只是实现了种子填充算法,但是运行效率不快,如果大佬有改进方法,欢迎和我交流,谢谢! 最后 ...

  4. Atitit Seed-Filling种子填充算法attilax总结

    Atitit Seed-Filling种子填充算法attilax总结 种子填充的原理,4联通与8联通区域的选择.. 三个队列 waitProcessPixList tempPixList Proces ...

  5. UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

    UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...

  6. Open gl 的不规则图形的4联通种子递归填充和扫描线种子递归填充算法实现

    实验题目:不规则区域的填充算法 实验目的:验证不规则区域的填充算法 实验内容:利用VC与OpenGL,实现不规则区域的填充算法. 1.必做:实现简单递归的不规则区域填充算法. 2.选做:针对简单递归算 ...

  7. [OpenGL] 不规则区域的填充算法

    不规则区域的填充算法 一.简单递归 利用Dfs实现简单递归填充. 核心代码: // 简单深度搜索填充 (四连通) void DfsFill(int x, int y) { || y < || x ...

  8. 图形学入门(3)——区域填充算法(region filling)

    继续图形学之旅,我们已经解决了如何画线和画圆的问题,接下来要解决的是,如何往一个区域内填充颜色?对一个像素填充颜色只需调用SetPixel之类的函数就行了,所以这个问题其实就是:如何找到一个区域内的所 ...

  9. [计算机图形学] 基于C#窗口的Bresenham直线扫描算法、种子填充法、扫描线填充法模拟软件设计(二)

    上一节链接:http://www.cnblogs.com/zjutlitao/p/4116783.html 前言: 在上一节中我们已经大致介绍了该软件的是什么.可以干什么以及界面的大致样子.此外还详细 ...

随机推荐

  1. 移动端click事件清除

    http://blog.csdn.net/zfy865628361/article/details/49512095

  2. 201771010118《面向对象程序设计(java)》第四周学习总结

    1.实验目的与要求 (1) 理解用户自定义类的定义: (2) 掌握对象的声明: (3) 学会使用构造函数初始化对象: (4) 使用类属性与方法的使用掌握使用: (5) 掌握package和import ...

  3. cmd 命令 net start mongodb 启动不了,提示 net 不是内部命令或者外部命令

    1.要管理员的身份进入 cmd 2.右击我的电脑-->属性-->高级系统设置 3.选择高级-->环境变量 4.找到系统变量-->Path-->编辑 5.把 C:\wind ...

  4. vue使用技巧(分页、nextTick、复制对象)

    分页技巧 v1.0+ 版本的时候使用过滤器 limitBy 实现 v2.0+ 版本的时候使用compute使用 slice 方法实现 data:{ pageNum:10 }, computed:{ n ...

  5. wordpress chronus主题 显示文章阅读数

    wordpress chronus主题 显示文章阅读数 第一步:将下面的代码拷贝到文件 /wp-content/themes/chronus/inc/template-tags.php 中 funct ...

  6. linux 服务器时间同步

    1  12 *  *  * .usr/sbin/nttpdate pool.ntp.org 分时 天 周 月 定时更同步 crontab -e 先查看是否安装 ntp这个包 没有安装的话 yum-y ...

  7. en-zh(科学技术)science and technology-2

    研究:长期不吃早餐,患心脏病风险增加87% Skipping breakfast could raise risk of heart disease by 87% Skipping breakfast ...

  8. 一、使用官方工具建立空springboot

          自己搭过springboot,看的官网,一点点自己弄,集成druid,做了些例子,从0到1弄了一下午. 当时没看到有工具可用,可以把依赖都加上,简称STS.       下载地址: htt ...

  9. 《PHP内核剖析 - 变量/内存管理》

    本文总结自: <PHP7 内核剖析 - 变量的内部实现> 一:变量的实现 - 变量是一个语言实现的基础. - 在PHP中,变量的组成部分为 变量名(zval) 变量值(zend_value ...

  10. linux sudo 运行找不到java、python命令

    在Ubuntu环境中安装好Java环境后设置环境变量:在/etc/profile中设置好了JAVA_HOME变量并引入到PATH中, 由于Ubuntu默认是不以root用户登录的,这时echo $PA ...