Hi.
I have tried the Exists(). I am not good at LINQ, this is all I got here.
private void censorPoints(List<Point3d> sourceLst, List<Point3d> distinctLst)
{
foreach (Point3d sourcePt in sourceLst)
{
bool existed = distinctLst.Exists(pt =>
{
if (pt.DistanceTo(sourcePt) < 20)
return true;
return false;
});
if (!existed)
distinctLst.Add(sourcePt);
}
}
I have my own problem of "the same" points. If two points are too close to each other, they are considered as "the same" points. One need to be filtered out. I don't know how to write the lambda expression for Distinct() and Contains() methods. I also don't which is the fastest.
If fastest is important, then you should consider using spatial indexing (discussed in the thread about finding the nearest point). You could do the filtering in the process of building the spatial index, since each point will have an index node, you can compare it to already-indexed points within the same index node. This would probably be the fastest way if you have a large number of coincident or 'equal' coordinates.
Doing this with the LINQ's Distinct() method is difficult when your comparison is 'fuzzy' because you would need to write a custom IEqualityComparer<Point3d>, and for Distinct() to take advantage of hashing, you would have to implement GetHashcode() and have it return the same hash value for all points that are equal. In other words if
Equals( a, b ) returns true, then
GetHashcode( a ) == GetHashcode( b ) must be true as well.
If you don't provide a hashcode (e.g., have GetHashcode() return 0 for all points), Distinct() will need to use Equals() to compare every point to every other point. Hence, while Distinct() will do the job, it will not perform well when the equality test is a 'fuzzy' comparison.