I want to group a pointcloud based on 2 conditions
simple on Y so I wrote
pointcloudH.GroupBy(KVP => KVP.Value.Y)
where KVP is anKeyValuePair<string,System.Drawing.Point>
and now I want to group it also by X if
X == (previousX + 1)
as far as I know I should usThenBy()
but what do I have to write between the brackets?
and here an example for a better illustration what I want to achieve
Sample pointcloud
(x|y) (1|1),(2|1),(4|1),(1|2),(2|3),(3|3),(4|3),(5|8),(9|10)
after step 1. it looks like this
group1 (1|1),(2|1),(4|1)
group2 (1|2)
group3 (2|3),(3|3),(4|3)
group4 (5|8)
group5 (9|10)
after step 2. it should look like this
group1 (1|1),(2|1)
group2 (4|1)
group3 (1|2)
group4 (2|3),(3|3),(4|3)
group5 (5|8)
group6 (9|10)
current code
var Hgroup = pointcloudH.OrderBy(KVP => KVP.Value.Y) // order by Y
.GroupBy(KVP => KVP.Value.Y) // groub by Y
.ThenBy(KVP => KVP.Value.X); // group by X ???
I don't think LINQ is the best tool for this kind of job, but it can be achieved. The important part is to think of the relation between your
Point.X
and the index of the relativePoint
in thePoint.Y
group. Once you realize you want to group them byPoint.X - Index
, you can do:Note that this will probably perform worst than a regular iterative algorithm. My pointcloudH is an array, but you can just change the lambda to reflect your own list. Also, remove the
ToList()
if you want to defer execution. This was to ease the result inspection in the debugger.If you want to group all points in a
Point.Y
group regardless of their index (ie order byPoint.X
as well. AddThenBy(p => p.X)
after the firstOrderBy
clause.