/* * Problem 1, Lab 7, Wednesday, 24-09-2008 Design and implement a class LineSegment (using the class Point) with the following attributes and methods. Attributes : Point p; Point q; These are the end-points of the line segment. Note: It may be considered that p.y 6= q.y. This is to avoid the condition when the slope of the line segment becomes ∞. Constructors : • LineSegment(Point a, Point b) It should build a line segment with end-point p same as that of the Point referenced by a and end-point q same as the Point referenced by b. • LineSegment(double x1, double y1, double x2, double y2) It should build a line segment with end-point p having coordinates (x1, y1) and end-point q having coordinates (x2, y2). • LineSegment() It should build a line segment with end-point p having coordinates (0, 0) and end- point q having coordinates (0, 0). Methods : (a) public double slope() To determine the slope of the line segment. (b) public boolean isAbove(Point r) Returns true if the line obtained by extending this line segment lies above point r. Else returns false. (c) public boolean intersects(LineSegment l) To determine whether line segment l intersects with this line. Use the method isAbove in this method. (d) public boolean isParallel(LineSegment l) To determine whether line segment l is parallel to this line. Use the method slope in this method. */ package Geometry; class LineSegment { Point p; Point q; LineSegment(Point a, Point b) { p = new Point(a.x, a.y); q = new Point(b.x, b.y); } LineSegment(double x1, double y1, double x2, double y2) { p = new Point(x1,y1); q = new Point(x2,y2); } LineSegment() { p = new Point(); q = new Point(); } //Slope of line with end points (x1,y1) and (x2,y2) is (y2-y1)/(x2-x1) public double slope() { return ((q.y - p.y)/(q.x - p.x)); } public boolean isAbove(Point r) { double m = slope(); double c = p.y - m * p.x; //line y = mx + c lies above point (x1, y1) if y1 − m*x1 − c < 0 return ((r.y - m * r.x -c) < 0); } public boolean intersects(LineSegment l) { //If both the end points of Linesegment l are either above or below the LineSegment 'this' then the LineSegment l does not intersect if(isAbove(l.p) && isAbove(l.q)) return false; else if (!isAbove(l.p) && !isAbove(l.q)) return false; else //Endpoints of LineSegment l lie on opposite side of 'this' line segment return true; } //Two lines are parallel only if their line slopes are same public boolean isParallel(LineSegment l) { return (slope() == l.slope()); } }