边界填充算法讲解

Boundary fill is the algorithm used frequently in computer graphics to fill a desired color inside a closed polygon having the same boundary color for all of its sides.

边界填充是在计算机图形学中经常使用的算法,用于在其所有边都具有相同边界颜色的封闭多边形内填充所需颜色。

The most approached implementation of the algorithm is a stack-based recursive function.

该算法最接近的实现是基于堆栈的递归函数。

这个怎么运作: (How it works:)

The problem is pretty simple and usually follows these steps:

该问题非常简单,通常按照以下步骤操作:

  1. Take the position of the starting point and the boundary color.取起点的位置和边界颜色。
  2. Decide wether you want to go in 4 directions (N, S, W, E) or 8 directions (N, S, W, E, NW, NE, SW, SE).确定您要沿4个方向(N,S,W,E)还是8个方向(N,S,W,E,NW,NE,SW,SE)行驶。
  3. Choose a fill color.选择一种填充颜色。
  4. Travel in those directions.朝那些方向旅行。
  5. If the pixel you land on is not the fill color or the boundary color , replace it with the fill color.如果您着陆的像素不是填充颜色或边界颜色,则将其替换为填充颜色。

Repeat 4 and 5 until you’ve been everywhere within the boundaries.

重复4和5,直到到达边界内的任何地方。

某些限制: (Certain Restrictions:)

The boundary color should be the same for all the edges of the polygon.

多边形的所有边缘的边界颜色应相同。

The starting point should be within the polygon.

起点应在多边形内。

代码段: (Code Snippet:)

void boundary_fill(int pos_x, int pos_y, int boundary_color, int fill_color)
{
current_color= getpixel(pos_x,pos_y);  //get the color of the current pixel position
if( current_color!= boundary_color || currrent_color != fill_color) // if pixel not already filled or part of the boundary then
{    putpixel(pos_x,pos_y,fill_color);  //change the color for this pixel to the desired fill_colorboundary_fill(pos_x + 1, pos_y,boundary_color,fill_color);  // perform same function for the east pixelboundary_fill(pos_x - 1, pos_y,boundary_color,fill_color);  // perform same function for the west pixelboundary_fill(pos_x, pos_y + 1,boundary_color,fill_color);  // perform same function for the north pixelboundary_fill(pos_x, pos_y - 1,boundary_color,fill_color);  // perform same function for the south pixel
}
}

From the given code you can see that for any pixel that you land on, you first check whether it can be changed to the fill_color and then you do so for its neighbours till all the pixels within the boundary have been checked.

从给定的代码中可以看到,对于您着陆的任何像素,首先要检查是否可以将其更改为fill_color,然后对其相邻像素进行更改,直到检查了边界内的所有像素为止。

翻译自: https://www.freecodecamp.org/news/boundary-fill-algorithm/

边界填充算法讲解

