Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- * Compilation: javac src.Point.java
- * Execution: java src.Point
- * Dependencies: none
- *
- * An immutable data type for points in the plane.
- * For use on Coursera, Algorithms Part I programming assignment.
- *
- ******************************************************************************/
- import edu.princeton.cs.algs4.StdDraw;
- import java.util.Comparator;
- public class Point implements Comparable<Point> {
- private final int x; // x-coordinate of this point
- private final int y; // y-coordinate of this point
- /**
- * Initializes a new point.
- *
- * @param x the <em>x</em>-coordinate of the point
- * @param y the <em>y</em>-coordinate of the 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) {
- if (that.y == y) { // the points are on the same horizontal line
- if (that.x == x)
- return Double.NEGATIVE_INFINITY; // points are equal
- else
- return +0.0; // that point is on an horizontal line
- } else if (that.x == x) {// the points are on a vertical line
- return Double.POSITIVE_INFINITY;
- } else
- return (that.y - y) * 1.0 / (that.x - x); // the elevation divided by the run
- }
- /**
- * 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) {
- int diffY = that.y - y;
- if (diffY < 0) return 1;
- else if (diffY > 0) return -1;
- // so points are on same horizontal line
- int diffX = that.x - x;
- if (diffX < 0) return 1;
- else if (diffX > 0) return -1;
- // points are equals
- return 0;
- }
- /**
- * Compares two points by the slope they make with this point. The slope is defined as in the
- * slopeTo() method.
- *
- * @return the Comparator that defines this ordering on points
- */
- public Comparator<Point> slopeOrder() {
- return Comparator.comparingDouble(this::slopeTo);
- }
- /**
- * Returns a string representation of this point. This method is provide for debugging; your
- * program should not rely on the format of the string representation.
- *
- * @return a string representation of this point
- */
- public String toString() {
- /* DO NOT MODIFY */
- return "(" + x + ", " + y + ")";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement