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
def graham_scan(input_list: List[point]) -> Polygon: # phase1 - sorting points input_list1 = get_simple_polygon(input_list) # phase2 - creating list of points who generate convex polygon s = Stack() s.push(input_list1[0]) s.push(input_list1[1]) s.push(input_list1[2]) for i in range(3, len(input_list)): while orientation(s.next_to_top(), s.peek(), input_list1[i]) < 0: s.pop() # print(s.next_to_top(), s.peek(), input_list1[i]) s.push(input_list1[i]) l = [] while not s.is_empty(): l.append(s.peek()) s.pop() return Polygon(l)
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
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)
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
def generate_polygon() -> Polygon: n = random.randint(3, 20) i = 0 l = [] while i < n: x = random.randint(-300, 300) y = random.randint(-300, 300) l.append(point(x, y)) i += 1 return Polygon(get_simple_polygon(l))
def earclip(filename): ear_vertex = [] triangles = [] polygon = Polygon(create_coordinates(filename)) if _is_clockwise(polygon): polygon.points.reverse() point_count = len(polygon.points) for i in range(point_count): prev_index = i - 1 prev_point = polygon.points[prev_index] point = polygon.points[i] next_index = (i + 1) % point_count next_point = polygon.points[next_index] if _is_ear(prev_point, point, next_point, polygon): ear_vertex.append(point) while ear_vertex and point_count >= 3: ear = ear_vertex.pop(0) i = polygon.points.index(ear) prev_index = i - 1 prev_point = polygon.points[prev_index] next_index = (i + 1) % point_count next_point = polygon.points[next_index] polygon.points.remove(ear) point_count -= 1 triangles.append(triangle(prev_point, ear, next_point)) if point_count > 3: prev_prev_point = polygon.points[prev_index - 1] next_next_index = (i + 1) % point_count next_next_point = polygon.points[next_next_index] groups = [ (prev_prev_point, prev_point, next_point, polygon), (prev_point, next_point, next_next_point, polygon), ] for group in groups: p = group[1] if _is_ear(*group): if p not in ear_vertex: ear_vertex.append(p) elif p in ear_vertex: ear_vertex.remove(p) return triangles
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
def get_simple_polygon1(p: Polygon): left_point = sorted(p.points, key=get_x_coordinate)[0] # print(left_point) def _get_tan(point: point) -> Tuple[float, float]: ''' :param point: upon which calcutes tangens and Euclidean distance :return: order of sorting ''' distance = euclid_distance(left_point, point) if point.x == left_point.x: tan = -inf else: tan = (point.y - left_point.y) / (point.x - left_point.x) if point.y == left_point.y: distance *= -1 return tan, distance p.points = sorted(p.points, key=_get_tan)
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
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
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)