Suppose I have a list of points and if they are in a plane, it would look like this:
How do I group them into Four(4) groups of points?
Suppose I have a list of points and if they are in a plane, it would look like this:
How do I group them into Four(4) groups of points?
We can override the equals method to check the Points are equal or not . Add appropriate tolerance in the equals method
public class Point
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public bool AreEquals(object obj,double tolerance)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
Point obj1 = (Point)obj;
var absX = Math.Pow(X - obj1.X, 2);
var absY = Math.Pow(Y - obj1.Y, 2);
var absZ = Math.Pow(Z - obj1.Z, 2);
if (Math.Abs(absX + absY + absZ) >= tolerance)
{
return false;
}
else
{
return true;
}
}
}
We can add the group the Points using below
public class PointGroup
{
public int GroupID { get; set; }
public Point Point1 { get; set; }
public bool IsGrouped { get; set; }
}
for (int i = 0; i < coll.Count(); i++)
{
PointGroup pg1 = coll[i];
if (!pg1.IsGrouped)
{
for (int j = 0; j < coll.Count(); j++)
{
PointGroup pg2 = coll[j];
if (pg1.Point1.AreEquals(pg2.Point1,0.1) && pg2.IsGrouped == false)
{
if (pg2.GroupID == j)
{
pg2.GroupID = pg1.GroupID;
pg2.IsGrouped = true;
}
}
}
pg1.IsGrouped = true;
}
}