• 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. }

  • 相关阅读:
    没有废话-MySQL,MyBatis 动态参数添加序号值(默认递增或根据内容进行递增)
    Java筑基28-泛型
    什么是RESTful API,Spring MVC如何支持RESTful架构
    框架分析(10)-SQLAlchemy
    【学习OpenCV4】OpenCV入门精讲(C++/Python双语教学)
    学信息系统项目管理师第4版系列28_组织级项目管理和量化项目管理
    Chrome浏览器:CORS 错误
    Node学习十四 —— 使用node创建HTTP请求
    AGV 控制Python
    GPS定位系统,GPSBDpro-远程车辆视频录像回放
  • 原文地址:https://blog.csdn.net/beijinghorn/article/details/124648562