algorithm第三周作业 Collinear Points

这篇博客介绍了Coursera上算法课程第三周的作业——Collinear Points,要求找出给定点中四个及以上共线的线段。讨论了Point、BruteCollinearPoints和FastCollinearPoints三个类的实现,重点在于点的排序、线段的唯一性以及处理垂直线的特殊情况。文章强调了compareTo和Comparator的使用,以及如何确保线段输出的正确性和封装性。
摘要由CSDN通过智能技术生成

cousera 上algorithm part I第三周课程讲述的是排序,包括插入排序、选择排序、希尔排序、归并排序和快速排序。其配套作业为Collinear Points,题目大意为给定若干点,求出其中的有四个及以上点共线的线段。
要求提交三个文件,Point.java,BruteCollinearPoints.java,FastCollinearPoints.java。

Point类

给定的的接口如下:

public class Point implements Comparable<Point> {
   
   public Point(int x, int y)                         // constructs the point (x, y)

   public   void draw()                               // draws this point
   public   void drawTo(Point that)                   // draws the line segment from this point to that point
   public String toString()                           // string representation
//上面的函数已经写好了,只要实现下面三个函数即可
   public               int compareTo(Point that)     // compare two points by y-coordinates, breaking ties by x-coordinates
   public            double slopeTo(Point that)       // the slope between this point and that point
   public Comparator<Point> slopeOrder()              // compare two points by slopes they make with this point
}

Points类实现的难点和注意点:
1.对java初学中来说,compareTo和Comparator实现有一些难点。简而言之,这两个接口的实现都是用来做比较的,两者的差别在于,打个比方,compareTo是默认比较器,Comparator是给默认分类器做补充的比较器。

Point[] points = new Point[10];
....//初始化Points
Point anchor = new Point(3,3);
Arrays.sort(points);//默认情况下用CompareTo里实现的比较方法来排序
Arrays.sort(points,anchor.slopeOrder())//不想用默认的,自己再写一个比较器来排序。

Point类实现如下:

/******************************************************************************
 *  Compilation:  javac Point.java
 *  Execution:    java Point
 *  Dependencies: none
 *
 *  An immutable data type for points in the plane.
 *  For use on Coursera, Algorithms Part I programming assignment.
 *
 ******************************************************************************/
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import edu.princeton.cs.algs4.StdDraw;

public class Point implements Comparable<Point> {
   

    private final int x;     // x-coordinate of this point
    private final int y;     // y-coordinate of this point
    public Point(int x, int y) {
   
        /* DO NOT MODIFY */
        this.x = x;
        this.y = y;
    }

    /**
     * Draws this point to standard draw.
     */
    public void draw() {
   
        /* DO NOT MODIFY */
        StdDraw.point(x, y);
    }

    /**
     * Draws the line segment between this point and the specified point
     * to standard draw.
     *
     * @param that the other point
     */
    public void drawTo(Point that) {
   
        /* DO NOT MODIFY */
        StdDraw.line(this.x, this.y, that.x, that.y);
    }

    /**
     * Returns the slope between this point and the specified point.
     * Formally, if the two points are (x0, y0) and (x1, y1), then the slope
     * is (y1 - y0) / (x1 - x0). For completeness, the slope is defined to be
     * +0.0 if the line segment connecting the two points is horizontal;
     * Double.POSITIVE_INFINITY if the line segment is vertical;
     * and Double.NEGATIVE_INFINITY if (x0, y0) and (x1, y1) are equal.
     *
     * @param  that the other point
     * @return the slope between this point and the specified point
     */
    public double slopeTo(Point that) {
   
        /* YOUR CODE HERE */
        if (this.y == that.y) //斜率为0或者同一个位置的点
        {
   
            if (this.x == that.x)
            {
   
                return Double.NEGATIVE_INFINITY;
            }
            else
            {
   
                return 0.0;//斜率为0
            }
        }
        else {
   
            if (this.x == that.x)
            {
   
                return Double.POSITIVE_INFINITY;
            }
            else
            {
   
                return 1.0 * (that.y - this.y) / (that.x - this.x);
            }
        }

    }

    /**
     * Compares two points by y-coordinate, breaking ties by x-coordinate.
     * Formally, the invoking point (x0, y0) is less than the argument point
     * (x1, y1) if and only if either y0 < y1 or if y0 = y1 and x0 < x1.
     *
     * @param  that the other point
     * @return the value <tt>0</tt> if this point is equal to the argument
     *         point (x0 = x1 and y0 = y1);
     *         a negative integer if this point is less than the argument
     *         point; and a positive integer if this point is greater than the
     *         argument point
     */
    public int compareTo(Point that) {
   
        /* YOUR CODE HERE */
        if (this.y < that.y)
        {
   
            return -1;
        }
        else if (this.y > that.y)
        {
   
            return 1;
        }
        else
        {
   
            if (this.x < that.x)
            
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值