public void inDomainAndRangeTest() { Point one = new Point(0, 0); Point two = new Point(1, 1); LineSegment target = new LineSegment(one, two); Point intersectionPoint = new Point(.5, .5); bool expected = true; Assert.AreEqual(expected, target.inDomainAndRange(intersectionPoint)); intersectionPoint = new Point(1.5, .5); expected = false; Assert.AreEqual(expected, target.inDomainAndRange(intersectionPoint)); one = new Point(0, 0); two = new Point(0, 5); target = new LineSegment(one, two); expected = false; intersectionPoint = new Point(1, 4); Assert.AreEqual(expected, target.inDomainAndRange(intersectionPoint)); //Here the point 0,10 is in the domain, but not the range. expected = false; intersectionPoint = new Point(0, 10); Assert.AreEqual(expected, target.inDomainAndRange(intersectionPoint)); //Point is on the line segment, ie within range and domain of a vertical line expected = true; intersectionPoint = new Point(0, 4); Assert.AreEqual(expected, target.inDomainAndRange(intersectionPoint)); }
public bool intersectsLineSegment(LineSegment L, ref Point intersectionpoint, ref bool infiniteIntersections) { Point possibleIntersectionPoint = this.intersectsLine(L, ref infiniteIntersections); if (infiniteIntersections) { //possibly Intersects at infinitely many points due to it being a line, but restricting it to a segment, changes things! //Find lowest bound: LineSegment theLowerSegment, theHigherSegment; if (this.isInfiniteSlope) { //Use y values to check, not x! if (this.lowerBound.y < L.lowerBound.y) { theHigherSegment = L; theLowerSegment = this; } else { theHigherSegment = this; theLowerSegment = L; } //See how much overlap there is on the 2 bounds if (theLowerSegment.upperBound.y < theHigherSegment.lowerBound.y) { //Non overlapping infiniteIntersections = false; intersectionpoint = null; return false; } else if (theLowerSegment.upperBound.y == theHigherSegment.lowerBound.y) { //Intersect at only one point intersectionpoint = theLowerSegment.upperBound; infiniteIntersections = false; return true; } else { //Overlap of bounds infiniteIntersections = true; intersectionpoint = null; return false; } } if (this.lowerBound.x < L.lowerBound.x) { theHigherSegment = L; theLowerSegment = this; } else { theHigherSegment = this; theLowerSegment = L; } //See how much overlap there is on the 2 bounds if (theLowerSegment.upperBound.x < theHigherSegment.lowerBound.x) { //Non overlapping infiniteIntersections = false; intersectionpoint = null; return false; } else if (theLowerSegment.upperBound.x == theHigherSegment.lowerBound.x) { //Intersect at only one point intersectionpoint = theLowerSegment.upperBound; infiniteIntersections = false; return true; } else { //Overlap of bounds infiniteIntersections = true; intersectionpoint = null; return false; } } if (possibleIntersectionPoint == null) { //Line doesnt interesect intersectionpoint = null; return false; } if (inDomainAndRange(possibleIntersectionPoint) && L.inDomainAndRange(possibleIntersectionPoint)) { intersectionpoint = possibleIntersectionPoint; return true; } //The line intersects but is not within the domain of both lines return false; }