c# - How to do a loop to sort and update list<> based on specific condition -
i have list of 3d data inside list namely c_top
the c_top
list consist of pattern orders y value in descending order , below:
//[ 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]
now, objective x value inside c_top list based on condition has same z value
for (int = 0; < c_top.count; i++) { if (i != c_top.count - 1) { // if next data , current data has same z, proceed if (c_top[i].p.z == c_top[i + 1].p.z) { // if x of next data greater current x of current data if (c_top[i + 1].p.x > c_top[i].p.x) { // select x , y value @ x+ direction xy_xpluslist = c_top.select(x => x.p.x).orderby(x => x).tolist(); } // if x of next data smaller current x of current data else if (c_top[i + 1].p.x < c_top[i].p.x) { // select x , y value @ x- direction xy_xminuslist = c_top.select(x => x.p.x).orderbydescending(x => x).tolist(); } } if (xy_xpluslist.count > 0) { //do } if (xy_xminuslist.count > 0) { //do } }
i using above loop, , above data example. due reason of c_top[1]
, c_top[2]
has different value of z. therefore, when i = 1
, the c_top[1].p.x
not saved has same z in c_top[0]
i result this
//output xy_xpluslist = { 2 } //only 1 x values c_top[0] saved xy_xminuslist = { 0 }
but result want when
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 }
in short. want consider each of data inside c_top
list.
when considering c_top[i]
:
if of data of z value inside c_top
list has same z c_top[i].p.z
, check x value inside c_top
list.
if got x value greater c_top[i].p.x
, x added xy_xpluslist
if got x value smaller c_top[i].p.x
, x added xy_minuslist
is there linq way on doing it?
something after sort same z, check x inside list, , add xy_xpluslist
explanation update
in each of coordinate in list, first check @ same z field or not. if same z field, check x.
if x greater current x coordinate, add x xpluslist
if x smaller current x coordinate, add x xminuslist
*i repeat above process of coordinate inside list.
explanation update2
imaging tip of pyramid polygon above 1 coordinate in c_top list
to draw pyramid polygon, need coordinate of tip, coordinate of rhs tip , coordinate of lhs tip, coordinate of front, , coordinate back.
so, now, question. have stored multiple tip coordinate inside c_top list. want check coordinates in rhs tip (in +x direction) , lhs tip (in -x direction). , same front , coordinates.
imaging have multiple tip coordinate arranged in same z field different height. want connected of tip respect highest tip coordinate show pink color line below:
update: answer question myself , question shall closed
//group z coordinate throughout v_tip list var groupedz = p in v_tip group p p.p.z q select q; foreach (var groupz in groupedz) { //order groupz y coordinate descending order var samezlist = groupz.orderbydescending(y => y.p.y).thenby(x => x.p.x).tolist(); //since list order descending w.r.t y coordinate, samezlist[0] peak coordinate in groupz var peakz = samezlist[0]; // @ same z, mean @ xy plane (int = 1; < samezlist.count; i++) { // mean x @ rhs peak if (peakz.p.x < samezlist[i].p.x) { // add x , y coordinates in rhs direction peak (x+ direction peak xy_xpluslist.add(samezlist[i].p.x); xy_ypluslist.add(samezlist[i].p.y); } else // x @ lhs peak { // add x , y coordinates in lhs direction peak (x- direction peak xy_xminuslist.add(samezlist[i].p.x); xy_yminuslist.add(samezlist[i].p.y); } } }
i'm not sure i'm 100% on question... how's work you?
public void dowork(list<int[]> c_top) { var distinctzvalues = c_top.select(p => p[2]); // gets 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 int[] c_top z == z foreach (int[] coord in coords) if (coord[0] > coord[1]) xy_minus.add(coord); else xy_plus.add(coord); } }
or, after further review, might looking unique lists based on z value...
public void dowork2(list<int[]> c_top) { var distinctzvalues = c_top.select(p => p[2]); // gets 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 int[] c_top 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); } }
Comments
Post a Comment