C#矩阵XY排序

矩阵XY快速排序

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;
        }
    }
}

九点标定从中间往外排序

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;
        }
    }
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值