Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 543215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:31:41+00:00 2026-05-13T10:31:41+00:00

This question is similar to the one here . We all know what PointF

  • 0

This question is similar to the one here.

We all know what PointF is, don’t we? This is the data structure:

public struct PointF
{
  public float X;
  public float Y;
}

How to implement IEqualityComparer<PointF> with tolerance? Let’s say my Equals code is like this

public const float Epsilon = 0.01; //say
public bool Equals(PointF pt1, PointF pt2)
{
   return Math.Abs(pt1.X-pt2.X)<Epsilon && Math.Abs(pt1.Y-pt2.Y)<Epsilon;
}

Question: How to implement the correct GetHashCode so that for a dictionary of PointF, I will access the element correctly?

I crack my head a few days but still can’t find a satisfactory solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:31:41+00:00Added an answer on May 13, 2026 at 10:31 am

    Instead of defining the tolerance by the distance, you could place the points in a grid.
    If two points are in the same cell, they’re considered equal and have the same hash code.

    public bool Equals(PointF pt1, PointF pt2)
    {
       return GetCell(pt1.X) == GetCell(pt2.X)
           && GetCell(pt1.Y) == GetCell(pt2.Y);
    }
    
    public int GetHashCode(PointF pt)
    {
       return GetCell(pt.X) ^ GetCell(pt.Y);
    }
    
    private static int GetCell(float f)
    {
        return (int)(f / 10); // cell size is 10 pixels
    }
    

    Thesis: There is no implementation of Equals and GetHashCode that meets your requirements.

    Proof: Consider the following three points, A, B, and C:

    Illustration

    As per your requirements,

    Equals(A, B) == true              // (i)
    Equals(B, C) == true              // (ii)
    Equals(A, C) == false             // (iii)
    GetHashCode(A) == GetHashCode(B)  // (iv)
    GetHashCode(B) == GetHashCode(C)  // (v)
    GetHashCode(A) != GetHashCode(C)  // (vi)
    

    But from (iv) and (v) follows

    GetHashCode(A) == GetHashCode(C)
    

    and thereby

    Equals(A, C) == true
    

    which contradicts (iii) and (vi).

    Since Equals and GetHashCode cannot return different values for the same arguments, there is no implementation that meets your requirements.
    q.e.d.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a similar question to this one here . Given a list of
I know there is another question that exists similar to this one but I
I found a few questions similar to this one here on SO, but none
This question is similar to this one How do I add options to a
realize this question is similar to this one. Pass URL parameters to a redirect_to
This question is similar to this other one , with the difference that the
Similar question as this one but for a Microsoft Environment. Email --> Exchange Server
This is a similar question to this one . I would like to convert
My question is similar to this one , but I would like to replicate
My question is similar to this one but none of the answers solve my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.