# List.Duplicates() does not work properly

I have a problem with removing duplicates from my list of custom class - I looked all around the internet to find out my solution but cannot make it work.

``````private List<VoronoiPolygon> CreatePolygons(Voronoi voronoi, List<GraphEdge> edges)
{
List<VoronoiPolygon> polygons = new List<VoronoiPolygon>();
for (int i = 0; i < pointAmount; i++)
{
``````

}

for (int i = 0; i < edges.Count; i++)
{
// putting duplicate data into the list.
}

for (int i = 0; i < polygons.Count; i++)
{
polygons_.vertices = polygons*.vertices.Distinct().ToList();
}*_

return polygons;
}
Now Voronoi Polygon is my own class that contains a Point for its centre and List for vertices of edges around that centre. Data input is correct, sites of the Voronoi are sorted so that is not the problem.
VoronoiPolygon Class:
public class VoronoiPolygon
{
* public Point centre;*
* public List vertices = new List();*

* public VoronoiPolygon(Point centre)*
* {*
* this.centre = centre;*
* }*
}
And this is my Point Class:
public class Point : IEqualityComparer
{
* public double x, y;*
* public Point()*
* {*
* }*

* public void setPoint(double x, double y)*
* {*
* this.x = x;*
* this.y = y;*
* }*

* public bool Equals(Point x, Point y)*
* {*
* if(ReferenceEquals(x, y))*
* {*
* return true;*
* }*

* if (ReferenceEquals(x, null) || ReferenceEquals(y, null))*
* {*
* return false;*
* }*

* return x.x == y.x && x.y == y.y;*
* }*

* public int GetHashCode(Point obj)*
* {*
* if(ReferenceEquals(obj, null))*
* {*
* return 0;*
* }*

* int xHashCode = obj.x.GetHashCode();*
* int yHashCode = obj.y.GetHashCode();*

* return xHashCode ^ yHashCode;*
* }*
}
But even with all of this, duplicate members of the list are not removed - can somebody tell me where is that problem? Thank you.

Well I don’t really see any issue in your code. Are you sure that the duplicates you’re talking about are “actual” duplicates? Keep in mind that 1337.123 is not the same as 1337.123000001, even though it’s essentially the same value. So how are those duplicates generated in the first place? If they are calculated they most likely will not be true duplicates. That means that neither the hash values will be the same nor the == comparison will yield true. Are you sure you need double precision for the points? I have the feeling it might cause more problems that it solves. You could try adding this to your Equals method in line 25 (just before your return) temporarily for debugging.

``````double dx = x.x - y.x;
double dy = x.y - y.y;
if (dx*dx+dy*dy < 0.0001 && (x.x != y.x || x.y != y.y))
{
Debug.Log("Two points really close but not equal. Those might need to be considered equal:
``````

" +
x.x.ToString(“G17”)+" == " + y.x.ToString(“G17”)+"?
and those two
“+
x.y.ToString(“G17”)+” == " + y.y.ToString(“G17”) );
}

Are you still just trying to render a voronoi image or do you actually need the geometry? If you just need to render an an image I’ve found a neat solution using 3d cone meshes with enough faces. See this online voronoi generator for reference. Try reducing the cone radius to 100. All the magic happens due to the depth buffer and trivial intersections. So you just render all those overlapping cones and the image from above will show the voronoi pattern of those points. Though if you reduce the triangles / cone you will see jaggy lines since the silhouette of the cones is not smooth. However with sufficient triangles you get an almost perfect image (if that’s what you’re after).

Of course generating the actual voronoi mesh or the delaunay triangulation is of course more useful in the long run.