2.5.25

上次更新:2019-04-17
发现了题解错误/代码缺陷/排版问题?请点这里:如何:提交反馈

解答

官方解答见:https://algs4.cs.princeton.edu/25applications/Point2D.java.html

这些比较器都以嵌套类的形式在 Point2D 中定义。
静态比较器直接在类中以静态成员的方式声明。
非静态比较器则需要提供工厂方法,该方法新建并返回对应的比较器对象。

代码

/// <summary>
/// 按照 X 顺序比较。
/// </summary>
private class XOrder : Comparer<Point2D>
{
    public override int Compare(Point2D x, Point2D y)
    {
        if (x.X < y.X)
            return -1;
        if (x.X > y.X)
            return 1;
        return 0;
    }
}

/// <summary>
/// 按照 Y 顺序比较。
/// </summary>
private class YOrder : Comparer<Point2D>
{
    public override int Compare(Point2D x, Point2D y)
    {
        if (x.Y < y.Y)
            return -1;
        if (x.Y > y.Y)
            return 1;
        return 0;
    }
}

/// <summary>
/// 按照极径顺序比较。
/// </summary>
private class ROrder : Comparer<Point2D>
{
    public override int Compare(Point2D x, Point2D y)
    {
        double delta = (x.X * x.X + x.Y * x.Y) - (y.X * y.X + y.Y * y.Y);
        if (delta < 0)
            return -1;
        if (delta > 0)
            return 1;
        return 0;
    }
}

/// <summary>
/// 按照 atan2 值顺序比较。
/// </summary>
private class Atan2Order : Comparer<Point2D>
{
    private Point2D parent;
    public Atan2Order() { }
    public Atan2Order(Point2D parent)
    {
        this.parent = parent;
    }
    public override int Compare(Point2D x, Point2D y)
    {
        double angle1 = this.parent.AngleTo(x);
        double angle2 = this.parent.AngleTo(y);
        if (angle1 < angle2)
            return -1;
        if (angle1 > angle2)
            return 1;
        return 0;
    }
}

/// <summary>
/// 按照极角顺序比较。
/// </summary>
private class PolorOrder : Comparer<Point2D>
{
    private Point2D parent;
    public PolorOrder() { }
    public PolorOrder(Point2D parent)
    {
        this.parent = parent;
    }
    public override int Compare(Point2D q1, Point2D q2)
    {
        double dx1 = q1.X - this.parent.X;
        double dy1 = q1.Y - this.parent.Y;
        double dx2 = q2.X - this.parent.X;
        double dy2 = q2.Y - this.parent.Y;

        if (dy2 >= 0 && dy2 < 0)
            return -1;
        else if (dy2 >= 0 && dy1 < 0)
            return 1;
        else if (dy1 == 0 && dy2 == 0)
        {
            if (dx1 >= 0 && dx2 < 0)
                return -1;
            else if (dx2 >= 0 && dx1 < 0)
                return 1;
            return 0;
        }
        else
            return -CCW(this.parent, q1, q2);
    }
}

/// <summary>
/// 按照距离顺序比较。
/// </summary>
private class DistanceToOrder : Comparer<Point2D>
{
    private Point2D parent;
    public DistanceToOrder() { }
    public DistanceToOrder(Point2D parent)
    {
        this.parent = parent;
    }
    public override int Compare(Point2D p, Point2D q)
    {
        double dist1 = this.parent.DistanceSquareTo(p);
        double dist2 = this.parent.DistanceSquareTo(q);

        if (dist1 < dist2)
            return -1;
        else if (dist1 > dist2)
            return 1;
        else
            return 0;
    }
}

/// <summary>
/// 提供到当前点极角的比较器。
/// </summary>
/// <returns></returns>
public Comparer<Point2D> Polor_Order()
{
    return new PolorOrder(this);
}

/// <summary>
/// 提供到当前点 Atan2 值的比较器。
/// </summary>
/// <returns></returns>
public Comparer<Point2D> Atan2_Order()
{
    return new Atan2Order(this);
}

/// <summary>
/// 提供到当前点距离的比较器。
/// </summary>
/// <returns></returns>
public Comparer<Point2D> DistanceTo_Order()
{
    return new DistanceToOrder(this);
}

另请参阅

SortApplication 库

上一题 下一题