I have a list of objects that have coordinates. The object is something like this:
private class Seats
{
public string Code { get; set; }
public long PosX { get; set; }
public long PosY { get; set; }
}
For all Seats inside the list, I need to know that they are in group of 4, in a horizontal row. For example, the list below is fine:
List<Seats> good = new List<Seats> {
new Seats {Code = "A1", PosX = 0, PosY = 0},
new Seats {Code = "A2", PosX = 1, PosY = 0},
new Seats {Code = "A3", PosX = 2, PosY = 0},
new Seats {Code = "A4", PosX = 3, PosY = 0}
};
The list below is also OK (two rows):
List<Seats> good = new List<Seats> {
new Seats {Code = "A1", PosX = 0, PosY = 0},
new Seats {Code = "A2", PosX = 1, PosY = 0},
new Seats {Code = "A3", PosX = 2, PosY = 0},
new Seats {Code = "A4", PosX = 3, PosY = 0},
new Seats {Code = "B1", PosX = 0, PosY = 1},
new Seats {Code = "B2", PosX = 1, PosY = 1},
new Seats {Code = "B3", PosX = 2, PosY = 1},
new Seats {Code = "B4", PosX = 3, PosY = 1}
};
The list below is also OK (same row, two groups, gap at (4,0)):
List<Seats> good = new List<Seats> {
new Seats {Code = "A1", PosX = 0, PosY = 0},
new Seats {Code = "A2", PosX = 1, PosY = 0},
new Seats {Code = "A3", PosX = 2, PosY = 0},
new Seats {Code = "A4", PosX = 3, PosY = 0},
new Seats {Code = "A6", PosX = 5, PosY = 0},
new Seats {Code = "A7", PosX = 6, PosY = 0},
new Seats {Code = "A8", PosX = 7, PosY = 0},
new Seats {Code = "A9", PosX = 8, PosY = 0}
};
But the list below is not OK because there is a gap at (3,0):
List<Seats> bad1 = new List<Seats> {
new Seats {Code = "A1", PosX = 0, PosY = 0},
new Seats {Code = "A2", PosX = 1, PosY = 0},
new Seats {Code = "A3", PosX = 2, PosY = 0},
new Seats {Code = "A5", PosX = 4, PosY = 0}
};
The list below is also not OK because there are five of them:
List<Seats> bad2 = new List<Seats> {
new Seats {Code = "A1", PosX = 0, PosY = 0},
new Seats {Code = "A2", PosX = 1, PosY = 0},
new Seats {Code = "A3", PosX = 2, PosY = 0},
new Seats {Code = "A4", PosX = 3, PosY = 0},
new Seats {Code = "A5", PosX = 4, PosY = 0}
};
The list below is also not OK because the four Seats need to be in a horizontal row:
List<Seats> bad3 = new List<Seats> {
new Seats {Code = "A1", PosX = 0, PosY = 0},
new Seats {Code = "A2", PosX = 1, PosY = 0},
new Seats {Code = "B1", PosX = 0, PosY = 1},
new Seats {Code = "B2", PosX = 1, PosY = 1}
};
For checking multiplication of 4 (it can be 8, 12, …) I can just do:
list.Count % 4 == 0
But I need help in how to check ‘in a row’.
Split all values in lists per Y value.
Then for each split list:
Assuming the points are in order (as in your example), at least for X coordinate.