Commit cfc4a4f3 authored by Michael Büsch's avatar Michael Büsch

geo2d: Fix intersection for aligned line segments

Signed-off-by: Michael Büsch's avatarMichael Buesch <m@bues.ch>
parent a66c4d76
......@@ -237,12 +237,16 @@ class LineSeg2D(Base2D):
if interA == interB:
continue
if not self.__inRect(interB.x, interB.y,
selfPointA, selfPointB):
selfPointA, selfPointB) or\
not self.__inRect(interB.x, interB.y,
otherPointA, otherPointB):
continue
return Inter2D(point=Point2D(interA.x, interA.y),
vect=Vect2D(interB.x - interA.x,
interB.y - interA.y),
intersects=True)
return Inter2D(point=Point2D(interA.x, interA.y),
intersects=True)
return None
inter = find(self.pointA, self.pointB,
......
......@@ -204,6 +204,70 @@ class Test_LineSeg2D(object):
assert_eq(inter.vect, Vect2D(0, -4))
assert_eq(inter.lineSeg, LineSeg2D(Point2D(4, 7), Point2D(4, 3)))
# parallel, vertical, pointy intersection
inter = LineSeg2D(Point2D(4, 1), Point2D(4, 5)).intersection(
LineSeg2D(Point2D(4, 5), Point2D(4, 7)))
assert_is(inter.intersects, True)
assert_eq(inter.point, Point2D(4, 5))
assert_eq(inter.vect, Vect2D(0, 0))
assert_eq(inter.lineSeg, LineSeg2D(Point2D(4, 5), Point2D(4, 5)))
# parallel, vertical, pointy intersection
inter = LineSeg2D(Point2D(4, 5), Point2D(4, 1)).intersection(
LineSeg2D(Point2D(4, 5), Point2D(4, 7)))
assert_is(inter.intersects, True)
assert_eq(inter.point, Point2D(4, 5))
assert_eq(inter.vect, Vect2D(0, 0))
assert_eq(inter.lineSeg, LineSeg2D(Point2D(4, 5), Point2D(4, 5)))
# parallel, vertical, pointy intersection
inter = LineSeg2D(Point2D(4, 5), Point2D(4, 1)).intersection(
LineSeg2D(Point2D(4, 7), Point2D(4, 5)))
assert_is(inter.intersects, True)
assert_eq(inter.point, Point2D(4, 5))
assert_eq(inter.vect, Vect2D(0, 0))
assert_eq(inter.lineSeg, LineSeg2D(Point2D(4, 5), Point2D(4, 5)))
# parallel, vertical, pointy intersection
inter = LineSeg2D(Point2D(4, 1), Point2D(4, 5)).intersection(
LineSeg2D(Point2D(4, 7), Point2D(4, 5)))
assert_is(inter.intersects, True)
assert_eq(inter.point, Point2D(4, 5))
assert_eq(inter.vect, Vect2D(0, 0))
assert_eq(inter.lineSeg, LineSeg2D(Point2D(4, 5), Point2D(4, 5)))
# parallel, vertical, full intersection
inter = LineSeg2D(Point2D(4, 1), Point2D(4, 7)).intersection(
LineSeg2D(Point2D(4, 1), Point2D(4, 7)))
assert_is(inter.intersects, True)
assert_eq(inter.point, Point2D(4, 1))
assert_eq(inter.vect, Vect2D(0, 6))
assert_eq(inter.lineSeg, LineSeg2D(Point2D(4, 1), Point2D(4, 7)))
# parallel, vertical, full intersection
inter = LineSeg2D(Point2D(4, 7), Point2D(4, 1)).intersection(
LineSeg2D(Point2D(4, 7), Point2D(4, 1)))
assert_is(inter.intersects, True)
assert_eq(inter.point, Point2D(4, 7))
assert_eq(inter.vect, Vect2D(0, -6))
assert_eq(inter.lineSeg, LineSeg2D(Point2D(4, 7), Point2D(4, 1)))
# parallel, vertical, full intersection
inter = LineSeg2D(Point2D(4, 1), Point2D(4, 7)).intersection(
LineSeg2D(Point2D(4, 7), Point2D(4, 1)))
assert_is(inter.intersects, True)
assert_eq(inter.point, Point2D(4, 1))
assert_eq(inter.vect, Vect2D(0, 6))
assert_eq(inter.lineSeg, LineSeg2D(Point2D(4, 1), Point2D(4, 7)))
# parallel, vertical, full intersection
inter = LineSeg2D(Point2D(4, 7), Point2D(4, 1)).intersection(
LineSeg2D(Point2D(4, 1), Point2D(4, 7)))
assert_is(inter.intersects, True)
assert_eq(inter.point, Point2D(4, 7))
assert_eq(inter.vect, Vect2D(0, -6))
assert_eq(inter.lineSeg, LineSeg2D(Point2D(4, 7), Point2D(4, 1)))
# AB vertical, CD horizontal, intersecting
inter = LineSeg2D(Point2D(4, 1), Point2D(4, 8)).intersection(
LineSeg2D(Point2D(2, 5), Point2D(12, 5)))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment