当前位置:   article > 正文

C#矩阵XY排序_c# 如何将一组点先按y值排列,再按x值排列

c# 如何将一组点先按y值排列,再按x值排列

矩阵XY快速排序

  1. using MyVision.Script.Method;
  2. public class MyScript : ScriptMethods
  3. {
  4. //
  5. struct MOTIONPOSXY_S
  6. {
  7. public double Pos_x;
  8. public double Pos_y;
  9. };
  10. //脚本执行该方法
  11. public bool Process()
  12. {//@
  13. try
  14. {
  15. //脚本代码写在下方
  16. List<double> PointX = GetDoubleList("斑点分析.X");
  17. List<double> PointY = GetDoubleList("斑点分析.Y");
  18. List<MOTIONPOSXY_S> PointList = new List<MOTIONPOSXY_S>();
  19. for (int i = 0; i < PointX.Count(); ++i)
  20. {
  21. MOTIONPOSXY_S tmp;
  22. tmp.Pos_x = PointX[i];
  23. tmp.Pos_y = PointY[i];
  24. PointList.Add(tmp);
  25. }
  26. //
  27. QuickSort(ref PointList, 0, PointX.Count() - 1);
  28. for (int i = 0; i < PointX.Count(); ++i)
  29. {
  30. PointX[i] = PointList[i].Pos_x;
  31. PointY[i] = PointList[i].Pos_y;
  32. //LogWarn("" + i + ":" + PointList[i].Pos_x + " " + PointList[i].Pos_y+ "", "");
  33. }
  34. //
  35. List<double> MPointX = new List<double>();
  36. List<double> MPointY = new List<double>();
  37. for (int i = 0; i < PointX.Count(); ++i)
  38. {
  39. MPointX.Add(PointList[i].Pos_x);
  40. MPointY.Add(PointList[i].Pos_y);
  41. }
  42. //
  43. SetDoubleList("数组定义.PointX", MPointX);
  44. SetDoubleList("数组定义.PointY", MPointY);
  45. //
  46. return true;
  47. }
  48. catch (Exception ex)
  49. {
  50. LogError(GetLogPre() + ex.ToString());
  51. return false;
  52. }
  53. }
  54. int Partition(ref List<MOTIONPOSXY_S> list, int low, int high)
  55. {
  56. MOTIONPOSXY_S pbase = list[low];
  57. while (low < high)
  58. {
  59. while (low < high && CompareMotion_PosXy(pbase, list[high]))
  60. {
  61. --high;
  62. }
  63. if (low < high)
  64. {
  65. list[low] = list[high];
  66. }
  67. while (low < high && CompareMotion_PosXy(list[low], pbase))
  68. {
  69. ++low;
  70. }
  71. if (low < high)
  72. {
  73. list[high] = list[low];
  74. }
  75. }
  76. list[low] = pbase;
  77. return low;
  78. }
  79. void QuickSort(ref List<MOTIONPOSXY_S> list, int low, int high)
  80. {
  81. if (low < high)
  82. {
  83. int pbase = Partition(ref list, low, high);
  84. QuickSort(ref list, low, pbase - 1);
  85. QuickSort(ref list, pbase + 1, high);
  86. }
  87. }
  88. //两个坐标比较大小const Test &v1, const Test &v2
  89. bool CompareMotion_PosXy(MOTIONPOSXY_S p1, MOTIONPOSXY_S p2)
  90. {
  91. double difference = p1.Pos_y - p2.Pos_y;
  92. difference = (difference >= 0 ? difference : (-difference));
  93. if (p1.Pos_y < p2.Pos_y)
  94. {
  95. if (difference < 100) return (p1.Pos_x < p2.Pos_x);
  96. else return true;
  97. }
  98. else if (p1.Pos_y == p2.Pos_y)
  99. {
  100. return (p1.Pos_x < p2.Pos_x);
  101. }
  102. else
  103. {
  104. if (difference < 100) return (p1.Pos_x < p2.Pos_x);
  105. else return false;
  106. }
  107. }
  108. }

