/* * Returns the crossing point is the line crosses the other line, null if the lines do not cross * (!) lines are assued to be finite, starting at the start point, and ending at the end point * @param line the other line * @param epsilon the amount of error that is permittable for two points to be considered equal * (two points that are closer than epsilon will be considered equal) */ public Point crosses(Line line, double epsilon) { if (CoordinateCalculator.hasDifferenceLessThan(end_pt.X, start_pt.X, epsilon) && !CoordinateCalculator.hasDifferenceLessThan(line.end_pt.X, line.start_pt.X, epsilon)) { float crosing_x = end_pt.X; float crossing_y = line.getSlope() * crosing_x + line.getConstant(); Point crossing_pt = new Point(crosing_x, crossing_y); if (this.containsInVector(crossing_pt, epsilon) && line.containsInVector(crossing_pt, epsilon)) { return(crossing_pt); } } else if (!CoordinateCalculator.hasDifferenceLessThan(end_pt.X, start_pt.X, epsilon) && CoordinateCalculator.hasDifferenceLessThan(line.end_pt.X, line.start_pt.X, epsilon)) { float crosing_x = line.end_pt.X; float crossing_y = getSlope() * crosing_x + getConstant(); Point crossing_pt = new Point(crosing_x, crossing_y); if (this.containsInVector(crossing_pt, epsilon) && line.containsInVector(crossing_pt, epsilon)) { return(crossing_pt); } } else if ((CoordinateCalculator.hasDifferenceLessThan(end_pt.X, start_pt.X, epsilon) && CoordinateCalculator.hasDifferenceLessThan(line.end_pt.X, line.start_pt.X, epsilon)) || (this.getSlope() == line.getSlope())) { if (start_pt.isCloseTo(line.getStartPoint(), epsilon) || start_pt.isCloseTo(line.getEndPoint(), epsilon)) { return(start_pt); } else if (end_pt.isCloseTo(line.getEndPoint(), epsilon) || end_pt.isCloseTo(line.getStartPoint(), epsilon)) { return(end_pt); } } else { float crosing_x = (line.getConstant() - this.getConstant()) / (this.getSlope() - line.getSlope()); float crossing_y = this.getSlope() * crosing_x + this.getConstant(); Point crossing_pt = new Point(crosing_x, crossing_y); if (this.containsInVector(crossing_pt, epsilon) && line.containsInVector(crossing_pt, epsilon)) { return(crossing_pt); } } return(null); }
public void testGetSlope() { Line test = new Line(new Point(10, 10), new Point(15, 20)); Assert.AreEqual(2, test.getSlope(), "the slope is correct"); }