生成泰森多边形

Posted on   云起  阅读(8)  评论(0编辑  收藏  举报  
void ConvexHull(Point[] pts)
        {
            int[][] temp = new int[pts.Length][];
            for (int i = 0; i < pts.Length; i++)
            {
                temp[i] = new int[2];
                temp[i][0] = (int)(pts[i].X + pts[i].Y);
                temp[i][1] = (int)(pts[i].X - pts[i].Y);
            }

            List<int[]> ls = temp.ToList();
            int max1index = ls.FindIndex(o => o[0] == ls.Max(j => j[0]));
            int max2index = ls.FindIndex(o => o[1] == ls.Max(j => j[1]));
            int min1index = ls.FindIndex(o => o[0] == ls.Min(j => j[0]));
            int min2index = ls.FindIndex(o => o[1] == ls.Min(j => j[1]));
            Console.Write(string.Format("{0},{1},{2},{3}", max1index, min1index, max2index, min2index));
	//找出点集中,最靠近点集四至的点。为了简化操作,我将点集pts的四至角点都加入了点集中,避免了外围凸包的复杂度和Voronoi切分时考虑边界

            List<Point> tubao = new List<Point>();
            List<Point> other = new List<Point>();

	//根据最右原则,求取凸包点列表。如果像我之前将四至角点加入点集,这部分可以除去,直接构建凸包点列表
            Point Pi, Pj, Pk;

            int st = max1index, ed = max2index;
            Pk = pts[st];
            Pi = Pk;
            Pj = pts[ed];

            do
            {
                if (tubao.Count > 0)
                    Pj = Pk;

                con:
                bool right = false;
                List<Point> rightpt = new List<Point>();

                rightpt.Clear();
                for (int i = 0; i < pts.Length; i++)
                {
                    if (pts[i].Equals(Pi)) continue;
                    if (pts[i].Equals(Pj)) continue;
                    int re = getPtDirection(Pi, Pj, pts[i]);
                    if (re > 0)
                    {
                        right = true;
                        rightpt.Add(pts[i]);
                    }
                    else if (re == 0)
                    {
                        //共线
                        right = false;
                    }
                    else
                    {
                        right = false;
                    }
                }

                if (rightpt.Count == 0)
                {
                    //更改后继点
                    tubao.Add(Pi);
                    Pi = Pj;
                    continue;
                }
                else
                {
                    List<double> rlen = new List<double>(rightpt.Count);
                    foreach (var pt in rightpt)
                    {
                        rlen.Add(pt2lnDis(pt, Pi, Pj));
                    }
                    int index = rlen.FindIndex(o => o == rlen.Max());

                    Pj = rightpt[index];
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
历史上的今天:
2015-01-08 生成要素西南方向点

随笔 - 119, 文章 - 0, 评论 - 3, 阅读 - 4066

Copyright © 2025 云起
Powered by .NET 9.0 on Kubernetes

点击右上角即可分享
微信分享提示