• 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
    {
        ///


        /// 洪水填充算法
        ///

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

    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. namespace Legalsoft.Truffer.Algorithm
    5. {
    6.     ///
    7.     /// 洪水填充算法
    8.     ///
    9.     public static partial class Algorithm_Gallery
    10.     {
    11.         private static void Fill_4Directions(int[,] screen, int x, int y, int prevC, int newC)
    12.         {
    13.             int M = screen.GetLength(0);
    14.             int N = screen.GetLength(1);
    15.             if (x < 0 || x >= M || y < 0 || y >= N)
    16.             {
    17.                 return;
    18.             }
    19.             if (screen[x, y] != prevC)
    20.             {
    21.                 return;
    22.             }
    23.             screen[x, y] = newC;
    24.             Fill_4Directions(screen, x + 1, y, prevC, newC);
    25.             Fill_4Directions(screen, x - 1, y, prevC, newC);
    26.             Fill_4Directions(screen, x, y + 1, prevC, newC);
    27.             Fill_4Directions(screen, x, y - 1, prevC, newC);
    28.         }
    29.         public static void Flood_Fill(int[,] screen, int x, int y, int newC)
    30.         {
    31.             int prevC = screen[x, y];
    32.             if (prevC == newC)
    33.             {
    34.                 return;
    35.             }
    36.             Fill_4Directions(screen, x, y, prevC, newC);
    37.         }
    38.     }
    39. }

  • 相关阅读:
    【复习整理归纳】| C++面经(内存管理)
    品牌低价的形式有哪些
    使用信号量实现简单双向同步
    Maven安装与配置完整教程
    csv文件导入mysql指定表中
    RestCloud ETL社区 八月精选问答
    产品经理进阶:如何写商业计划书?
    SpringBoot:事务的操作(动力)
    iwebsec靶场 代码执行关卡通关笔记
    声纹模型-2020:ECAPA-TDNN
  • 原文地址:https://blog.csdn.net/beijinghorn/article/details/124648562