Created
March 1, 2016 19:15
-
-
Save mamelara/653754d59311206a8599 to your computer and use it in GitHub Desktop.
Point code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************** | |
* 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.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 final Comparator<Point> SLOPE_ORDER = new SlopeOrder(); | |
/* | |
* Initializes a new point. | |
* | |
* @param x the <em>x</em>-coordinate of the point | |
* @param y the <em>y</em>-coordinate of the point | |
*/ | |
private class SlopeOrder implements Comparator<Point> { | |
public int compare(Point point1, Point point2) { | |
double slope1 = slopeTo(point1); | |
double slope2 = slopeTo(point2); | |
if(slope1 > slope2){ | |
return 1; | |
} | |
else if(slope1 < slope2) { | |
return -1; | |
} | |
return 0; | |
} | |
} | |
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) { | |
double deltaY = that.y - this.y; | |
double deltaX = that.x - this.x; | |
if (deltaX == 0 && deltaY == 0) { | |
return Double.NEGATIVE_INFINITY; | |
} | |
if(deltaX == 0) { | |
return Double.POSITIVE_INFINITY; | |
} | |
if(deltaY == 0) { | |
return 0.0; | |
} | |
return deltaY/deltaX; | |
} | |
/* | |
* 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 0< 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 pointb | |
*/ | |
private boolean less(int p1, int p2) { | |
return p1 < p2; | |
} | |
private boolean greater(int p1, int p2) { | |
return p1 > p2; | |
} | |
public int compareTo(Point that) { | |
if (less(this.y, that.y)) { | |
return -1; | |
} | |
else if (greater(this.y, that.y)) { | |
return 1; | |
} | |
else if (greater(that.x, this.x)) { | |
return -1; | |
} | |
else if(greater(this.x, that.x)) { | |
return 1; | |
} | |
else { | |
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 | |
*/ | |
/* | |
* 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 + ")"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment