Пример #1
0
def polar_angle(p0, point_list):
    '''sort a sequence <p1, p2, ... , pn> of n points according to
    their polar angles with respect to a given origin point p0.
    '''
    v0 = vector((p0[0] + 1, p0[1]), p0)    # The polar angle of v0 is 0
    vector_list = [vector(p, p0) for p in point_list]
    angle_0 = [] #list of vectors whose polar angles are  0
    angle_pi = [] #list of vectors whose polar angles are pi
    angle_0_pi = [] #list of vectors whose polar angles are larger than 0 and smaller than pi
    angle_pi_2pi = [] #list of vectors whose polar angles are larger than pi and smaller than 2pi
    for v in vector_list:
        if v == v0:
            if v.x > 0:
                angle_0.append(v)
            elif v.x < 0:
                angle_pi.append(v)
        elif v < v0:
            angle_pi_2pi.append(v)
        elif v > v0:
            angle_0_pi.append(v)
    heap_0_pi = max_heap(angle_0_pi)
    heap_pi_2pi = max_heap(angle_pi_2pi)
    heap_0_pi.heapsort()
    heap_pi_2pi.heapsort()
    return [(v.x, v.y) for v in (angle_0 + heap_0_pi + angle_pi + heap_pi_2pi)]
Пример #2
0
def polar_angle(p0, point_list):
    '''sort a sequence <p1, p2, ... , pn> of n points according to
    their polar angles with respect to a given origin point p0.
    '''
    v0 = vector((p0[0] + 1, p0[1]), p0)  # The polar angle of v0 is 0
    vector_list = [vector(p, p0) for p in point_list]
    angle_0 = []  #list of vectors whose polar angles are  0
    angle_pi = []  #list of vectors whose polar angles are pi
    angle_0_pi = [
    ]  #list of vectors whose polar angles are larger than 0 and smaller than pi
    angle_pi_2pi = [
    ]  #list of vectors whose polar angles are larger than pi and smaller than 2pi
    for v in vector_list:
        if v == v0:
            if v.x > 0:
                angle_0.append(v)
            elif v.x < 0:
                angle_pi.append(v)
        elif v < v0:
            angle_pi_2pi.append(v)
        elif v > v0:
            angle_0_pi.append(v)
    heap_0_pi = max_heap(angle_0_pi)
    heap_pi_2pi = max_heap(angle_pi_2pi)
    heap_0_pi.heapsort()
    heap_pi_2pi.heapsort()
    return [(v.x, v.y) for v in (angle_0 + heap_0_pi + angle_pi + heap_pi_2pi)]
Пример #3
0
def any_disks_intersect(S):
    '''This algorithm takes as input a set S of n disks represented by its center point and radius, returning the boolean value TRUE if any pair of disks in S intersects, and FALSE otherwise.'''
    T = rb_tree()
    point_list = []
    disk_list = []
    for s in S:
        disk_list.append(disk(s))
    for s in disk_list:
        center_point = s[0]
        radius = s[1]
        x = center_point[0]
        y = center_point[1]
        point_list.append(point([x - radius, 0, y], s))
        point_list.append(point([x + radius, 1, y], s))
    heap_point = max_heap(point_list)
    heap_point.heapsort()
    print heap_point
    for p in heap_point:
        if p[1] == 0:
            s = p.disk
            T.insert(s)
            a = T.above(s)
            b = T.below(s)
            print "insert: ", a, b
            if (a != None and disks_intersect(a, s)) or (b != None and disks_intersect(b, s)):
                return True
        if p[1] == 1:
            s = p.disk
            a = T.above(s)
            b = T.below(s)
            if a != None and b != None and disks_intersect(a, b):
                return True
            T.delete(s)
    return False
Пример #4
0
def any_segments_intersect(S):
    '''This algorithm takes as input a set S of n line segments, returning the boolean value TRUE if any pair of segments in S intersects, and FALSE otherwise.'''
    T = rb_tree()
    segment_list = []
    point_list = []
    for s in S:
        segment_list.append(segment(s))
    for s in segment_list:
        point_list.append(point([s[0][0], 0, s[0][1]], s))    
        point_list.append(point([s[1][0], 1, s[1][1]], s))    
    heap_point = max_heap(point_list)
    heap_point.heapsort()
    for p in heap_point:
        if p[1] == 0:
            s = p.segment
            T.insert(s)
            a = T.above(s)
            b = T.below(s)
            if (a != None and segments_intersect(a[0], a[1], s[0], s[1])) or (b != None and segments_intersect(b[0], b[1], s[0], s[1])):
                return True
        if p[1] == 1:
            s = p.segment
            a = T.above(s)
            b = T.below(s)
#            print( a)
#            print( b)
#            print( type(a))
#            print( type(b))
            if a != None and b != None and segments_intersect(a[0], a[1], b[0], b[1]):
                return True
            T.delete(s)
    return False
