当前位置: 首页 > news >正文

C#,计算几何,计算机图形学(Computer Graphics)洪水填充算法(Flood Fill Algorithm)与源代码

1 泛洪填充算法(Flood Fill Algorithm)

泛洪填充算法(Flood Fill Algorithm) ,又称洪水填充算法,是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows 自带画图软件的油漆桶功能。

2 源程序

using System;
using System.Collections;
using System.Collections.Generic;

namespace Legalsoft.Truffer.Algorithm
{
    /// <summary>
    /// 洪水填充算法
    /// </summary>
    public static partial class Algorithm_Gallery
    {
        private static void Fill_4Directions(int[,] screen, int x, int y, int prevC, int newC)
        {
            int M = screen.GetLength(0);
            int N = screen.GetLength(1);

            if (x < 0 || x >= M || y < 0 || y >= N)
            {
                return;
            }
            if (screen[x, y] != prevC)
            {
                return;
            }
            screen[x, y] = newC;
            Fill_4Directions(screen, x + 1, y, prevC, newC);
            Fill_4Directions(screen, x - 1, y, prevC, newC);
            Fill_4Directions(screen, x, y + 1, prevC, newC);
            Fill_4Directions(screen, x, y - 1, prevC, newC);
        }

        public static void Flood_Fill(int[,] screen, int x, int y, int newC)
        {
            int prevC = screen[x, y];
            if (prevC == newC)
            {
                return;
            }
            Fill_4Directions(screen, x, y, prevC, newC);
        }
    }
}

3 代码格式

using System;
using System.Collections;
using System.Collections.Generic;namespace Legalsoft.Truffer.Algorithm
{/// <summary>/// 洪水填充算法/// </summary>public static partial class Algorithm_Gallery{private static void Fill_4Directions(int[,] screen, int x, int y, int prevC, int newC){int M = screen.GetLength(0);int N = screen.GetLength(1);if (x < 0 || x >= M || y < 0 || y >= N){return;}if (screen[x, y] != prevC){return;}screen[x, y] = newC;Fill_4Directions(screen, x + 1, y, prevC, newC);Fill_4Directions(screen, x - 1, y, prevC, newC);Fill_4Directions(screen, x, y + 1, prevC, newC);Fill_4Directions(screen, x, y - 1, prevC, newC);}public static void Flood_Fill(int[,] screen, int x, int y, int newC){int prevC = screen[x, y];if (prevC == newC){return;}Fill_4Directions(screen, x, y, prevC, newC);}}
}

http://www.lryc.cn/news/305274.html

相关文章:

  • C# 实现网页内容保存为图片并生成压缩包
  • C#_事件简述
  • C语言:指针(一)
  • 【leetcode刷题之路】面试经典150题(3)——哈希表+区间
  • 群晖NAS DSM7.2.1安装宝塔之后无法登陆账号密码问题解决
  • 9、使用 ChatGPT 的 GPT 制作自己的 GPT!
  • 企业微信应用开发:使用Cpolar域名配置进行本地接口回调的调试指南
  • js 可选链运算符(?.)空值合并运算符(??)逻辑空赋值运算符(??=)
  • vue 手势解锁功能
  • 介绍 CI / CD
  • Stable Diffusion 3 Early Preview发布
  • 【解决(几乎)任何机器学习问题】:特征选择
  • 24 双非计算机秋招总结
  • 用友NC65与用友NCC对接集成NC65-凭证列表查询打通凭证新增
  • 【初中生讲机器学习】12. 似然函数和极大似然估计:原理、应用与代码实现
  • 【达梦数据库】查看pesg回滚段信息的视图和SQL
  • UML---活动图
  • 编程笔记 Golang基础 018 常量与变量
  • 如何使用Douglas-042为威胁搜索和事件应急响应提速
  • 华为配置WLAN AC和AP之间VPN穿越示例
  • 跨语言的序列化与反序列化
  • 软考-中级-系统集成2023年综合知识(三)
  • 五、使用脚手架
  • 抛弃chatgpt,使用微软的Cursor提升coding效率
  • uniapp插件uViewplus的使用(涉及TS下的问题)
  • google浏览器chrome无法访问localhost等本地虚拟域名的解决方法
  • (2.2w字)前端单元测试之Jest详解篇
  • 【C++私房菜】面向对象中的多态
  • (done) 什么是特征值和特征向量?如何求特征值的特征向量 ?如何判断一个矩阵能否相似对角化?
  • [rust] 11 所有权