Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two points I need to compare two points
point p1= new point(100,100);
point p2 = new point(105,103);

i tried if(p1.Equals(p2))
and if(p1==p2)
{
}
both didnt work

please hellp me how to keep some approximation and check two points like p1.x+5 and p1.y+5 or p1.x-5 or p1.Y-5 and compare with p2
Posted

Two ideas come to my mind. Is one of these what you're looking for?
C#
private bool IsManhattanDistanceWithinTolerance(Point p0, Point p1, float tolerance)
{
    return(
        Math.Abs(p1.X - p0.X) <= tolerance
        && Math.Abs(p1.Y - p1.Y) <= tolerance
    );
}

private bool IsEuclidicDistanceWithinTolerance(Point p0, Point p1, float tolerance)
{
    return( Math.Sqrt(((p1.X - p0.X) * (p1.X - p0.X)) + ((p1.Y - p0.Y) * (p1.Y - p0.Y))) <= tolerance)
}
 
Share this answer
 
Comments
BillWoodruff 27-Aug-13 3:38am    
+5 Now that is creativity at play in the fields of code :)
Your code compared instances (references) of two Point objects. Basically, are both references pointing to the same memory location?

You need to compare the X and Y properties of each point separately.
if ((p1.X == p2.X) && (p1.Y == p2.Y))
 
Share this answer
 
Comments
BillWoodruff 26-Aug-13 21:57pm    
Dave, in this case I must disagree with you: two separate declarations of Point Objects created by 'new: do not point to the same locations in memory. A 'Point (under-the-hood a 'Struct) is a Value Type, and is allocated directly on the Stack.

Of course, if In-Line Allocation occurs, where value-types that you expect to see on the stack are copied-over to be used as Properties, or whatever, of reference objects (like a Class, or WinForms Controls): that's the case of in-line allocation, as Albahari said:

"Previously we said that for value-typed local variables, memory is allocated on the stack. So does that mean the newly copied Size struct is also allocated on the stack? The answer is no, because it’s not a local variable! Instead, it’s stored in a field of another object (in this case a form) that’s allocated on the heap. Therefore, it must, too, be allocated on the heap. This mode of storage is called 'in-line'." http://www.albahari.com/valuevsreftypes.aspx
Dave Kreskowiak 26-Aug-13 23:36pm    
Man, I have been "eyes-down" in a web project for way too long now...
Direct comparison for identical values (all values match) of two separately created Points is built-in to .NET.

Put a TextBox on a Form, and then execute some code like this somewhere:
C#
Point p1 = new Point(100, 100);
Point p2 = new Point(100, 100);

textBox1.Clear();
textBox1.AppendText((p1 == p2).ToString());
You'll see that if all numbers are the same, the result is 'true, and if any number is not the same, the result is 'false.

Was there some other kind of comparison you had in mind here ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900