@Test void intersect() { Line l1 = new Line(-3, 5); Line l2 = new Line(5, -8); assertEquals(l1.intersect(l2), l2.intersect(l1)); assertEquals(l1.intersect(l2), new Point(1.625, 0.125)); Line p2 = new Line(-3, 6); assertThrows(IllegalArgumentException.class, () -> { p2.intersect(l1); }); Line e2 = new Line(-3, 5); assertThrows(IllegalArgumentException.class, () -> { e2.intersect(l1); }); Line v1 = new Line(new Point(5, 1), Double.NEGATIVE_INFINITY); Line v2 = new Line(new Point(2, 1), Double.POSITIVE_INFINITY); assertThrows(IllegalArgumentException.class, () -> { v1.intersect(v2); }); }
public Point intersect(Line l2) { if (MathUtil.approxEquals(this.slope, l2.slope)) { if (MathUtil.approxEquals(this.intercept, l2.intercept)) { throw new IllegalArgumentException("Equal lines intersect everywhere"); } else { throw new IllegalArgumentException("Parallel lines do not intersect"); } } if (isVerticalLine) { return new Point(intercept, l2.evaluate(intercept)); } else if (l2.isVerticalLine) { return new Point(l2.intercept, this.evaluate(intercept)); } else { double xIntersect = (l2.intercept - this.intercept) / (this.slope - l2.slope); return new Point(xIntersect, this.evaluate(xIntersect)); } }
Line { public Point intersect(Line l2) { if (MathUtil.approxEquals(this.slope, l2.slope)) { if (MathUtil.approxEquals(this.intercept, l2.intercept)) { throw new IllegalArgumentException("Equal lines intersect everywhere"); } else { throw new IllegalArgumentException("Parallel lines do not intersect"); } } if (isVerticalLine) { return new Point(intercept, l2.evaluate(intercept)); } else if (l2.isVerticalLine) { return new Point(l2.intercept, this.evaluate(intercept)); } else { double xIntersect = (l2.intercept - this.intercept) / (this.slope - l2.slope); return new Point(xIntersect, this.evaluate(xIntersect)); } } }
Line { public Point intersect(Line l2) { if (MathUtil.approxEquals(this.slope, l2.slope)) { if (MathUtil.approxEquals(this.intercept, l2.intercept)) { throw new IllegalArgumentException("Equal lines intersect everywhere"); } else { throw new IllegalArgumentException("Parallel lines do not intersect"); } } if (isVerticalLine) { return new Point(intercept, l2.evaluate(intercept)); } else if (l2.isVerticalLine) { return new Point(l2.intercept, this.evaluate(intercept)); } else { double xIntersect = (l2.intercept - this.intercept) / (this.slope - l2.slope); return new Point(xIntersect, this.evaluate(xIntersect)); } } Line(Point p1, Point p2);  Line(Point point, double slope);  Line(double slope, double yIntercept); }
Line { public Point intersect(Line l2) { if (MathUtil.approxEquals(this.slope, l2.slope)) { if (MathUtil.approxEquals(this.intercept, l2.intercept)) { throw new IllegalArgumentException("Equal lines intersect everywhere"); } else { throw new IllegalArgumentException("Parallel lines do not intersect"); } } if (isVerticalLine) { return new Point(intercept, l2.evaluate(intercept)); } else if (l2.isVerticalLine) { return new Point(l2.intercept, this.evaluate(intercept)); } else { double xIntersect = (l2.intercept - this.intercept) / (this.slope - l2.slope); return new Point(xIntersect, this.evaluate(xIntersect)); } } Line(Point p1, Point p2);  Line(Point point, double slope);  Line(double slope, double yIntercept); double perpendicularSlope(); double evaluate(double x); Point intersect(Line l2); Point nearestLinePoint(Point p); boolean isVertical(); static Point midpoint(Point p1, Point p2); static double distance(Point p1, Point p2); @Override boolean equals(Object o); }
Line { public Point intersect(Line l2) { if (MathUtil.approxEquals(this.slope, l2.slope)) { if (MathUtil.approxEquals(this.intercept, l2.intercept)) { throw new IllegalArgumentException("Equal lines intersect everywhere"); } else { throw new IllegalArgumentException("Parallel lines do not intersect"); } } if (isVerticalLine) { return new Point(intercept, l2.evaluate(intercept)); } else if (l2.isVerticalLine) { return new Point(l2.intercept, this.evaluate(intercept)); } else { double xIntersect = (l2.intercept - this.intercept) / (this.slope - l2.slope); return new Point(xIntersect, this.evaluate(xIntersect)); } } Line(Point p1, Point p2);  Line(Point point, double slope);  Line(double slope, double yIntercept); double perpendicularSlope(); double evaluate(double x); Point intersect(Line l2); Point nearestLinePoint(Point p); boolean isVertical(); static Point midpoint(Point p1, Point p2); static double distance(Point p1, Point p2); @Override boolean equals(Object o);  }