Beispiel #1
0
def draw_segments_best_de(filename):
    p = create_coordinates(filename)
    polygon = Polygon(p)

    result = run_de_best_algorithm(filename)

    return is_guard_set(result, polygon)[1], result
Beispiel #2
0
def triangulation_ear_clipping(filename)->List[triangle]:
    triangles = []
    ears = []

    segments = []
    input_list = create_coordinates(filename)
    poly = Polygon(input_list)
    num_of_triangles = len(poly.points) - 2

    if poly.convexPolygon():

        for i in range(0, len(poly.points) - 2):
            triangles.append(triangle(poly.points[0], poly.points[i + 1], poly.points[(i + 2)]))

        return triangles

    else:

        print('else block')
        num_of_points = len(poly.points)
        while len(triangles) < num_of_triangles:


            for i in range(0, len(poly.points)):


                print(i)
                curr_point = poly.points[i]
                prev_point = poly.points[(i - 1) % num_of_points]
                next_point = poly.points[(i + 1) % num_of_points]
                next_next_point = poly.points[(i + 2) % (num_of_points)]
                diagonal = segment(prev_point, next_point)


                if is_convex_vertex( next_point, next_next_point, curr_point):
                    if orientation(next_point, prev_point, curr_point) > 0 and orientation(prev_point, next_point, next_next_point) > 0 and not IntersectionP1(poly, diagonal):
                        ears.append(curr_point)
                else:
                    if orientation(next_point, prev_point, next_next_point) < 0 or orientation(prev_point, next_point, curr_point) < 0 and not IntersectionP1(poly, diagonal):
                        ears.append(curr_point)

            if len(ears) == 0:
                triangles.append(triangle(poly.points[0], poly.points[1], poly.points[2]))
            print('ears', ears)
            for i in range(0, len(ears)):
                print('len ears', len(ears))
                ear = ears[i]
                j = poly.points.index(ear)
                prev_point = poly.points[(j - 1) % (num_of_points)]
                next_point = poly.points[(j + 1) % (num_of_points)]
                triangles.append(triangle(prev_point, ear, next_point))
                poly.points.remove(ear)
                num_of_points -= 1
            print('len of triangles', len(triangles))
            ears.clear()


        return triangles
Beispiel #3
0
def run_de_best_algorithm(filename) -> List[point]:
    start_time = time.time()
    p = create_coordinates(filename)
    polygon = Polygon(p)
    init_population = create_init_population(reflex_vertices(polygon), polygon)
    result = de_best_1_bin(init_population[0], init_population[1], polygon)

    final = [result]

    # print('init', final)
    #
    # print(len(result[1]))
    # for i in range(0, len(result[1])):
    #     print(result[1][i].count(1))

    #print('num of reflex', len(reflex_vertices(polygon)))

    guards = []
    h = 30
    for i in range(0, h):

        result1 = de_rand_1_bin(final[i][0], final[i][1], polygon)
        final.append(result1)
        guards = [result1[0]]
    min = 1000
    max = 0
    position = 0
    srednja = 0
    pos_max = 0
    test = 0
    for k in range(0, len(final[h][1])):
        test = len(postprocessing(final[h][0][k], polygon))
        min_k = final[h][1][k].count(1)
        max_k = final[h][1][k].count(1)
        print('mink', min_k)
        # if min > min_k:
        #     min = min_k
        #     position = k
        #     guards = final[h][0][k].copy()
        if min > test:
            min = test
            position = k
        # if max < max_k:
        #     max = max_k
        #     pos_max = k
        if max < test:
            max = test
        srednja = srednja + test

    print("--- %s seconds ---" % (time.time() - start_time))

    print('postprocessing')
    #print(postprocessing(final[h][0][position], polygon))
    #print('final result:', postprocessing(final[h][0][position], polygon))
    print('max guards', max)
    print('srednja ', srednja / len(final[h][1]))
    print('min guards', min)
    return postprocessing(final[h][0][position], polygon)
Beispiel #4
0
def find_guards_inside_random_triangle(filename) -> Tuple[List[point], List[segment]]:

    input_list = create_coordinates(filename)
    segments = []
    polygon = Polygon(input_list)
    #print('poly.points', len(polygon.points))

    triangles = earclip(filename)

    #print('poly.points', len(polygon.points))

    guards = []
    visible_vertex = []


    visited_triangles = []

    while len(visible_vertex) < len(polygon.points):

        #print("novi while")
        n = random.randint(0, len(triangles)-1)
        rand_triangle = triangles[n]
        while rand_triangle in visited_triangles and len(visited_triangles) < 18:
            n = random.randint(0, len(triangles)-1)
            rand_triangle = triangles[n]
        visited_triangles.append(rand_triangle)

        #print("izabrao random trougao")
        rand_point = point_on_triangle(rand_triangle.first, rand_triangle.second, rand_triangle.third)

        for vertex in polygon.points:
            #print("vertex", vertex)

            if vertex not in visible_vertex:
                s = segment(rand_point, vertex)
                p1 = point((vertex.x + rand_point.x)/2, (vertex.y+rand_point.y)/2)

                if not IntersectionP1(polygon, s) and PointInsidePolygon(p1, polygon):

                    #print("provjerio za jedan vertex")
                    visible_vertex.append(vertex)
                    segments.append(s)
                    if rand_point not in guards:
                        guards.append(rand_point)
                        #print("dosao do guarda")
                    else:
                        for i in range(0, len(polygon.points)):
                            seg = segment(polygon.points[i], polygon.points[(i + 1) % len(polygon.points)])
                            if same_segments(seg, s):
                                visible_vertex.append(vertex)
                                segments.append(s)
    # for t in triangles:
    #     t.draw()


    return guards, segments
Beispiel #5
0
def find_center_guards_in_random_triangle(filename) -> List[point]:

    input_list = create_coordinates(filename)

    polygon = Polygon(input_list)
    #print('poly.points', len(polygon.points))

    triangles = earclip(filename)

    #print('poly.points', len(polygon.points))

    guards = []
    visible_vertex = []


    visited_triangles = []

    while len(visible_vertex) < len(polygon.points):

        #print("novi while")
        n = random.randint(0, len(triangles)-1)
        rand_triangle = triangles[n]
        while rand_triangle in visited_triangles and len(visited_triangles) < 18:
            n = random.randint(0, len(triangles)-1)
            rand_triangle = triangles[n]
        visited_triangles.append(rand_triangle)

        #print("izabrao random trougao")

        rand_point = point((rand_triangle.first.x + rand_triangle.second.x + rand_triangle.third.x)/3, (rand_triangle.first.y +rand_triangle.second.y+rand_triangle.third.y)/3)

        for vertex in polygon.points:
            #print("vertex", vertex)

            if vertex not in visible_vertex:
                s = segment(rand_point, vertex)
                p1 = point((vertex.x + rand_point.x)/2, (vertex.y+rand_point.y)/2)

                if not IntersectionP1(polygon, s) and PointInsidePolygon(p1, polygon):

                    #print("provjerio za jedan vertex")
                    visible_vertex.append(vertex)
                    if rand_point not in guards:
                        guards.append(rand_point)
                        #print("dosao do guarda")
    # for t in triangles:
    #     t.draw()

    return guards
Beispiel #6
0
def run_naive_algorithm(filename)-> List[point]:

    input_list = create_coordinates(filename)
    guards = []
    visible_vertex = []

    polygon = Polygon(input_list)

    polygon_vertices = input_list

    while len(visible_vertex) < len(polygon_vertices):

        print("novi while")
        random_guard = generate_point_inside_polygon(filename, polygon)
        print("izabrao random tacku")

        for vertex in polygon_vertices:
            print("vertex", vertex)

            if vertex not in visible_vertex:
                s = segment(random_guard, vertex)
                p1 = point((vertex.x + random_guard.x)/2, (vertex.y+random_guard.y)/2)

                if not IntersectionP1(polygon, s) and PointInsidePolygon(p1, polygon):
                    visible_vertex.append(vertex)
                    if random_guard not in guards:
                        guards.append(random_guard)
                        print("dosao do guarda")

                    # slucaj kada se segmenti preklapaju, tj kada cuvar sa refleksne ivice vidi susjedni vrh
                else:
                    for i in range(0, len(polygon.points)):
                        seg = segment(polygon.points[i], polygon.points[(i + 1) % len(polygon.points)])
                        if same_segments(seg, s):
                            visible_vertex.append(vertex)

    return guards
Beispiel #7
0
def ear_clipping(filename) -> List[triangle]:
    triangles = []
    ears = []
    segments = []
    input_list = create_coordinates(filename)
    poly = Polygon(input_list)

    if poly.convexPolygon():

        for i in range(0, len(poly.points) - 2):
            triangles.append(triangle(poly.points[0], poly.points[i + 1], poly.points[(i + 2)]))

        return triangles

    else:
        num_of_points = len(poly.points)

        # for i in range(num_of_points):
        #
        #     prev_vertex = poly.points[(i - 1) % num_of_points]
        #     curr_vertex = poly.points[i]
        #     next_vertex = poly.points[(i + 1) % num_of_points]
        #
        #     if is_ear(prev_vertex, curr_vertex, next_vertex, poly):
        #         ears.append(curr_vertex)
        #
        # while ears and num_of_points >= 3:
        #
        #     ear = ears.pop(0)
        #     i = poly.points.index(ear)
        #     prev_point = poly.points[(i - 1) % (num_of_points)]
        #     next_point = poly.points[(i + 1) % (num_of_points)]
        #
        #     poly.points.remove(ear)
        #
        #     # izbrisemo jednu tacku, na poziciji 'i' pa i+2 nakon brisanja postaje i + 1 -- next_next_vertex
        #     num_of_points -= 1
        #     triangles.append(triangle(prev_point, ear, next_point))
        #
        #     if num_of_points > 3:
        #
        #         prev_prev_point = poly.points[(i - 2) % num_of_points]
        #         next_next_point = poly.points[(i + 1) % num_of_points]
        #
        #         if is_ear(prev_prev_point, prev_point, next_point, poly):
        #             if prev_point not in ears:
        #                 ears.append(prev_point)
        #         elif prev_point in ears:
        #             ears.remove(prev_point)
        #
        #         if is_ear(prev_point, next_point, next_next_point, poly):
        #             if next_point not in ears:
        #                 ears.append(next_point)
        #         elif next_point in ears:
        #             ears.remove(next_point)
        # print(len(triangles))
        # return triangles
        i = 0
        while num_of_points >= 3:

            if is_ear_1(poly.points[(i-2) % num_of_points], poly.points[(i-1) % num_of_points], poly.points[i], poly.points[(i+1) % num_of_points], poly):
                triangles.append(triangle(poly.points[(i-1) % num_of_points], poly.points[i], poly.points[(i+1)%num_of_points]))
                poly.points.remove(i)
                num_of_points = num_of_points - 1
                i = 0
            else:
                i = i + 1
        return triangles
Beispiel #8
0
def run_de_algorithm(filename) -> List[point]:
    start_time = time.time()
    p = create_coordinates(filename)
    polygon = Polygon(p)
    init_population = create_init_population(reflex_vertices(polygon), polygon)
    result = de_rand_1_bin(init_population[0], init_population[1], polygon)

    final = [result]

    # print('init', final)
    #
    # print(len(result[1]))
    # for i in range(0, len(result[1])):
    #     print(result[1][i].count(1))

    #print('num of reflex', len(reflex_vertices(polygon)))

    guards = []
    h = 30
    t = time.time()
    avr_time = 0
    avr_time_post = 0
    for i in range(0, h):
        start_time = time.time()
        start_time_1 = time.time()
        result1 = de_rand_1_bin(final[i][0], final[i][1], polygon)
        avr_time = avr_time + (time.time() - start_time)
        final.append(result1)
        #guards = [result1[0]]
    min = 1000
    max = 0
    position = 0
    srednja = 0
    pos_max = 0

    for k in range(0, len(final[h][1])):
        test = len(postprocessing(final[h][0][k], polygon))
        guards.append(test)
        min_k = final[h][1][k].count(1)
        max_k = final[h][1][k].count(1)
        #print('mink', min_k)
        # if min > min_k:
        #     min = min_k
        #     position = k
        #     guards = final[h][0][k].copy()
        if min > test:
            min = test
            position = k
        # if max < max_k:
        #     max = max_k
        #     pos_max = k
        if max < test:
            max = test
        srednja = srednja + test
    avr_time_post = avr_time_post + start_time_1
    #pos_max = k
    #print('len', len(final[h][1]))
    #print(min)
    #print(position)
    #print('len of guards', len(guards))
    # print(final[h][0])
    # print(final[h][1])

    #print('max guards:', len(postprocessing(final[h][0][pos_max], polygon)), postprocessing(final[h][0][pos_max], polygon))
    #print('min guards -final result:', postprocessing(final[h][0][position], polygon))
    print('guards', guards)
    print('max guards', max)
    print('srednja ', srednja / len(final[h][1]))
    print('min guards', min)
    print('avg bez postprocesinga', avr_time / h)
    print('avg sa postprocesing', avr_time_post / h)
    print("--- %s seconds ---" % (time.time() - t))
    return postprocessing(final[h][0][position], polygon)