怎样对平面中的点进行顺时针或者逆时针排序

论坛 期权论坛 脚本     
匿名技术用户   2021-1-7 05:55   757   0

在二维空间中对点进行排序,可能会遇见其中有些点是垂直于X轴 ,或者垂直于Y轴 ,这样就可能对于用反正切的值去比较产生考虑不完全的影响导致结果错误。这样我提出一个好的解决方案就是在这些点中找一个新的点来作为新的坐标点在进行用反正切的值判断,相当于把说有的点进行了一个坐标轴的旋转(如果不是凸多边形,存在旋转后反有多个点而垂直x轴或者垂直y轴的情况),本人很懒没有画出图只贴出了代码。本程序是在vs2010 C#语言 XNA环境下进行的。

using System;
using Microsoft.Xna.Framework;
using System.Collections;
using System.Collections.Generic;

namespace PointSort
{
#if WINDOWS || XBOX
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// 
        public static void TrianglSort(List<Vector3> list)
        {
            List<double> atan = new List<double>();
            Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
            Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue);

            foreach (Vector3 vec in list)
            {
                if (vec.X < min.X && vec.Y < min.Y) min = vec;
                if (vec.X > max.X && vec.Y > max.Y) max = vec;
            }
            Vector3 Center = new Vector3((min.X + max.X) / 2, (min.Y + max.Y) / 2, 0);


            for (int i = 0; i < list.Count; i++)
            {
                list[i] = new Vector3(list[i].X, list[i].Y, MathHelper.ToDegrees((float)Math.Atan2(list[i].Y - Center.Y, list[i].X - Center.X)));
            }
            for (int i = 0; i < list.Count - 1; i++)
                for (int j = i + 1; j < list.Count; j++)
                {
                    if (list[i].Z < list[j].Z)
                    {
                        Vector3 temp = list[j];
                        list[j] = list[i];
                        list[i] = temp;
                    }
                }
        }
        static void Main(string[] args)
        {
            using (Game1 game = new Game1())
            {
                List<Vector3> vec = new List<Vector3>();
                vec.Add(new Vector3(1, 1, 0));
                vec.Add(new Vector3(1, -1, 0));
                vec.Add(new Vector3(-1, 0, 0));
                vec.Add(new Vector3(-1, -1, 0));
                vec.Add(new Vector3(1, -1, 0));
                TrianglSort(vec);
            }
        }
    }
#endif
}


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP