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;
        }
    }
}
相关推荐
写bug的小屁孩1 小时前
前后端交互接口(三)
运维·服务器·数据库·windows·用户界面·qt6.3
Envyᥫᩣ2 小时前
C#语言:从入门到精通
开发语言·c#
hairenjing11233 小时前
在 Android 手机上从SD 卡恢复数据的 6 个有效应用程序
android·人工智能·windows·macos·智能手机
plmm烟酒僧6 小时前
Windows下QT调用MinGW编译的OpenCV
开发语言·windows·qt·opencv
IT技术分享社区8 小时前
C#实战:使用腾讯云识别服务轻松提取火车票信息
开发语言·c#·云计算·腾讯云·共识算法
Jtti8 小时前
Windows系统服务器怎么设置远程连接?详细步骤
运维·服务器·windows
小奥超人9 小时前
PPT文件设置了修改权限,如何取消权?
windows·经验分享·microsoft·ppt·办公技巧
星沁城9 小时前
240. 搜索二维矩阵 II
java·线性代数·算法·leetcode·矩阵
△曉風殘月〆15 小时前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm
逐·風16 小时前
unity关于自定义渲染、内存管理、性能调优、复杂物理模拟、并行计算以及插件开发
前端·unity·c#