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

ops/2024/9/24 21:18:01/

问题点

使用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<Point> GetPolygonInteriorPoints(Point[] polygon_points, int width, int height){var interiorPoints = new List<Point>();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<Point> GetPolygonInteriorPoints(Point[] polygon_points){var interiorPoints = new List<Point>();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})");}}
}

注意点

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

 


http://www.ppmy.cn/ops/85897.html

相关文章

FastAPI集成mongodb的增删改查样例

样例如下 import os from typing import Optional, Listfrom fastapi import FastAPI, Body, HTTPException, status from fastapi.responses import Response from pydantic import ConfigDict, BaseModel, Field, EmailStr from pydantic.functional_validators import Befo…

HTML开发小技巧:根据用户浏览器的分辨率调整控件的大小

在Html页面开发中&#xff0c;我们通常会用Style进行控件的宽度高度进行控件的格式设置&#xff0c;如果直接设置像素的话&#xff0c;无法根据用户的浏览器进行宽高的适配&#xff0c;所以我们要做到根据实际使用的浏览器进行控件大小的自动调整&#xff0c;以下是几种控件自动…

100个python的基本语法知识【下】

50. 压缩文件&#xff1a; import zipfilewith zipfile.ZipFile("file.zip", "r") as zip_ref:zip_ref.extractall("extracted")51. 数据库操作&#xff1a; import sqlite3conn sqlite3.connect("my_database.db") cursor conn.c…

网络安全-华为华三交换机防火墙日志解析示例

DEF_SYSLOG_SWITCH_HUAWEI.py 华为交换机日志解析示例 # -*- coding: utf8 -*- import time from DEF_COLOR import * ## 终端显示颜色def 时间戳_2_时间文本(时间戳, 时间文本格式%Y-%m-%d %H:%M:%S):#时间文本格式 %Y-%m-%d %H:%M:%S时间类 time.localtime(时间戳)时间…

快速安装torch-gpu和Tensorflow-gpu(自用,Ubuntu)

要更详细的教程可以参考Tensorflow PyTorch 安装&#xff08;CPU GPU 版本&#xff09;&#xff0c;这里是有基础之后的快速安装。 一、Pytorch 安装 conda create -n torch_env python3.10.13 conda activate torch_env conda install cudatoolkit11.8 -c nvidia pip ins…

go 并发

一、问题 1.1描述 我想要检测一组url的运行状态&#xff0c;如果ok则返回true&#xff0c;结果返回的结果是空的 func CheckWebsites(wc WebsiteChecker, urls []string) map[string]bool {results : make(map[string]bool)for _, url : range urls {go func() {results[url…

MongoDB教程(二十一):MongoDB大文件存储GridFS

&#x1f49d;&#x1f49d;&#x1f49d;首先&#xff0c;欢迎各位来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里不仅可以有所收获&#xff0c;同时也能感受到一份轻松欢乐的氛围&#xff0c;祝你生活愉快&#xff01; 文章目录 引言一、GridFS…

外卖霸王餐系统架构怎么选?

在当今日益繁荣的外卖市场中&#xff0c;外卖霸王餐作为一种独特的营销策略&#xff0c;受到了众多商家的青睐。然而&#xff0c;要想成功实施外卖霸王餐活动&#xff0c;一个安全、稳定且高效的架构选择至关重要。本文将深入探讨外卖霸王餐架构的选择&#xff0c;以期为商家提…