判断点是否在面
1、代码
using System; using System.Collections.Generic; namespace ToolClass { class Program { static void Main(string[] args) { List<Point> polygonData = new List<Point> { new Point { X=0, Y=0 }, new Point { X=1, Y=0 }, new Point { X=0, Y=1 }, new Point { X=0, Y=0 }, }; Point testPoint1 = new Point { X = 0.2, Y = 0.2 }; Point testPoint2 = new Point { X = 0.9, Y = 0.1 }; if (IsPolygon(polygonData)) { Console.WriteLine("结果一:" + IsPointInPolygon(GetPolygonToLines(polygonData), testPoint1)); Console.WriteLine("结果二:" + IsPointInPolygon(GetPolygonToLines(polygonData), testPoint2)); } else { Console.WriteLine("面数据异常"); } Console.ReadKey(); } /// <summary> /// 判断点是否在面 /// </summary> /// <param name="lines"></param> /// <param name="point"></param> /// <returns></returns> public static bool IsPointInPolygon(List<Line> lines, Point point) { var count = 0; foreach (var item in lines) { if (point.Y < item.StartY != point.Y < item.EndY) if (point.X < (point.Y - item.StartY) * (item.EndX - item.StartX) / (item.EndY - item.StartY) + item.StartX) count++; } if ((count + 1) % 2 == 0) return true; else return false; } /// <summary> /// 面转线 /// </summary> /// <param name="points"></param> /// <returns></returns> public static List<Line> GetPolygonToLines(List<Point> points) { List<Line> lines = new List<Line>(); for (int i = 0; i < points.Count; i++) { Line line = new Line(); line.StartX = points[i].X; line.StartY = points[i].Y; if (i == points.Count-1) { line.EndX = points[0].X; line.EndX = points[0].Y; } else { line.EndX = points[i + 1].X; line.EndY = points[i + 1].Y; } lines.Add(line); } return lines; } /// <summary> /// 判断是否是个面数据 /// </summary> /// <param name="polygonData"></param> /// <returns></returns> public static bool IsPolygon(List<Point> polygonData) { var pointCount = polygonData.Count; if (pointCount < 3) return false; if (polygonData[0].X != polygonData[pointCount - 1].X || polygonData[0].Y != polygonData[pointCount - 1].Y) return true; return false; } public class Point { public double X { get; set; } public double Y { get; set; } } public class Line { public double StartX { get; set; } public double StartY { get; set; } public double EndX { get; set; } public double EndY { get; set; } } } }
2、注释事项
一些特殊情况未处理,比如在面得边线上
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!