Esempio n. 1
0
 def __calculateIntersection(self):
     if self.__line1 is not None and self.__line2 is not None:
         check = LineSegment.checkSegmentIntersection(
             self.__line1, self.__line2)
         if check:
             self.__status_text = 'Line Segments intersect with each other.'
             self.__intersection_point = LineSegment.lineSegmentsIntersectionPoint(
                 self.__line1, self.__line2)
         else:
             self.__status_text = 'Line Segment doesn\'t intersect with each other.'
             self.__intersection_point = None
Esempio n. 2
0
def anySegmentsIntersect(lines):
    S = []
    for i in range(len(lines)):
        if lines[i].start.x > lines[i].end.x or (
                lines[i].start.x == lines[i].end.x
                and lines[i].start.y > lines[i].end.y):
            end = lines[i].start
            start = lines[i].end
            lines[i] = LineSegment(start, end)
        pt1 = Point(lines[i].start.x, lines[i].start.y, 0)
        S.append(pt1)
        pt2 = Point(lines[i].end.x, lines[i].end.y, 1)
        S.append(pt2)

        pt1.other_end = pt2
        pt2.other_end = pt1

    sortedPoints = sorted(S, key=cmp_to_key(compare))

    dict = {}
    for pt in sortedPoints:
        dict[pt] = Node(pt)

    T = RedBlackTree()

    # Step 3
    for pt in sortedPoints:
        nd = dict[pt]
        # print(T)
        # print('node : ', nd, '\tp: ', nd.parent.key, '\tl: ', nd.left.key, '\tr: ', nd.right.key)
        if nd.key.ptype == 0:
            T.insert(nd)
            line1 = LineSegment(nd.key, nd.key.other_end)
            prd = T.predecessor(nd)
            if prd:
                line2 = LineSegment(prd.key, prd.key.other_end)
                if LineSegment.checkSegmentIntersection(line1, line2):
                    return (line1, line2)
            else:
                print('pred not found : ', nd)

            ssc = T.successor(nd)
            if ssc:
                line2 = LineSegment(ssc.key, ssc.key.other_end)
                if LineSegment.checkSegmentIntersection(line1, line2):
                    return (line1, line2)
            else:
                print('succ not found : ', nd)
        if nd.key.ptype == 1:
            T.delete(dict[nd.key.other_end])
            T.insert(nd)
            prd = T.predecessor(nd)
            ssc = T.successor(nd)
            if prd and ssc:
                line1 = LineSegment(prd.key, prd.key.other_end)
                line2 = LineSegment(ssc.key, ssc.key.other_end)
                print(line1, '\t', line2)
                if LineSegment.checkSegmentIntersection(line1, line2):
                    print('intersect : ', T.size)
                    return (line1, line2)
                else:
                    print('does not intersect')
            else:
                print('prev or succ not found : ', nd)
            T.delete(nd)
    return None
Esempio n. 3
0
def sweepLine(lines):
    """
        Return:
            lines (LineSegment)
        Args:
            lines (LineSegment)

        find all intersected line pairs
    """
    pq = PriorityQueue()
    for line in lines:
        pq.put((line.start.x, line.start.y, 0))
        pq.put((line.end.x, line.end.y, 1))

    # points = linesToSortedPoints(lines)

    print(len(points))
    for pt in points:
        print(pt[0], pt[1])

    active_lines = AVLTree()
    intersectedLines = []

    for i in range(len(points)):
        pt = points[i]

        line1 = pt[2]
        active_lines.insert((pt[0].y, pt[0].x), line1)

        if pt[1] == True:
            if active_lines.count <= 1:
                continue
            try:
                prev = active_lines.prev_key((pt[0].y, pt[0].x))
                line2 = active_lines[prev]
                check = LineSegment.checkSegmentIntersection(line1, line2)
                print(line1, line2, check)
                if check:
                    intersectedLines.extend([line1, line2])
            except:
                print(f'{pt[0]} twa da 1!')

            try:
                succ = active_lines.succ_key((pt[0].y, pt[0].x))
                line2 = active_lines[succ]
                check = LineSegment.checkSegmentIntersection(line1, line2)
                print(line1, line2, check)
                if check:
                    intersectedLines.extend([line1, line2])
            except:
                print(f'{pt[0]} twa da 2!')
        else:
            if pt[3]:
                active_lines.remove((pt[2].end.y, pt[2].end.x))
            else:
                active_lines.remove((pt[2].start.y, pt[2].start.x))

            if active_lines.count > 2:
                try:
                    prev = active_lines.prev_key((pt[0].y, pt[0].x))
                    line1 = active_lines[prev]
                    try:
                        succ = active_lines.succ_key((pt[0].y, pt[0].x))
                        line2 = active_lines[succ]
                        check = LineSegment.checkSegmentIntersection(
                            line1, line2)
                        print(line1, line2, check)
                        if check:
                            intersectedLines.extend([line1, line2])
                    except:
                        print(f'{pt[0]} twa da 3!')
                except:
                    print(f'{pt[0]} twa da 4!')

            active_lines.remove((pt[0].y, pt[0].x))
    print(f'active lines : {active_lines.count}')
    active_lines.clear()

    return intersectedLines