题注
Princeton Algorithm Part I中Week 3的Assignment实际上也是很早前就做完了,不过因为懒,一直没放到网上来… 正好新周期的Algorithm课也督促我复习一遍算法知识,另一方面也有机会把所有的Assignment做完吧。上个周期中,Week 4的Assignment没有拿到满分,就萌生退意了。这回卷土重来,希望能拿到一个Perfect的结果(当然了,Princeton的公开课是没有Accomplishment的,做这个只是学习和课外消遣而已)。
Week 3的题目是图论中的一个很典型的问题,在LeetCode中也有相应的算法题,也算是为刷LeetCode图论的题打点基础吧!
题目
Programming Assignment 3: Pattern Recognition
Write a program to recognize line patterns in a given set of points.
Computer vision involves analyzing patterns in visual images andreconstructing the real-world objects that produced them. The processin often broken up into two phases: feature detection andpattern recognition. Feature detection involves selectingimportant features of the image; pattern recognition involvesdiscovering patterns in the features. We will investigate aparticularly clean pattern recognition problem involving points andline segments. This kind of pattern recognition arises in many otherapplications such as statistical data analysis.
The problem.Given a set of N distinct points in the plane, draw every (maximal) line segment that connects a subset of 4 or more of the points.
Point data type.Create an immutable data type Point that represents a point in the planeby implementing the following API:
To get started, use the data type Point.java,which implements the constructor and the draw(), drawTo(), and toString() methods.Your job is to add the following components.public class Point implements Comparable<Point> { public final Comparator<Point> SLOPE_ORDER; // compare points by slope to this point public Point(int x, int y) // construct the point (x, y) public void draw() // draw this point public void drawTo(Point that) // draw the line segment from this point to that point public String toString() // string representation public int compareTo(Point that) // is this point lexicographically smaller than that point? public double slopeTo(Point that) // the slope between this point and that point }
- The compareTo() method should compare points by their y-coordinates,breaking ties by their x-coordinates.Formally, the invoking point(x0, y0)is less than the argument point(x1, y1)if and only if either y0 < y1 or ify0 = y1 and x0 < x1.
- The slopeTo() method should return the slope between the invoking point(x0, y0) and the argument point(x1, y1), which is given by the formula(y1 − y0) / (x1 − x0).Treat the slope of a horizontal line segment as positive zero [added 7/29];treat the slope of a vertical line segment as positive infinity;treat the slope of a degenerate line segment (between a point and itself) as negative infinity.
- The SLOPE_ORDER comparator should compare points by the slopes theymake with the invoking point (x0, y0).Formally, the point (x1, y1) is less thanthe point (x2, y2) if and only if the slope(y1 − y0) / (x1 − x0) is less than the slope(y2 − y0) / (x2 − x0).Treat horizontal, vertical, and degenerate line segments as in the slopeTo() method.
Brute force.Write a program Brute.java that examines 4 points at a time and checks whetherthey all lie on the same line segment, printing out any