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 代码格式

cs 复制代码
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);
        }
    }
}
相关推荐
摆烂仙君1 小时前
小米2025年校招笔试真题手撕(二)
算法
星释3 小时前
Mac Python 安装依赖出错 error: externally-managed-environment
开发语言·python·macos
小迅先生4 小时前
AI开发 | Web API框架选型-FastAPI
开发语言·python·fastapi
五花肉村长4 小时前
Linux-读者写著问题和读写锁
linux·运维·服务器·开发语言·数据库·visualstudio
biubiubiu07064 小时前
windows中JDK切换版本
java·开发语言
xin007hoyo5 小时前
算法笔记·数学·最大公约数
笔记·算法
丶Darling.5 小时前
Day126 | 灵神 | 二叉树 | 层数最深的叶子结点的和
数据结构·c++·算法·二叉树·深度优先
ALex_zry5 小时前
Go核心特性与并发编程
开发语言·后端·golang
yuanpan5 小时前
CMake创建C++项目与npm创建nodejs项目异曲同工
开发语言·c++·npm
南瓜胖胖5 小时前
R语言科研编程-柱状图
开发语言·r语言