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);
        }
    }
}
相关推荐
while(1){yan}18 分钟前
基于IO流的三个小程序
java·开发语言·青少年编程
木头左18 分钟前
自适应门限动态调整算法在量化交易策略中的应用
算法
deepdata_cn18 分钟前
非线性规划(NLP)算法
算法
王燕龙(大卫)19 分钟前
rust:线程
开发语言·rust
Dev7z25 分钟前
基于MATLAB的零件表面缺陷检测系统设计与实现
开发语言·人工智能·matlab
TL滕28 分钟前
从0开始学算法——第十五天(滑动窗口)
笔记·学习·算法
@小码农28 分钟前
2025年全国青少年信息素养大赛 Gandi编程 小低组初赛真题
数据结构·人工智能·算法·蓝桥杯
CoderYanger28 分钟前
贪心算法:7.最长连续递增序列
java·算法·leetcode·贪心算法·1024程序员节
曼巴UE536 分钟前
UE C++ 字符串的操作
java·开发语言·c++
鹿角片ljp41 分钟前
力扣104.求二叉树最大深度:递归和迭代
算法·leetcode·二叉树·递归