【C#】在一个给定的宽、高范围内,获取到该多边形内部的所有坐标集合?
创始人
2024-12-16 13:04:57
0

问题点

使用C#语言在一个给定的宽、高范围内,获取到该多边形内部的所有坐标集合?

这个多边形可能存在交叉及互相重叠部分

图像的宽、高可以定义为:2000*2000

多边形坐标集合:Point[] polygon_points = new Point[] { new Point(992, 1461), new Point(1616, 925), new Point(1706, 1816), new Point(1155, 948), new Point(1986, 1216), new Point(996, 1956), new Point(811, 1031) };

 

解决方案一:针对整体的图像宽高扫描(可能耗时)

要在 C# 中实现从一个多边形中获取其在给定宽度和高度范围内的所有整数坐标点,你可以使用一种称为“扫描线算法”(Scanline Algorithm)的方法。这种方法通过水平扫描多边形的每一条边,来确定哪些像素应该被填充。但是,直接用纯数学方法实现可能会比较复杂,特别是对于复杂的多边形。

下面是一个简化版的示例代码,它使用了 System.Drawing 命名空间中的 GraphicsPathRegion 类来帮助我们找到多边形内部的点。请注意,这个示例假设你在一个 Windows Forms 应用程序中工作,因为 System.Drawing 主要用于图形处理。

using System; using System.Drawing; using System.Collections.Generic;  class PolygonPointsFinder {     public static List GetPolygonInteriorPoints(Point[] polygon_points, int width, int height)     {         var interiorPoints = new List();                  using (var graphicsPath = new GraphicsPath())         {             graphicsPath.AddPolygon(polygon_points);                          using (var region = new Region(graphicsPath))             {                 for (int y = 0; y < height; y++)                 {                     for (int x = 0; x < width; x++)                     {                         if (region.IsVisible(x, y))                         {                             interiorPoints.Add(new Point(x, y));                         }                     }                 }             }         }          return interiorPoints;     } }  class Program {     static void Main()     {         Point[] polygon_points = new Point[]         {             new Point(992, 1461),             new Point(1616, 925),             new Point(1706, 1816),             new Point(1155, 948),             new Point(1986, 1216),             new Point(996, 1956),             new Point(811, 1031)         };          int width = 2000; // 定义宽度         int height = 2000; // 定义高度          var points = PolygonPointsFinder.GetPolygonInteriorPoints(polygon_points, width, height);          // 打印所有内部点         foreach (var point in points)         {             Console.WriteLine($"({point.X}, {point.Y})");         }     } }

这段代码首先定义了一个 GetPolygonInteriorPoints 方法,它接收一个多边形的顶点和一个指定的宽度与高度。它创建一个 GraphicsPath 对象并添加多边形路径,然后创建一个 Region 对象来表示这个路径的区域。接下来,它遍历整个区域并检查每个点是否在区域内。如果在区域内,则将该点添加到结果列表中。

最后,Main 方法中调用了 GetPolygonInteriorPoints 并打印出所有内部点。由于多边形可能非常大,实际运行时可能需要较长的时间,尤其是当多边形的边界非常接近或超出指定的宽度和高度时。

 

解决方案二:针对多边形的最小包含矩形扫描

同方法一,在获取坐标时,先把多边形形成的路径计算出来,进而再次循环

using System; using System.Drawing; using System.Collections.Generic;  class PolygonPointsFinder {         public static List GetPolygonInteriorPoints(Point[] polygon_points)         {             var interiorPoints = new List();             using (var graphicsPath = new GraphicsPath())             {                 graphicsPath.AddPolygon(polygon_points);                 RectangleF boundsF = graphicsPath.GetBounds();                 Rectangle bounds = new Rectangle(                     (int)Math.Floor(boundsF.X),                     (int)Math.Floor(boundsF.Y),                     (int)Math.Ceiling(boundsF.Width),                     (int)Math.Ceiling(boundsF.Height)                 );                 using (var region = new Region(graphicsPath))                 {                     for (int y = bounds.Top; y < bounds.Bottom; y++)                     {                         for (int x = bounds.Left; x < bounds.Right; x++)                         {                             if (region.IsVisible(x, y))                             {                                 interiorPoints.Add(new Point(x, y));                             }                         }                     }                 }             }             return interiorPoints;         } }  class Program {     static void Main()     {         Point[] polygon_points = new Point[]         {             new Point(992, 1461),             new Point(1616, 925),             new Point(1706, 1816),             new Point(1155, 948),             new Point(1986, 1216),             new Point(996, 1956),             new Point(811, 1031)         };          int width = 2000; // 定义宽度         int height = 2000; // 定义高度          var points = PolygonPointsFinder.GetPolygonInteriorPoints(polygon_points);          // 打印所有内部点         foreach (var point in points)         {             Console.WriteLine($"({point.X}, {point.Y})");         }     } }

注意点

图像的边界处是否需要处理,看情况根据自身情况考虑

 

相关内容

热门资讯

正版授权!微信里面牌九房卡链接... 微信游戏中心:牌九房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信牌九房卡”,...
分享!微信里玩拼三张房卡在哪弄... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享...
正版授权!微信拼三张链接房卡,... 微信游戏中心:拼三张房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信拼三张房...
正版授权!微信斗牛大厅链接房卡... 微信游戏中心:炸金花房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信炸金花房...
正版授权!微信链接9人房卡牛牛... 微信游戏中心:炸金花房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信炸金花房卡...
分享!微信炸金花购买房卡方法/... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享...
正版授权!微信链接拼三张房卡充... 微信游戏中心:拼三张房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信拼三张房...
正版授权!微信链接牛牛房卡怎么... 微信游戏中心:牛牛房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信牛牛房卡”...
分享!微信斗牛怎么买房卡/牛魔... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享受...
正版授权!微信牛牛房卡游戏代理... 微信游戏中心:炸金花房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信炸金花房卡...
正版授权!微信牛牛房卡多少钱一... 微信游戏中心:炸金花房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信炸金花房...
分享!微信牛牛房卡如何购买/新... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享受...
正版授权!微信拼三张房卡15元... 微信游戏中心:拼三张房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信拼三张房...
正版授权!玩斗牛链接房卡怎么购... 微信游戏中心:斗牛房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信斗牛房卡”,...
分享!微信牛牛房卡链接在哪买的... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享受...
正版授权!微信牌九房卡找谁买,... 微信游戏中心:牌九房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信牌九房卡”...
正版授权!微信里斗牛牛的房卡哪... 微信游戏中心:炸金花房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信炸金花房...
正版授权!微信牛牛房卡购买方式... 微信游戏中心:炸金花房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信炸金花房卡...
分享!微信链接拼三张房卡怎样买... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享...
正版授权!微信拼三张房间房卡怎... 微信游戏中心:拼三张房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信拼三张房...