C#矩阵XY排序

矩阵XY快速排序

cs 复制代码
using MyVision.Script.Method;

public class MyScript : ScriptMethods
{
    //
    struct MOTIONPOSXY_S
    {
        public double Pos_x;
        public double Pos_y;
    };

    //脚本执行该方法
    public bool Process()
    {//@
        try
        {
            //脚本代码写在下方 
            List<double> PointX = GetDoubleList("斑点分析.X");
            List<double> PointY = GetDoubleList("斑点分析.Y");
            List<MOTIONPOSXY_S> PointList = new List<MOTIONPOSXY_S>();
            for (int i = 0; i < PointX.Count(); ++i)
            {
                MOTIONPOSXY_S tmp;
                tmp.Pos_x = PointX[i];
                tmp.Pos_y = PointY[i];
                PointList.Add(tmp);
            }

            //
            QuickSort(ref PointList, 0, PointX.Count() - 1);
            for (int i = 0; i < PointX.Count(); ++i)
            {
                PointX[i] = PointList[i].Pos_x;
                PointY[i] = PointList[i].Pos_y;
                //LogWarn("" + i + ":" + PointList[i].Pos_x + " " + PointList[i].Pos_y+ "", "");
            }

            //
            List<double> MPointX = new List<double>();
            List<double> MPointY = new List<double>();
            for (int i = 0; i < PointX.Count(); ++i)
            {
                MPointX.Add(PointList[i].Pos_x);
                MPointY.Add(PointList[i].Pos_y);
            }

            //
            SetDoubleList("数组定义.PointX", MPointX);
            SetDoubleList("数组定义.PointY", MPointY);

            //
            return true;
        }
        catch (Exception ex)
        {
            LogError(GetLogPre() + ex.ToString());
            return false;
        }
    }

    int Partition(ref List<MOTIONPOSXY_S> list, int low, int high)
    {
        MOTIONPOSXY_S pbase = list[low];
        while (low < high)
        {
            while (low < high && CompareMotion_PosXy(pbase, list[high]))
            {
                --high;
            }
            if (low < high)
            {
                list[low] = list[high];
            }
            while (low < high && CompareMotion_PosXy(list[low], pbase))
            {
                ++low;
            }
            if (low < high)
            {
                list[high] = list[low];
            }
        }
        list[low] = pbase;
        return low;
    }

    void QuickSort(ref List<MOTIONPOSXY_S> list, int low, int high)
    {
        if (low < high)
        {
            int pbase = Partition(ref list, low, high);
            QuickSort(ref list, low, pbase - 1);
            QuickSort(ref list, pbase + 1, high);
        }
    }

    //两个坐标比较大小const Test &v1, const Test &v2
    bool CompareMotion_PosXy(MOTIONPOSXY_S p1, MOTIONPOSXY_S p2)
    {
        double difference = p1.Pos_y - p2.Pos_y;
        difference = (difference >= 0 ? difference : (-difference));
        if (p1.Pos_y < p2.Pos_y)
        {
            if (difference < 100) return (p1.Pos_x < p2.Pos_x);
            else return true;
        }
        else if (p1.Pos_y == p2.Pos_y)
        {
            return (p1.Pos_x < p2.Pos_x);
        }
        else
        {
            if (difference < 100) return (p1.Pos_x < p2.Pos_x);
            else return false;
        }
    }
}

九点标定从中间往外排序

cs 复制代码
using MyVision.Script.Method;

public class MyScript : ScriptMethods
{
    //
    struct MOTIONPOSXY_S
    {
        public double Pos_x;
        public double Pos_y;
        public double Pos_r;
    };

