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);
        }
    }
}
相关推荐
微信公众号:AI创造财富2 小时前
conda create -n modelscope python=3.8 conda: command not found
开发语言·python·conda
鱼会上树cy2 小时前
空间解析几何10:三维圆弧拟合【附MATLAB代码】
开发语言·matlab
IT艺术家-rookie3 小时前
golang--channel的关键特性和行为
开发语言·后端·golang
凌肖战3 小时前
力扣网C语言编程题:三数之和
c语言·算法·leetcode
心软且酷丶5 小时前
leetcode:263. 丑数(python3解法,数学相关算法题)
python·算法·leetcode
Cyrus_柯5 小时前
C++(面向对象编程——关键字)
开发语言·c++·算法·面向对象
大龄Python青年5 小时前
C语言 函数怎样通过数组来返回多个值
c语言·开发语言
LQYYDSY5 小时前
【C语言极简自学笔记】重讲运算符
c语言·开发语言·笔记
2013编程爱好者5 小时前
C++二分查找
开发语言·c++·算法·二分查找