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);
        }
    }
}
相关推荐
橘子海全栈攻城狮几秒前
【源码+文档+调试讲解】实验室耗材管理系统springboot 094
java·开发语言·spring boot·后端·spring
范纹杉想快点毕业4 分钟前
《嵌入式硬件从入门到精通:电源 / 模电 / 数电 / 通信核心全解析》
java·开发语言·数据库·单片机·嵌入式硬件
Ryan ZX12 分钟前
【Go语言基础】Go语言开发环境搭建
开发语言·后端·golang
虹科测试测量13 分钟前
德思特干货 | 单通道、多通道衰减器与衰减矩阵:如何选择合适的衰减方案
服务器·测试工具·算法·矩阵
x***J34831 分钟前
后端服务限流算法,计数器与滑动窗口
java·开发语言
mit6.8241 小时前
py期中实验选题:实现天气预测
python·算法
xuehaikj1 小时前
YOLOv8多场景人物识别定位与改进ASF-DySample算法详解
算法·yolo·目标跟踪
二川bro1 小时前
第51节:Three.js源码解析 - 核心架构设计
开发语言·javascript·ecmascript
-大头.1 小时前
响应式编程实战:WebFlux与RSocket深度解析
java·开发语言
异步的告白1 小时前
C语言-数据结构-2-单链表程序-增删改查
c语言·开发语言·数据结构