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;
        }
    }
}
相关推荐
码农君莫笑28 分钟前
使用blazor开发信息管理系统的应用场景
数据库·信息可视化·c#·.net·visual studio
ue星空1 小时前
Windbg常用命令
windows
可喜~可乐3 小时前
C# WPF开发
microsoft·c#·wpf
云云3214 小时前
亚矩阵云手机
线性代数·智能手机·矩阵
矩阵推荐官hy147624 小时前
短视频矩阵系统种类繁多,应该如何对比选择?
人工智能·python·矩阵·流量运营
泰勒今天不想展开5 小时前
jvm接入prometheus监控
jvm·windows·prometheus
易我数据恢复大师7 小时前
怎么设置电脑密码?Windows和Mac设置密码的方法
windows·macos·电脑
666和7777 小时前
C#的单元测试
开发语言·单元测试·c#
m0_748256567 小时前
Windows 11 Web 项目常见问题解决方案
前端·windows
小码编匠8 小时前
WPF 星空效果:创建逼真的宇宙背景
后端·c#·.net