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 PointX = GetDoubleList("斑点分析.X");
            List PointY = GetDoubleList("斑点分析.Y");
            List PointList = new List();
            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 MPointX = new List();
            List MPointY = new List();
            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 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 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;
        }
    }
}

九点标定从中间往外排序

C#矩阵XY排序_第1张图片

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 PointX = GetDoubleList("斑点分析.X");
            List PointY = GetDoubleList("斑点分析.Y");
            List PointR = GetDoubleList("斑点分析.最大内直径");
            List PointList = new List();
            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 MPointX = new List();
            List MPointY = new List();
            List MPointR = new List();

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

你可能感兴趣的:(c#,矩阵,windows)