Again this sample is a very simplified version of my actual problem involving a custom comparer for linq grouping. What have I done wrong?
The code below produces the result below (1.2, 0), (4.1, 0), (4.1, 0), (1.1, 0),
however I was expecting the following since 1.1 and 1.2 are < 1.0 apart. (1.2, 0), (1.1, 0), (4.1, 0), (4.1, 0),
class Program
{
static void Main(string[] args)
{
IEnumerable<Point> points = new List<Point> {
new Point(1.1, 0.0)
, new Point(4.1, 0.0)
, new Point(1.2, 0.0)
, new Point(4.1, 0.0)
};
foreach (var group in points.GroupBy(p => p, new PointComparer()))
{
foreach (var num in group)
Console.Write(num.ToString() + ", ");
Console.WriteLine();
}
Console.ReadLine();
}
}
class PointComparer : IEqualityComparer<Point>
{
public bool Equals(Point a, Point b)
{
return Math.Abs(a.X - b.X) < 1.0;
}
public int GetHashCode(Point point)
{
return point.X.GetHashCode()
^ point.Y.GetHashCode();
}
}
class Point
{
public double X;
public double Y;
public Point(double p1, double p2)
{
X = p1;
Y = p2;
}
public override string ToString()
{
return "(" + X + ", " + Y + ")";
}
}