Advertisement
jules0707

Point.java

Nov 24th, 2020
978
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 KB | None | 0 0
  1. /******************************************************************************
  2.  *  Compilation:  javac src.Point.java
  3.  *  Execution:    java src.Point
  4.  *  Dependencies: none
  5.  *
  6.  *  An immutable data type for points in the plane.
  7.  *  For use on Coursera, Algorithms Part I programming assignment.
  8.  *
  9.  ******************************************************************************/
  10.  
  11. import edu.princeton.cs.algs4.StdDraw;
  12.  
  13. import java.util.Comparator;
  14.  
  15. public class Point implements Comparable<Point> {
  16.  
  17.     private final int x;     // x-coordinate of this point
  18.     private final int y;     // y-coordinate of this point
  19.  
  20.     /**
  21.      * Initializes a new point.
  22.      *
  23.      * @param x the <em>x</em>-coordinate of the point
  24.      * @param y the <em>y</em>-coordinate of the point
  25.      */
  26.     public Point(int x, int y) {
  27.         /* DO NOT MODIFY */
  28.         this.x = x;
  29.         this.y = y;
  30.     }
  31.  
  32.     /**
  33.      * Draws this point to standard draw.
  34.      */
  35.     public void draw() {
  36.         /* DO NOT MODIFY */
  37.         StdDraw.point(x, y);
  38.     }
  39.  
  40.     /**
  41.      * Draws the line segment between this point and the specified point to standard draw.
  42.      *
  43.      * @param that the other point
  44.      */
  45.     public void drawTo(Point that) {
  46.         /* DO NOT MODIFY */
  47.         StdDraw.line(this.x, this.y, that.x, that.y);
  48.     }
  49.  
  50.     /**
  51.      * Returns the slope between this point and the specified point. Formally, if the two points are
  52.      * (x0, y0) and (x1, y1), then the slope is (y1 - y0) / (x1 - x0). For completeness, the slope
  53.      * is defined to be +0.0 if the line segment connecting the two points is horizontal;
  54.      * Double.POSITIVE_INFINITY if the line segment is vertical; and Double.NEGATIVE_INFINITY if
  55.      * (x0, y0) and (x1, y1) are equal.
  56.      *
  57.      * @param that the other point
  58.      * @return the slope between this point and the specified point
  59.      */
  60.     public double slopeTo(Point that) {
  61.         if (that.y == y) { // the points are on the same horizontal line
  62.             if (that.x == x)
  63.                 return Double.NEGATIVE_INFINITY; // points are equal
  64.             else
  65.                 return +0.0; // that point is on an horizontal line
  66.         } else if (that.x == x) {// the points are on a vertical line
  67.             return Double.POSITIVE_INFINITY;
  68.         } else
  69.             return (that.y - y) * 1.0 / (that.x - x); // the elevation divided by the run
  70.     }
  71.  
  72.     /**
  73.      * Compares two points by y-coordinate, breaking ties by x-coordinate. Formally, the invoking
  74.      * point (x0, y0) is less than the argument point (x1, y1) if and only if either y0 < y1 or if
  75.      * y0 = y1 and x0 < x1.
  76.      *
  77.      * @param that the other point
  78.      * @return the value <tt>0</tt> if this point is equal to the argument point (x0 = x1 and y0 =
  79.      * y1); a negative integer if this point is less than the argument point; and a positive integer
  80.      * if this point is greater than the argument point
  81.      */
  82.     public int compareTo(Point that) {
  83.         int diffY = that.y - y;
  84.  
  85.         if (diffY < 0) return 1;
  86.         else if (diffY > 0) return -1;
  87.  
  88.         // so points are on same horizontal line
  89.         int diffX = that.x - x;
  90.  
  91.         if (diffX < 0) return 1;
  92.         else if (diffX > 0) return -1;
  93.         // points are equals
  94.         return 0;
  95.     }
  96.  
  97.     /**
  98.      * Compares two points by the slope they make with this point. The slope is defined as in the
  99.      * slopeTo() method.
  100.      *
  101.      * @return the Comparator that defines this ordering on points
  102.      */
  103.  
  104.     public Comparator<Point> slopeOrder() {
  105.         return Comparator.comparingDouble(this::slopeTo);
  106.     }
  107.  
  108.     /**
  109.      * Returns a string representation of this point. This method is provide for debugging; your
  110.      * program should not rely on the format of the string representation.
  111.      *
  112.      * @return a string representation of this point
  113.      */
  114.     public String toString() {
  115.         /* DO NOT MODIFY */
  116.         return "(" + x + ", " + y + ")";
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement