Skip to content

Instantly share code, notes, and snippets.

@benkoshy
Last active July 3, 2019 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benkoshy/8d8d05d1dc5f784dd2c87fdc9d30faf5 to your computer and use it in GitHub Desktop.
Save benkoshy/8d8d05d1dc5f784dd2c87fdc9d30faf5 to your computer and use it in GitHub Desktop.
A Point3d Comparer - it allows us to compare lists which are populated by points

The Point Comparer

        // Attributed to Gilles. But I made some very minor modifications.
        // WARNING: Implement the EqualityComparer<T> abstract class, rather than the
        // IEqualityComparer<T> interface. This is because the MS documentation recommends
        // that you do so.

        private class Point3dComparer : IEqualityComparer<Point3d>
        {
            private Tolerance tolerance;

            public Point3dComparer()
                : this(Tolerance.Global)
            {
            }

            public Point3dComparer(Tolerance tolerance)
            {
                this.tolerance = tolerance;
            }

            public bool Equals(Point3d pt1, Point3d pt2)
            {
                return pt1.IsEqualTo(pt2, tolerance);
            }

            public int GetHashCode(Point3d pt)
            {
                var d = tolerance.EqualPoint * 2.0;
                return new Point3d(
                Math.Round(pt.X / d),
                Math.Round(pt.Y / d),
                Math.Round(pt.Z / d)).GetHashCode();
            }
        }

But how do you use it?

// get polyline points:
       List<Point3d> layoutPoints = layoutPolyline.GetPolylinePoints().ToList();
       List<Point3d> shopdrawingPoints = shopDrawingPolyline.GetPolylinePoints().ToList();

       PointsUtility.Point3dComparer pointComparer = new PointsUtility.Point3dComparer(t);
                      
// Now if the two lists have the same elements but in a different order, then 
// SequenceEqual will return false.

       bool pointsEqual = layoutPoints.SequenceEqual(shopdrawingPoints, pointComparer);       
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment