基于最小坐标标准的排序点列表
Sorting points list based on a min coordinate criteria
我希望你能帮我解决这个问题。我必须做一些 "special sorting" 的点列表(x、y、z 坐标)作为更大代码的一部分。
此列表(根据定义)将:
i) 总是有一列的所有值都为零。
ii) 第一个点和最后一个点总是相同的。
列表的排序将取决于哪一列为零。我自己可以毫无问题地识别此列(请参阅代码),但是当它带有排序位时我很挣扎。首先,我必须找到满足某些特定条件的点,然后根据此重新组织列表。我已经在代码中以注释的形式解释了排序标准(请参阅第 2 和第 3 节)。
如果您能在 2) 和 3) 上提供帮助,那就太好了(我认为第一部分没问题)。
非常感谢
代码:
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
[STAThread]
public static void Main(string[] args)
{
//Populating the list
var ListPoints = new List<double[]>();
double[] P0 = { 10, 10, 0 };
double[] P1 = { 10, 0, 0 };
double[] P2 = { 0, 0, 0 };
double[] P3 = { 0, 10, 0 };
ListPoints.Add(P0);
ListPoints.Add(P1);
ListPoints.Add(P2);
ListPoints.Add(P3);
ListPoints.Add(P0);
///This list (by definition) will:
/// i) Always have a column with all the values equal zero
/// ii) The first and the last point will always be the same.
///We need to detect the column with all values = zero, because the list sorting will depend on this.
/// 1) Detect which columns has all values equal to zero using the columnZero variable
var counterX = new List<int>();
var counterY = new List<int>();
var counterZ = new List<int>();
for (int i = 0; i < ListPoints.Count - 1; i++)
{
//First column with all values equal zero
if (ListPoints[i][0] == 0 && ListPoints[i][0] == ListPoints[i + 1][0]) { counterX.Add(1); }
//Second column with all values equal zero
if (ListPoints[i][1] == 0 && ListPoints[i][1] == ListPoints[i + 1][1]) { counterY.Add(1); }
//Third column with all values equal zero
if (ListPoints[i][2] == 0 && ListPoints[i][2] == ListPoints[i + 1][2]) { counterZ.Add(1); }
}
if (counterX.Count == ListPoints.Count - 1)
{ Console.WriteLine("all points of the 1st column are zero");}
if (counterY.Count == ListPoints.Count - 1)
{ Console.WriteLine("all points of the 2nd column are zero");}
if (counterZ.Count == ListPoints.Count - 1)
{ Console.WriteLine("all points of the 3rd column are zero");}
/// 2) Now a point "Q" must be found in the list according to this:
/// 2.1/ If the first column has all values = zero:
/// Find the row index of the smallest value in the second column.
/// If there are several rows in the second column with the same minimum value go and find between those which one has the smallest value in the third column.
/// If there is only one value in the second column keep that one.
/// 2.2/ If the second column has all values = zero:
/// Find the row index of the smallest value in the first column.
/// If there are several rows in the first column with the same minimum value go and find between those which one has the smallest value in the third column.
/// If there is only one value in the first column keep that one.
/// 2.3/ If the third column has all values = zero:
/// Find the row index of the smallest value in the first column.
/// If there are several rows in the first column with the same minimum value go and find between those which one has the smallest value in the second column.
/// If there is only one value in the first column keep that one.
///
/// 3) Once this value has been found we have to put the list starting by this point "Q", then copy the previous values at the end of the list and finally add again "Q".
/// Example:The column with all values = 0 is column 3 and the generic point "Q" is the point "P2" in my list. The input is P0-P1-P2-P3-P0 but I want to have it P2-P3-P0-P1-P2.
}
}
根据您的标准,排序算法的前两点退化为一个更简单的算法。
即:无论哪一列全为0,您总是按第一列排序,然后按第二列,再按第三列升序排序。这会将您的目标项目留在列表的开头,然后您只需从原始列表中获取正确的顺序即可。
private List<double[]> SpecialSort(List<double[]> list)
{
// Make an editable duplicate of your original list and remove the duplicated array at the start (or end) of the list.
List<double[]> orig = new List<double[]>(list);
orig.RemoveAt(0);
// Copy and sort the list by 1st column, 2nd column, 3rd column.
List<double[]> copy = orig.OrderBy(p => p[0]).ThenBy(p => p[1]).ThenBy(p => p[2]).ToList();
// The first item in the copy-sorted list is your point "Q".
// Find its index in the original list.
int index = orig.IndexOf(copy[0]);
List<double[]> sorted = new List<double[]>();
// For the list count + 1 (adding point Q twice) add the original list
// objects in the correct order, starting at "point'Q'".
for (int i = 0; i <= orig.Count; i++)
{
sorted.Add(orig[(index + i) % orig.Count]);
}
return sorted;
}
然后调用为
ListPoints = this.SpecialSort(ListPoints);
我希望你能帮我解决这个问题。我必须做一些 "special sorting" 的点列表(x、y、z 坐标)作为更大代码的一部分。
此列表(根据定义)将:
i) 总是有一列的所有值都为零。
ii) 第一个点和最后一个点总是相同的。
列表的排序将取决于哪一列为零。我自己可以毫无问题地识别此列(请参阅代码),但是当它带有排序位时我很挣扎。首先,我必须找到满足某些特定条件的点,然后根据此重新组织列表。我已经在代码中以注释的形式解释了排序标准(请参阅第 2 和第 3 节)。
如果您能在 2) 和 3) 上提供帮助,那就太好了(我认为第一部分没问题)。
非常感谢
代码:
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
[STAThread]
public static void Main(string[] args)
{
//Populating the list
var ListPoints = new List<double[]>();
double[] P0 = { 10, 10, 0 };
double[] P1 = { 10, 0, 0 };
double[] P2 = { 0, 0, 0 };
double[] P3 = { 0, 10, 0 };
ListPoints.Add(P0);
ListPoints.Add(P1);
ListPoints.Add(P2);
ListPoints.Add(P3);
ListPoints.Add(P0);
///This list (by definition) will:
/// i) Always have a column with all the values equal zero
/// ii) The first and the last point will always be the same.
///We need to detect the column with all values = zero, because the list sorting will depend on this.
/// 1) Detect which columns has all values equal to zero using the columnZero variable
var counterX = new List<int>();
var counterY = new List<int>();
var counterZ = new List<int>();
for (int i = 0; i < ListPoints.Count - 1; i++)
{
//First column with all values equal zero
if (ListPoints[i][0] == 0 && ListPoints[i][0] == ListPoints[i + 1][0]) { counterX.Add(1); }
//Second column with all values equal zero
if (ListPoints[i][1] == 0 && ListPoints[i][1] == ListPoints[i + 1][1]) { counterY.Add(1); }
//Third column with all values equal zero
if (ListPoints[i][2] == 0 && ListPoints[i][2] == ListPoints[i + 1][2]) { counterZ.Add(1); }
}
if (counterX.Count == ListPoints.Count - 1)
{ Console.WriteLine("all points of the 1st column are zero");}
if (counterY.Count == ListPoints.Count - 1)
{ Console.WriteLine("all points of the 2nd column are zero");}
if (counterZ.Count == ListPoints.Count - 1)
{ Console.WriteLine("all points of the 3rd column are zero");}
/// 2) Now a point "Q" must be found in the list according to this:
/// 2.1/ If the first column has all values = zero:
/// Find the row index of the smallest value in the second column.
/// If there are several rows in the second column with the same minimum value go and find between those which one has the smallest value in the third column.
/// If there is only one value in the second column keep that one.
/// 2.2/ If the second column has all values = zero:
/// Find the row index of the smallest value in the first column.
/// If there are several rows in the first column with the same minimum value go and find between those which one has the smallest value in the third column.
/// If there is only one value in the first column keep that one.
/// 2.3/ If the third column has all values = zero:
/// Find the row index of the smallest value in the first column.
/// If there are several rows in the first column with the same minimum value go and find between those which one has the smallest value in the second column.
/// If there is only one value in the first column keep that one.
///
/// 3) Once this value has been found we have to put the list starting by this point "Q", then copy the previous values at the end of the list and finally add again "Q".
/// Example:The column with all values = 0 is column 3 and the generic point "Q" is the point "P2" in my list. The input is P0-P1-P2-P3-P0 but I want to have it P2-P3-P0-P1-P2.
}
}
根据您的标准,排序算法的前两点退化为一个更简单的算法。
即:无论哪一列全为0,您总是按第一列排序,然后按第二列,再按第三列升序排序。这会将您的目标项目留在列表的开头,然后您只需从原始列表中获取正确的顺序即可。
private List<double[]> SpecialSort(List<double[]> list)
{
// Make an editable duplicate of your original list and remove the duplicated array at the start (or end) of the list.
List<double[]> orig = new List<double[]>(list);
orig.RemoveAt(0);
// Copy and sort the list by 1st column, 2nd column, 3rd column.
List<double[]> copy = orig.OrderBy(p => p[0]).ThenBy(p => p[1]).ThenBy(p => p[2]).ToList();
// The first item in the copy-sorted list is your point "Q".
// Find its index in the original list.
int index = orig.IndexOf(copy[0]);
List<double[]> sorted = new List<double[]>();
// For the list count + 1 (adding point Q twice) add the original list
// objects in the correct order, starting at "point'Q'".
for (int i = 0; i <= orig.Count; i++)
{
sorted.Add(orig[(index + i) % orig.Count]);
}
return sorted;
}
然后调用为
ListPoints = this.SpecialSort(ListPoints);