using System;
using System.Collections;
using GeoAPI.Geometries;
namespace GisSharpBlog.NetTopologySuite.Utilities{
/// <summary>
/// CoordinateCompare is used in the sorting of arrays of Coordinate objects.
/// Implements a lexicographic comparison.
/// </summary>
public class CoordinateCompare : IComparer
{
/// <summary>
/// Constructor.
/// </summary>
public CoordinateCompare() { }
/// <summary>
/// Compares two object and returns a value indicating whether one is less than, equal to or greater
/// than the other.
/// </summary>
/// <param name="x">First Coordinate object to compare.</param>
/// <param name="y">Second Coordinate object to compare.</param>
/// <returns>
///<table cellspacing="0" class="dtTABLE">
///<TR VALIGN="top">
/// <TH width=50%>Value</TH>
///<TH width=50%>Condition</TH>
///</TR>
///<TR VALIGN="top">
/// <TD width=50%>Less than zero</TD>
///<TD width=50%><I>a</I> is less than <I>b</I>.</TD>
///</TR>
///<TR VALIGN="top">
/// <TD width=50%>Zero</TD>
///<TD width=50%><I>a</I> equals <I>b</I>.</TD>
///</TR>
///<TR VALIGN="top">
/// <TD width=50%>Greater than zero</TD>
///<TD width=50%><I>a</I> is greater than <I>b</I>.</TD>
///</TR>
///</table>
/// </returns>
/// <remarks>If a implements IComparable, then a. CompareTo (b) is returned; otherwise, if b
/// implements IComparable, then b. CompareTo (a) is returned.
/// Comparing a null reference (Nothing in Visual Basic) with any type is allowed and does not
/// generate an exception when using IComparable. When sorting, a null reference (Nothing) is
/// considered to be less than any other object.
/// </remarks>
public int Compare(object x, object y)
{
int returnValue = 0;
if (x is ICoordinate && y is ICoordinate)
{
ICoordinate coord1 = (ICoordinate) x;
ICoordinate coord2 = (ICoordinate) y;
if (coord1.X < coord2.X)
returnValue = -1;
else if (coord1.X > coord2.X)
returnValue = 1;
else if (coord1.Y < coord2.Y)
returnValue = -1;
else if (coord1.Y > coord2.Y)
returnValue = 1;
else returnValue = 0;
}
else throw new ArgumentException("Wrong arguments type: ICoordinate expected");
return returnValue;
}
}
}
|