Пример #5
0
def any_segments_intersect(S):
    '''This algorithm takes as input a set S of n line segments, returning the boolean value TRUE if any pair of segments in S intersects, and FALSE otherwise.'''
    T = rb_tree()
    segment_list = []
    point_list = []
    for s in S:
        segment_list.append(segment(s))
    for s in segment_list:
        point_list.append(point([s[0][0], 0, s[0][1]], s))    
        point_list.append(point([s[1][0], 1, s[1][1]], s))    
    heap_point = max_heap(point_list)
    heap_point.heapsort()
    for p in heap_point:
        if p[1] == 0:
            s = p.segment
            T.insert(s)
            a = T.above(s)
            b = T.below(s)
            if (a != None and segments_intersect(a[0], a[1], s[0], s[1])) or (b != None and segments_intersect(b[0], b[1], s[0], s[1])):
                return True
        if p[1] == 1:
            s = p.segment
            a = T.above(s)
            b = T.below(s)
#            print a
#            print b
#            print type(a)
#            print type(b)
            if a != None and b != None and segments_intersect(a[0], a[1], b[0], b[1]):
                return True
            T.delete(s)
    return False
def y_sort(S):
    Y = []
    for p in S:
        Y.append((p[1], p[0]))
    YY = max_heap(Y)
    YY.heapsort()
    Y = []
    for i in range(0, len(YY)):
        Y.append((YY[i][1], YY[i][0]))
    return Y
Пример #7
0
def y_sort(S):
    Y = []
    for p in S:
        Y.append((p[1], p[0]))
    YY = max_heap(Y)
    YY.heapsort()
    Y = []
    for i in range(0, len(YY)):
        Y.append((YY[i][1], YY[i][0]))
    return Y
Пример #8
0
def any_disks_intersect(S):
    """
    This algorithm takes as input a set S of n disks represented by its center point and radius, returning the boolean value TRUE if any pair of disks in S intersects, and FALSE otherwise.
    :param S:
    :return:
    """
    T = rb_tree()
    point_list = []
    disk_list = []
    for s in S:
        disk_list.append(disk(s))
    for s in disk_list:
        center_point = s[0]
        radius = s[1]
        x = center_point[0]
        y = center_point[1]
        point_list.append(point([x - radius, 0, y], s))
        point_list.append(point([x + radius, 1, y], s))
    heap_point = max_heap(point_list)
    heap_point.heapsort()
    print(heap_point)
    for p in heap_point:
        if p[1] == 0:
            s = p.disk
            T.insert(s)
            a = T.above(s)
            b = T.below(s)
            print("insert: ", a, b)
            if (a is not None
                    and disks_intersect(a, s)) or (b is not None
                                                   and disks_intersect(b, s)):
                return True
        if p[1] == 1:
            s = p.disk
            a = T.above(s)
            b = T.below(s)
            if a is not None and b is not None and disks_intersect(a, b):
                return True
            T.delete(s)
    return False
Пример #9
0
def three_points_colinear(points_list):
    '''An algorithm to determine whether any three points in a set of n points are colinear'''
    n = len(points_list)
    vectors_list = []
    for i in range(n):
        for j in range(i + 1, n):
            vectors_list.append(vector(points_list[i], points_list[j], i, j))
    v0 = vector((1, 0), (0, 0))
    heap_vectors = max_heap(vectors_list)
    heap_vectors.heapsort()
    status = [False] * n
    v = heap_vectors[0]
    status[v.p1_index] = True
    status[v.p2_index] = True
    stack = []
    stack.append(v.p1_index)
    stack.append(v.p2_index)
    for i in range(1, len(heap_vectors)):
        v = heap_vectors[i]
        if v == heap_vectors[i - 1]:
            if status[v.p1_index]:
                return True
            elif status[v.p2_index]:
                return True
            else:
                status[v.p1_index] = True
                status[v.p2_index] = True
                stack.append(v.p1_index)
                stack.append(v.p2_index)
        else:
            print(len(stack))
            print(stack)
            for i in range(len(stack)):
                status[stack.pop()] = False
            stack.append(v.p1_index)
            stack.append(v.p2_index)
    return False
def x_sort(S):
    X = max_heap(S)
    X.heapsort()
    return X
Пример #11
0
def x_sort(S):
    X = max_heap(S)
    X.heapsort()
    return X
Пример #12
0
 def test_max_heapify(self):
     a = [16, 4, 10, 14, 7, 9, 3, 2, 8, 1]
     h = max_heap(a)
     h.max_heapify(1)
     self.assertEquals(h, [16, 14, 10, 8, 7, 9, 3, 2, 4, 1])
Пример #13
0
 def test_heapsort(self):
     a = [4, 1, 3, 3, 16, 9, 10, 14, 8, 7]
     h = max_heap(a)
     h.heapsort()
     self.assertEquals(h, [1, 3, 3, 4, 7, 8, 9, 10, 14, 16])
Пример #14
0
 def test_build_max_heap(self):
     a = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7]
     h = max_heap(a)
     h.build_max_heap()
     self.assertEquals(h, [16, 14, 10, 8, 7, 9, 3, 2, 4, 1])