    //脚本执行该方法
    public bool Process()
    {//@
        try
        {
            //脚本代码写在下方 
            List<double> PointX = GetDoubleList("斑点分析.X");
            List<double> PointY = GetDoubleList("斑点分析.Y");
            List<double> PointR = GetDoubleList("斑点分析.最大内直径");
            List<MOTIONPOSXY_S> PointList = new List<MOTIONPOSXY_S>();
            for (int i = 0; i < PointX.Count(); ++i)
            {
                MOTIONPOSXY_S tmp;
                tmp.Pos_x = PointX[i];
                tmp.Pos_y = PointY[i];
                tmp.Pos_r = PointR[i]/2;
                PointList.Add(tmp);
            }

            //
            QuickSort(ref PointList, 0, PointX.Count() - 1);
            for (int i = 0; i < PointX.Count(); ++i)
            {
                PointX[i] = PointList[i].Pos_x;
                PointY[i] = PointList[i].Pos_y;
                PointR[i] = PointList[i].Pos_r;
                LogWarn("" + i + ":" + PointList[i].Pos_x + " " + PointList[i].Pos_y + PointList[i].Pos_r + "", "");
            }

            //
            List<double> MPointX = new List<double>();
            List<double> MPointY = new List<double>();
            List<double> MPointR = new List<double>();

            //
            MPointX.Add(PointX[4]); MPointX.Add(PointX[5]);
            MPointX.Add(PointX[2]); MPointX.Add(PointX[1]); MPointX.Add(PointX[0]);
            MPointX.Add(PointX[3]);
            MPointX.Add(PointX[6]); MPointX.Add(PointX[7]); MPointX.Add(PointX[8]);

            //
            MPointY.Add(PointY[4]); MPointY.Add(PointY[5]);
            MPointY.Add(PointY[2]); MPointY.Add(PointY[1]); MPointY.Add(PointY[0]);
            MPointY.Add(PointY[3]);
            MPointY.Add(PointY[6]); MPointY.Add(PointY[7]); MPointY.Add(PointY[8]);

            //
            MPointR.Add(PointR[4]); MPointR.Add(PointR[5]);
            MPointR.Add(PointR[2]); MPointR.Add(PointR[1]); MPointR.Add(PointR[0]);
            MPointR.Add(PointR[3]);
            MPointR.Add(PointR[6]); MPointR.Add(PointR[7]); MPointR.Add(PointR[8]);

            //
            SetDoubleList("数组定义.Value0", MPointX);
            SetDoubleList("数组定义.Value1", MPointY);
            SetDoubleList("数组定义.Value2", MPointR);
            
            //
            return true;
        }
        catch (Exception ex)
        {
            LogError(GetLogPre() + ex.ToString());
            return false;
        }
    }

    int Partition(ref List<MOTIONPOSXY_S> list, int low, int high)
    {
        MOTIONPOSXY_S pbase = list[low];
        while (low < high)
        {
            while (low < high && CompareMotion_PosXy(pbase, list[high]))
            {
                --high;
            }
            if (low < high)
            {
                list[low] = list[high];
            }
            while (low < high && CompareMotion_PosXy(list[low], pbase))
            {
                ++low;
            }
            if (low < high)
            {
                list[high] = list[low];
            }
        }
        list[low] = pbase;
        return low;
    }

    void QuickSort(ref List<MOTIONPOSXY_S> list, int low, int high)
    {
        if (low < high)
        {
            int pbase = Partition(ref list, low, high);
            QuickSort(ref list, low, pbase - 1);
            QuickSort(ref list, pbase + 1, high);
        }
    }

    //两个坐标比较大小const Test &v1, const Test &v2
    bool CompareMotion_PosXy(MOTIONPOSXY_S p1, MOTIONPOSXY_S p2)
    {
        double difference = p1.Pos_y - p2.Pos_y;
        difference = (difference >= 0 ? difference : (-difference));
        if (p1.Pos_y < p2.Pos_y)
        {
            if (difference < 100) return (p1.Pos_x < p2.Pos_x);
            else return true;
        }
        else if (p1.Pos_y == p2.Pos_y)
        {
            return (p1.Pos_x < p2.Pos_x);
        }
        else
        {
            if (difference < 100) return (p1.Pos_x < p2.Pos_x);
            else return false;
        }
    }
}
相关推荐
pchmi4 分钟前
C#贪心算法
贪心算法·c#
阿正的梦工坊1 小时前
PyTorch下三角矩阵生成函数torch.tril的深度解析
人工智能·pytorch·矩阵
ekkcole2 小时前
windows使用命令解压jar包,替换里面的文件。并重新打包成jar包,解决Failed to get nested archive for entry
java·windows·jar
且听风吟ayan4 小时前
leetcode day20 滑动窗口209+904
算法·leetcode·c#
a小胡哦4 小时前
Windows、Mac、Linux,到底该怎么选?
linux·windows·macos·操作系统
不爱学习的YY酱6 小时前
MusicGPT的本地化部署与远程调用:让你的Windows电脑成为AI音乐工作站
人工智能·windows
GISer_Qing6 小时前
ASP.NET Core 8.0学习笔记(二十七)——数据迁移:Migrations深入与其他迁移命令
数据库·c#·.netcore·entityframework
web150854159357 小时前
超级详细Spring AI运用Ollama大模型
人工智能·windows·spring
追烽少年x8 小时前
C# WinForm 中的事件驱动模型
c#
CE贝多芬9 小时前
WPF的页面设计和实用功能实现
c#·wpf