001 package classwork;
002 /**
003 * Models a line.
004 * @author Tom James
005 *
006 */
007 public class Line {
008 /**
009 * Constructs a line given a point and a slope.
010 * @param x The x-coordinate.
011 * @param y The y-coordinate.
012 * @param m The slope.
013 */
014 public Line(int x,int y,double m) {
015 b = y - m*x;
016 this.m = m;
017 }
018 /**
019 * Constructs a line given two points.
020 * @param x1 The first x-coordinate.
021 * @param y1 The first y-coordinate.
022 * @param x2 The second x-coordinate.
023 * @param y2 The second y-coordinate.
024 */
025 public Line(int x1,int y1,int x2,int y2) {
026 if ((x1-x2) != 0) {
027 m = (y1-y2)/(x1-x2);
028 }
029 b = y1 - m*x1;
030 }
031 /**
032 * Constructs a line given a slope and an x-intercept.
033 * @param m The slope.
034 * @param b The x-intercept.
035 */
036 public Line(double m,int b) {
037 this.m = m;
038 this.b = b;
039 }
040 /**
041 * Constructs a line in the form x = a.
042 * @param a The x-intercept of the vertical line.
043 */
044 public Line(int a) {
045 b = a;
046 }
047 /**
048 * @return The slope of the line.
049 */
050 public double getSlope() {
051 return m;
052 }
053 /**
054 * @return The x-intercept of the line.
055 */
056 public double getB() {
057 return b;
058 }
059 /**
060 * @return Whether or not the line is vertical.
061 */
062 public boolean isVertical() {
063 if (vertical = true) {
064 return true;
065 }
066 else {
067 return false;
068 }
069 }
070 /**
071 * @param other The other line to be tested.
072 * @return Whether or not the two lines intersect.
073 */
074 public boolean intersects(Line other) {
075 if (other.equals(this) == true) {
076 return true;
077 }
078 if (vertical == true && other.isVertical() == true) {
079 return false;
080 }
081 if (m == other.getSlope()) {
082 return false;
083 }
084 return true;
085 }
086 /**
087 * @param other The other line to be tested.
088 * @return Whether or not the lines are parallel.
089 */
090 public boolean isParallel(Line other) {
091 if (vertical == true && other.isVertical() == true) {
092 return true;
093 }
094 if (m == other.getSlope()) {
095 return true;
096 }
097 return false;
098 }
099 /**
100 * @param other The other line to be tested,
101 * @return Whether or not the lines are the same.
102 */
103 public boolean equals(Line other) {
104 if (m == other.getSlope() && b == other.getB()) {
105 return true;
106 }
107 return false;
108 }
109 // private int x,y,x1,x2,y1,y2,a,b;
110 /**
111 * The slope of the line.
112 */
113 private double m;
114 /**
115 * The x-intercept of the line.
116 */
117 private double b;
118 /**
119 * Whether or not the line is vertical.
120 */
121 private boolean vertical;
122 }