C# 如何循环排序和更新列表<&燃气轮机;根据具体情况
我在一个列表中有一个3D数据列表,即C# 如何循环排序和更新列表<&燃气轮机;根据具体情况,c#,linq,list,3d,C#,Linq,List,3d,我在一个列表中有一个3D数据列表,即c\u Top c_Top列表由一个模式组成,其中y值按降序排列,如下所示: //[ x, y, z] [ 2, 10, 1] //c_Top[0] [ 7, 8, 1] //c_Top[1] [ 1, 7, 2] //c_Top[2] [ 3, 6, 1] //c_Top[3] [ 4, 6, 2] //c_Top[4] . . . . .
c\u Top
c_Top
列表由一个模式组成,其中y值按降序排列,如下所示:
//[ x, y, z]
[ 2, 10, 1] //c_Top[0]
[ 7, 8, 1] //c_Top[1]
[ 1, 7, 2] //c_Top[2]
[ 3, 6, 1] //c_Top[3]
[ 4, 6, 2] //c_Top[4]
. .
. .
. //c_Top[cTop.Count - 1]
现在,我的目标是根据c_Top list中具有相同z值的条件获取所有x值
for (int i = 0; i < c_Top.Count; i++)
{
if (i != c_Top.Count - 1)
{
// if the next data and the current data has same z, then proceed
if (c_Top[i].p.Z == c_Top[i + 1].p.Z)
{
// if the x of next data is greater than current x of current data
if (c_Top[i + 1].p.X > c_Top[i].p.X)
{
// Select all the x and y value at X+ direction
xy_xplusList = c_Top.Select(x => x.p.X).OrderBy(x => x).ToList();
}
// if the x of next data is smaller than current x of current data
else if (c_Top[i + 1].p.X < c_Top[i].p.X)
{
// Select all the x and y value at X- direction
xy_xminusList = c_Top.Select(x => x.p.X).OrderByDescending(x => x).ToList();
}
}
if (xy_xplusList.Count > 0)
{
//Do something
}
if (xy_xminusList.Count > 0)
{
//Do something
}
}
但实际上我想要的结果是这样的
c_Top[0] // Output xy_xplusList = { 2, 3 }
// xy_xminusList = { 0 }
c_Top[1] // Output xy_xplusList = { 0 }
// xy_xminusList = { 3 }
c_Top[2] // Output xy_xplusList = { 1 , 4 }
// xy_xminusList = { 0 }
c_Top[3] // Ouput xy_xplusList = { 7 }
// xy_xminusList = { 2 }
c_Top[4] // Ouput xy_xplusList = { 0 }
// xy_xminusList = { 1 }
简而言之。我想考虑代码里面的每一个数据。
在考虑c_Top[i]时:
如果c_Top
列表中z值的所有数据与c_Top[i].p.z
具有相同的z,则它将检查c_Top
列表中的所有x值
如果得到的x值大于c\u Top[i].p.x
,则x将添加到xy\u xplusList
如果得到的x值小于c_Top[i].p.x
,则x将被添加到xy_minusList
有什么好办法吗
类似于使用相同的z排序后,检查列表中的所有x,并添加到xy\u xplusList
解释更新
for (int i = 0; i < c_Top.Count; i++)
{
if (i != c_Top.Count - 1)
{
// if the next data and the current data has same z, then proceed
if (c_Top[i].p.Z == c_Top[i + 1].p.Z)
{
// if the x of next data is greater than current x of current data
if (c_Top[i + 1].p.X > c_Top[i].p.X)
{
// Select all the x and y value at X+ direction
xy_xplusList = c_Top.Select(x => x.p.X).OrderBy(x => x).ToList();
}
// if the x of next data is smaller than current x of current data
else if (c_Top[i + 1].p.X < c_Top[i].p.X)
{
// Select all the x and y value at X- direction
xy_xminusList = c_Top.Select(x => x.p.X).OrderByDescending(x => x).ToList();
}
}
if (xy_xplusList.Count > 0)
{
//Do something
}
if (xy_xminusList.Count > 0)
{
//Do something
}
}
在每个坐标输入列表中,首先检查它们是否位于同一Z字段。如果它们是相同的Z域,那么我检查所有的X
如果X大于当前X坐标,则将X添加到xplusList
如果X小于当前X坐标,则将X添加到xminusList
*我对列表中的所有坐标重复上述过程
解释更新2
想象上面金字塔多边形的顶端是c_顶部列表中的一个坐标
要绘制金字塔多边形,我们需要尖端的坐标,RHS从尖端的坐标和LHS从尖端的坐标,前面的坐标和后面的坐标
现在,回到问题上来。我在c_顶部列表中存储了多个提示坐标。现在我想检查从尖端(在+X方向)到RHS的所有坐标,以及从尖端到LHS的所有坐标(在-X方向)。前后坐标也一样
成像,我有多个尖端坐标安排在同一个Z场不同的高度。我想按照最高的尖端坐标将所有尖端连接在一起,如下图所示:
更新:自己回答问题,此问题将被关闭
//Group all the Z coordinate throughout the v_Tip list
var groupedZ = from p in v_Tip
group p by p.p.Z into q
select q;
foreach (var groupZ in groupedZ)
{
//Order the groupZ with Y coordinate descending order
var SameZList = groupZ.OrderByDescending(y => y.p.Y).ThenBy(x => x.p.X).ToList();
//Since the list is order by descending w.r.t Y coordinate, the SameZList[0] will be the peak coordinate in groupZ
var peakZ = SameZList[0];
// At same Z, mean at XY plane
for (int i = 1; i < SameZList.Count; i++)
{
// mean the x is at the RHS from the peak
if (peakZ.p.X < SameZList[i].p.X)
{
// add all the X and Y coordinates in RHS direction from the peak (X+ direction from the peak
xy_xplusList.Add(SameZList[i].p.X);
xy_yplusList.Add(SameZList[i].p.Y);
}
else // the x is at the LHS from the peak
{
// add all the X and Y coordinates in LHS direction from the peak (X- direction from the peak
xy_xminusList.Add(SameZList[i].p.X);
xy_yminusList.Add(SameZList[i].p.Y);
}
}
}
//将整个v_提示列表中的所有Z坐标分组
var groupedZ=从v_尖端中的p开始
p.p.Z将p分组为q
选择q;
foreach(groupedZ中的var groupZ)
{
//按Y坐标降序排列groupZ
var SameZList=groupZ.OrderByDescending(y=>y.p.y).ThenBy(x=>x.p.x).ToList();
//由于列表是按w.r.t Y坐标降序排列的,因此SameZList[0]将是groupZ中的峰值坐标
var peakZ=SameZList[0];
//在相同的Z,XY平面上的平均值
for(int i=1;i
我不确定我是否100%同意你的问题……但这对你有什么作用
public void DoWork(List<int[]> c_Top)
{
var distinctZvalues = c_Top.Select(p => p[2]); // this gets an enumerable object of unique Z values
List<int[]> xy_minus = new List<int[]>();
List<int[]> xy_plus = new List<int[]>();
foreach (var z in distinctZvalues)
{
List<int[]> coords = c_Top.Where(p => p[2] == z).ToList(); // pull all int[] from c_Top where z == z
foreach (int[] coord in coords)
if (coord[0] > coord[1])
xy_minus.Add(coord);
else
xy_plus.Add(coord);
}
}
public void DoWork(列表c_顶部)
{
var distinctZvalues=c_Top.Select(p=>p[2]);//这将获取具有唯一Z值的可枚举对象
列表xy_减号=新列表();
列表xy_plus=新列表();
foreach(变量z在distinctzvalue中)
{
List coords=c_-Top.Where(p=>p[2]==z).ToList();//从c_-Top中提取所有int[],其中z==z
foreach(int[]坐标中的坐标)
if(坐标[0]>坐标[1])
xy_减。加(坐标);
其他的
xy_plus.添加(坐标);
}
}
或者,在进一步检查之后,您可能会根据Z值查找唯一的列表
public void DoWork2(List<int[]> c_Top)
{
var distinctZvalues = c_Top.Select(p => p[2]); // this gets an enumerable object of unique Z values
Dictionary<int, List<int[]>> xy_minus = new Dictionary<int, List<int[]>>();
Dictionary<int, List<int[]>> xy_plus = new Dictionary<int, List<int[]>>();
foreach (var z in distinctZvalues)
{
List<int[]> minus = new List<int[]>();
List<int[]> plus = new List<int[]>();
List<int[]> coords = c_Top.Where(p => p[2] == z).ToList(); // pull all int[] from c_Top where z == z
foreach (int[] coord in coords)
if (coord[0] > coord[1])
minus.Add(coord);
else
plus.Add(coord);
xy_minus.Add(z, minus);
xy_plus.Add(z, plus);
}
}
public void DoWork2(列表c_顶部)
{
var distinctZvalues=c_Top.Select(p=>p[2]);//这将获取具有唯一Z值的可枚举对象
字典xy_减号=新字典();
字典xy_plus=新字典();
foreach(变量z在distinctzvalue中)
{
列表减号=新列表();
List plus=新列表();
List coords=c_-Top.Where(p=>p[2]==z).ToList();//从c_-Top中提取所有int[],其中z==z
foreach(int[]坐标中的坐标)
if(坐标[0]>坐标[1])
减.加(坐标);
其他的
加.加(协调);
xy_减号。加(z,减号);
加上(z,加上);
}
}
我不确定我是否100%同意你的问题……但这对你有什么作用
public void DoWork(List<int[]> c_Top)
{
var distinctZvalues = c_Top.Select(p => p[2]); // this gets an enumerable object of unique Z values
List<int[]> xy_minus = new List<int[]>();
List<int[]> xy_plus = new List<int[]>();
foreach (var z in distinctZvalues)
{
List<int[]> coords = c_Top.Where(p => p[2] == z).ToList(); // pull all int[] from c_Top where z == z
foreach (int[] coord in coords)
if (coord[0] > coord[1])
xy_minus.Add(coord);
else
xy_plus.Add(coord);
}
}
public void DoWork(列表c_顶部)
{
var distinctZvalues=c_Top.Select(p=>p[2]);//这将获取具有唯一Z值的可枚举对象
列表xy_减号=新列表();
列表xy_plus=新列表();
foreach(变量z在distinctzvalue中)
{
List coords=c_-Top.Where(p=>p[2]==z).ToList();//从c_-Top中提取所有int[],其中z==z
foreach(int[]坐标中的坐标)
if(坐标[0]>坐标[1])
xy_减。加(坐标);
其他的
xy_plus.添加(坐标);
}
}
或者,在进一步审查之后,您可能会寻找唯一的列表