边界填充算法讲解_边界填充算法相关推荐

  1. 算法工程师_浅谈算法工程师的职业定位与发展

    随着大数据和以深度学习为代表的人工智能技术的飞速发展,算法工程师这个职业逐渐成为国内互联网行业的标配.2016年3月,谷歌旗下DeepMind公司的围棋程序"AlphaGo"战胜职 ...

  2. 聚类算法 距离矩阵_模糊聚类算法

    模糊聚类算法 1.如何理解模糊聚类 事物间的界线,有些是明确的,有些则是模糊的.当聚类涉及到事物之间的模糊界线时,需要运用模糊聚类分析方法. 如何理解模糊聚类的"模糊"呢:假设有两 ...

  3. vrp 节约算法 c++_数据结构和算法(Golang实现)(8.1)基础知识-前言

    基础知识 学习数据结构和算法.我们要知道一些基础的知识. 一.什么是算法 算法(英文algorithm)这个词在中文里面博大精深,表示算账的方法,也可以表示运筹帷幄的计谋等.在计算机科技里,它表示什么 ...

  4. 扫描线填充算法代码_手写算法并记住它:计数排序

    对于经典算法,你是否也遇到这样的情形:学时觉得很清楚,可过阵子就忘了? 本系列文章就尝试解决这个问题. 研读那些排序算法,细品它们的名字,其实都很贴切. 比如计数排序,所谓"计数" ...

  5. python数据结构和算法讲解_【学习】python数据结构和算法

    二.算法分析 2.2 什么是算法分析 大O表示法 image.png 2.3 python数据结构的性能 列表 image.png 字典 image.png 说一下list[index]的o(1)原理 ...

  6. java五子棋的重要算法讲解_[Java五子棋小游戏-Ai算法精讲以及实现]-02--高级算法初步...

    高级算法初步 走对自己利益最大的路 它有难了不要慌,抛弃它,以这盘棋局的胜利为他报仇 碰撞检测与跨步算法原理图 反向计算权重 每次计算完权重,都要进行一次反向权重计算 权重值 : 两侧的权重值相加 权 ...

  7. svm通俗讲解_通俗易懂--SVM算法讲解(算法+案例)

    1.SVM讲解 SVM是一个很复杂的算法,不是一篇博文就能够讲完的,所以此篇的定位是初学者能够接受的程度,并且讲的都是SVM的一种思想,通过此篇能够使读着会使用SVM就行,具体SVM的推导过程有一篇博 ...

  8. 机器学习经典算法实践_服务机器学习算法的系统设计-不同环境下管道的最佳实践

    机器学习经典算法实践 "Eureka"! While working on a persistently difficult-to-solve problem, you disco ...

  9. 开根号的笔算算法图解_机器学习KNN算法之手写数字识别

    1.算法简介 手写数字识别是KNN算法一个特别经典的实例,其数据源获取方式有两种,一种是来自MNIST数据集,另一种是从UCI欧文大学机器学习存储库中下载,本文基于后者讲解该例. 基本思想就是利用KN ...

最新文章

  1. 【BZOJ】4430: [Nwerc2015]Guessing Camels赌骆驼
  2. 数学之美 系列九 -- 如何确定网页和查询的相关性
  3. boost::incremental_components用法的测试程序
  4. 音视频开发相关工具整理
  5. cartographer学习笔记--如何保存cartagrapher_ros建好的地图
  6. php3绕过,PHPB2B注入#3(绕过过滤)
  7. 线性代数拾遗(五):矩阵变换的应用
  8. 接口自动化测试_Python自动化测试学习路线之接口自动化测试「模块四」
  9. (五)如何训练和测试AI语言翻译系统
  10. Flume之介绍 核心组件 可靠性 恢复性
  11. java 数据类型转换的一场_Java数据类型之间的转换
  12. 【FPGA】TestBench中关于@eachvec
  13. PHPKafka 1.0 发布,支持全部 50 个 API
  14. python时间模块小结
  15. 顺序表的基本操作代码实现
  16. Word2010为图片批量插入题注
  17. Android 用 broadcast receiver组件实现音乐盒
  18. 手机号86注册不了谷歌?无法验证手机号如何100%解决!
  19. 基于51单片机简易智能家居
  20. Python基础篇(三)-- 列表、元组、字典、集合、字符串

热门文章

  1. 【Echarts】当页面宽度改变时如何使图表宽度自适应
  2. JavaScript判断对象是否为空对象或空数组
  3. mac tomcat https
  4. 小程序获取用户所在城市完整代码
  5. Swift3实现的绘制股票K线库, FastImageCache提升图片的加载和渲染速度,Chameleon颜色框架
  6. GitBook本地的安装与查看
  7. Django基础-数据分页
  8. Hadoop基础-网络拓扑机架感知及其实现
  9. JVM实用参数 GC日志
  10. WIN7 64位系统下,右下角的声音和电源图标不见的解决办法