九点标定从中间往外排序

  1. using MyVision.Script.Method;
  2. public class MyScript : ScriptMethods
  3. {
  4. //
  5. struct MOTIONPOSXY_S
  6. {
  7. public double Pos_x;
  8. public double Pos_y;
  9. public double Pos_r;
  10. };
  11. //脚本执行该方法
  12. public bool Process()
  13. {//@
  14. try
  15. {
  16. //脚本代码写在下方
  17. List<double> PointX = GetDoubleList("斑点分析.X");
  18. List<double> PointY = GetDoubleList("斑点分析.Y");
  19. List<double> PointR = GetDoubleList("斑点分析.最大内直径");
  20. List<MOTIONPOSXY_S> PointList = new List<MOTIONPOSXY_S>();
  21. for (int i = 0; i < PointX.Count(); ++i)
  22. {
  23. MOTIONPOSXY_S tmp;
  24. tmp.Pos_x = PointX[i];
  25. tmp.Pos_y = PointY[i];
  26. tmp.Pos_r = PointR[i]/2;
  27. PointList.Add(tmp);
  28. }
  29. //
  30. QuickSort(ref PointList, 0, PointX.Count() - 1);
  31. for (int i = 0; i < PointX.Count(); ++i)
  32. {
  33. PointX[i] = PointList[i].Pos_x;
  34. PointY[i] = PointList[i].Pos_y;
  35. PointR[i] = PointList[i].Pos_r;
  36. LogWarn("" + i + ":" + PointList[i].Pos_x + " " + PointList[i].Pos_y + PointList[i].Pos_r + "", "");
  37. }
  38. //
  39. List<double> MPointX = new List<double>();
  40. List<double> MPointY = new List<double>();
  41. List<double> MPointR = new List<double>();
  42. //
  43. MPointX.Add(PointX[4]); MPointX.Add(PointX[5]);
  44. MPointX.Add(PointX[2]); MPointX.Add(PointX[1]); MPointX.Add(PointX[0]);
  45. MPointX.Add(PointX[3]);
  46. MPointX.Add(PointX[6]); MPointX.Add(PointX[7]); MPointX.Add(PointX[8]);
  47. //
  48. MPointY.Add(PointY[4]); MPointY.Add(PointY[5]);
  49. MPointY.Add(PointY[2]); MPointY.Add(PointY[1]); MPointY.Add(PointY[0]);
  50. MPointY.Add(PointY[3]);
  51. MPointY.Add(PointY[6]); MPointY.Add(PointY[7]); MPointY.Add(PointY[8]);
  52. //
  53. MPointR.Add(PointR[4]); MPointR.Add(PointR[5]);
  54. MPointR.Add(PointR[2]); MPointR.Add(PointR[1]); MPointR.Add(PointR[0]);
  55. MPointR.Add(PointR[3]);
  56. MPointR.Add(PointR[6]); MPointR.Add(PointR[7]); MPointR.Add(PointR[8]);
  57. //
  58. SetDoubleList("数组定义.Value0", MPointX);
  59. SetDoubleList("数组定义.Value1", MPointY);
  60. SetDoubleList("数组定义.Value2", MPointR);
  61. //
  62. return true;
  63. }
  64. catch (Exception ex)
  65. {
  66. LogError(GetLogPre() + ex.ToString());
  67. return false;
  68. }
  69. }
  70. int Partition(ref List<MOTIONPOSXY_S> list, int low, int high)
  71. {
  72. MOTIONPOSXY_S pbase = list[low];
  73. while (low < high)
  74. {
  75. while (low < high && CompareMotion_PosXy(pbase, list[high]))
  76. {
  77. --high;
  78. }
  79. if (low < high)
  80. {
  81. list[low] = list[high];
  82. }
  83. while (low < high && CompareMotion_PosXy(list[low], pbase))
  84. {
  85. ++low;
  86. }
  87. if (low < high)
  88. {
  89. list[high] = list[low];
  90. }
  91. }
  92. list[low] = pbase;
  93. return low;
  94. }
  95. void QuickSort(ref List<MOTIONPOSXY_S> list, int low, int high)
  96. {
  97. if (low < high)
  98. {
  99. int pbase = Partition(ref list, low, high);
  100. QuickSort(ref list, low, pbase - 1);
  101. QuickSort(ref list, pbase + 1, high);
  102. }
  103. }
  104. //两个坐标比较大小const Test &v1, const Test &v2
  105. bool CompareMotion_PosXy(MOTIONPOSXY_S p1, MOTIONPOSXY_S p2)
  106. {
  107. double difference = p1.Pos_y - p2.Pos_y;
  108. difference = (difference >= 0 ? difference : (-difference));
  109. if (p1.Pos_y < p2.Pos_y)
  110. {
  111. if (difference < 100) return (p1.Pos_x < p2.Pos_x);
  112. else return true;
  113. }
  114. else if (p1.Pos_y == p2.Pos_y)
  115. {
  116. return (p1.Pos_x < p2.Pos_x);
  117. }
  118. else
  119. {
  120. if (difference < 100) return (p1.Pos_x < p2.Pos_x);
  121. else return false;
  122. }
  123. }
  124. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/305540?site
推荐阅读
相关标签
  

闽ICP备14008679号