
public virtual int CompareTo(Point other)
{
if (this.X < other.X  this.X == other.X && this.Y < other.Y)
return 1;
else if (this.X > other.X  this.X == other.X && this.Y > other.Y)
return 1;
else// (this.X == other.X && this.Y == other.Y)
return 0;
}
it is wrong.So the former two condition can't confirm (this.X == other.X && this.Y == other.Y)


Coordinator
Feb 21, 2007 at 6:39 PM

It is correct.
Either this.X is less than, greater than or equal to other.X, and this.Y is less than, greater than or equal to other.Y.
If it this.X is less than other.X, the first condition is true, if this.X is greater than other.X the second is true.
If this.X is equal to other.X, all three conditions now depend on the comparison of this.Y to other.Y. Since there are only three exclusive possibilities and there are three exclusive conditions, then one must be true. If it is the last one, this.X must be
equal to other.X (since a previous condition would have been true otherwise), and this.Y must be equal to other.Y by the same reasoning.



A different way to explain this is that a point to the left of another, is consider "less than", and to the right "more than". If it's neither to the right or the left of the other (ie. the X coordinates are the same), a point above another is consider
"more than" and below is "less than".
If it's neither "less than" or "more than", it follows that it must be equal.
This method is very useful for sorting point collections from lefttoright and bottomup, for use in spatial indexing or intersection algorithms.
Yukungis: If you still think it is wrong, could you please show a unit test that makes this method fail?



It's not wrong, but it isn't exactly written in the most readable manner :) It helps to understand it if you rewrite it to something like:
public virtual int CompareTo(Point other)
{
if (this.X < other.X  (this.X == other.X && this.Y < other.Y))
{
return 1;
}
else if (this.X > other.X  (this.X == other.X && this.Y > other.Y))
{
return 1;
}
else // (this.X == other.X && this.Y == other.Y)
{
return 0;
}
}
That way you can clearly see that there are only two conditions to be met in each if statement, as opposed to the three that appear to be present in the code that the OP posted.



Forgive me, but isn't there an epsilon for comparing doubles?


Coordinator
Feb 26, 2007 at 5:41 PM

Not in this comparison, apparently  although it should.
Copying to workitem...


Coordinator
Feb 26, 2007 at 5:46 PM

This discussion has been copied to
Work Item 8510. You may wish to continue further discussion there.

