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);
        }
    }
}
相关推荐
星河队长6 分钟前
VS创建C++动态库和C#访问过程
java·c++·c#
MYX_30930 分钟前
第三章 线型神经网络
深度学习·神经网络·学习·算法
ftpeak37 分钟前
JavaScript性能优化实战
开发语言·javascript·性能优化
William_cl1 小时前
【C# MVC 前置】异步编程 async/await:从 “卡界面” 到 “秒响应” 的 Action 优化指南(附微软官方避坑清单)
microsoft·c#·mvc
一个很帅的帅哥1 小时前
JavaScript事件循环
开发语言·前端·javascript
驰羽1 小时前
[GO]gin框架:ShouldBindJSON与其他常见绑定方法
开发语言·golang·gin
程序员大雄学编程1 小时前
「用Python来学微积分」5. 曲线的极坐标方程
开发语言·python·微积分
yong99901 小时前
C#驱动斑马打印机实现包装自动打印
java·数据库·c#
坚持编程的菜鸟2 小时前
LeetCode每日一题——三角形的最大周长
算法·leetcode·职场和发展
Jose_lz2 小时前
C#开发学习杂笔(更新中)
开发语言·学习·c#