public void testTranslate() { Line test = new Line(new Point((float)101.11, 3), new Point((float)33.3333, (float)-36.38)); test.translate(10, -1); Assert.AreEqual(43.3333, test.getEndPoint().X, 0.0001, "the resulting end x coordinate is correct"); Assert.AreEqual(-37.38, test.getEndPoint().Y, 0.0001, "the resulting end y coordinate is correct"); Assert.AreEqual(111.11, test.getStartPoint().X, 0.0001, "the resulting start x coordinate is correct"); Assert.AreEqual(2, test.getStartPoint().Y, 0.0001, "the resulting start y coordinate is correct"); }
public void testRotate() { Line test = new Line(new Point(-2, 3), new Point(2, 1)); test.rotate(90); Assert.AreEqual(-1, test.getEndPoint().X, 0.1, "the resulting end x coordinate is correct"); Assert.AreEqual(2, test.getEndPoint().Y, 0.1, "the resulting end y coordinate is correct"); Assert.AreEqual(-3, test.getStartPoint().X, 0.1, "the resulting start x coordinate is correct"); Assert.AreEqual(-2, test.getStartPoint().Y, 0.1, "the resulting start y coordinate is correct"); }
/* * 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); }