def jarvis_test(n=1000): errors = 0 for i in range(n): print(end=str(i) + " ") points = [ Point(random.uniform(-10, 10), random.uniform(-10, 10)) for i in range(30) ] carcass = jarvis(points) state = is_convex(carcass) for point in points: if is_inside(carcass, point) == "inside" or is_inside( carcass, point) == "on": pass else: state = False break if state: pass # print("ok") else: errors += 1 draw_figure(carcass) for p in carcass: plt.plot(p.x, p.y, 'ro', color=(0, 0, 0)) plt.plot(carcass[0].x, carcass[0].y, 'ro', color=(1, 0, 0)) for p in points: plt.plot(p.x, p.y, 'ro') plt.show() print("error") print() return errors
def test_inside(func, figure=[ Point(-1, -1), Point(-5, 3), Point(0, 11), Point(6, 13), Point(10, 4.5), Point(4, -3) ], range_number=100): draw_figure(figure) random.seed(1) for i in range(range_number): given_point = Point(random.randint(-5, 10), random.randint(-5, 10)) #-2,0 func_value = func(figure, given_point) if func_value == "on": plt.plot(given_point.x, given_point.y, 'ro', color=(0, 0, 0), markersize=4) elif func_value == "inside": plt.plot(given_point.x, given_point.y, 'ro', markersize=4) else: plt.plot(given_point.x, given_point.y, 'ro', color=(0, 0.5, 0.3), markersize=4) plt.show()
def test_inside(func, figure=[ Point(-1, -1), Point(-5, 3), Point(0, 11), Point(6, 13), Point(10, 4.5), Point(4, -3) ], range_number=100): draw_figure(figure) random.seed(1) mistakes_counter = 0 for i in range(range_number): given_point = Point(random.randint(-5, 10), random.randint(-5, 10)) #-2,0 func_value = func(figure, given_point) standard_value = is_inside(figure, given_point) if func_value != standard_value: print("ABORT", given_point, "Returned:", func_value, "Expected:", standard_value) plt.plot(given_point.x, given_point.y, 'ro', color=(0.5, 0.5, 0.5), markersize=5) continue mistakes_counter += 1 if func_value == "on": plt.plot(given_point.x, given_point.y, 'ro', color=(0, 0, 0), markersize=4) elif func_value == "inside": plt.plot(given_point.x, given_point.y, 'ro', markersize=4) else: plt.plot(given_point.x, given_point.y, 'ro', color=(0, 0.5, 0.3), markersize=4) print("There is no incompatibilities" if mistakes_counter == 0 else str(mistakes_counter) + " incompatibilities") plt.show()
def diameter_search_test(n=1000): errors = 0 for i in range(n): print(end=str(i) + " ") points = [ Point(random.uniform(-10, 10), random.uniform(-10, 10)) for i in range(30) ] d_max, d1, d2 = diameter_search(points) n = len(points) c = int(factorial(n) / factorial(n - 2) / 2) for i in range(c * 5): p1 = random.choice(points) p2 = random.choice(points) dis = distance(p1, p2) if dis > d_max: print(d_max, dis) carcass = jarvis(points) draw_figure(carcass) draw_figure((d1, d2)) draw_figure((p1, p2)) plt.show() errors += 1 break print() return errors
] simple_figure = [ Point(10, 10), Point(40, 50), Point(10, 80), Point(50, 70), Point(70, 90), Point(60, 30), Point(90, 40), Point(60, 20), Point(40, 35) ] new_points = [] for point in points: if convex_is_inside(convex_figure, point) == "inside": new_points.append(point) point.set_random_speed(1, seed=100) points = new_points mat, = ax.plot([point.x for point in points], [point.y for point in points], 'o', markersize=4) anim = FuncAnimation( fig, animate_points, frames=100, interval=1, blit=True) # blit=True means only re-draw the parts that have changed. draw_figure(simple_figure) draw_figure(convex_figure) plt.show()
from lab1_2 import is_intersect from lab1_3 import is_simple, draw_figure from copy import deepcopy def is_convex(original_points): flag = True if is_simple(original_points): points = deepcopy(original_points) points = points + [points[0]] + [points[1]] for i in range(0, len(points) - 2): temp = define_orientation(points[i], points[i + 1], points[i + 2]) if temp != "on": try: if temp != last: return False except: pass last = temp return True else: return False if __name__ == "__main__": points = [] for i in range(0, 4): # random.randint(3,6) points.append(Point(random.randint(0, 20), random.randint(0, 20))) print("Convex", is_convex(points)) draw_figure(points) plt.show()
d_max = 0, p, q counter = 0 while q != carcass[0] and counter < n: p = P.next(p) d_max = (distance(p, q), p, q) if distance(p, q) >= d_max[0] else d_max while S(p, P.next(p), P.next(q)) > \ S(p, P.next(p), q): counter += 1 q = P.next(q) if distance(p, q) != distance(q0, carcass[0]): d_max = (distance(p, q), p, q) if distance(p, q) >= d_max[0] else d_max # if S(p, P.next(p), P.next(q)) == S(p, P.next(p), q): # if distance(p, q) != (q0, carcass[-1]): # d_max = (distance(p, P.next(q)), p, P.next(q)) if distance(p, P.next(q)) >= d_max[0] else d_max return d_max if __name__ == "__main__": random.seed(9) #9 1 points = [ Point(random.randint(-10, 10), random.randint(-10, 10)) for i in range(30) ] # points = [Point(0,0), Point(10,0),Point(0,10),Point(10,10)] d_max = diameter_search(points) print(d_max[0]) draw_figure(d_max[1:]) draw_figure(jarvis(points)) plt.show()
if S(P, Q, point) > max: max = S(P, Q, point) max_point = point C = max_point convex_hull.insert(convex_hull.index(P), C) segment_1, segment_2 = [], [ ] # segment_1 is to the right side of PC, segment_2 is to the right side of the CQ for point in segment: if define_orientation(P, C, point) == "right": segment_1.append(point) if define_orientation(C, Q, point) == "right": segment_2.append(point) find_hull(segment_1, P, C, convex_hull) find_hull(segment_2, C, Q, convex_hull) if __name__ == "__main__": random.seed(10) points = [ Point(random.uniform(-10, 10), random.uniform(-10, 10)) for i in range(30) ] plt.plot([i.x for i in points], [i.y for i in points], 'ro', color=(0.5, 0.5, 0)) hull = quick_hull(points) plt.plot([i.x for i in hull], [i.y for i in hull], 'ro', color=(0.5, 0.5, 1)) draw_figure(hull) plt.show()
if __name__ == "__main__": segment_list = [[Point(2, 5), Point(4, 6)], [Point(-2, 2), Point(0, 4)], [Point(-4, 4), Point(4, 4)], [Point(-4, 2), Point(0.1, 4.4)], [Point(0, 6), Point(4, 6)], [Point(2, 2), Point(4, 5)], [Point(0, 5), Point(0, -5)]] point_list = [[Point(-3, 0), Point(-5, 6), Point(4, 3), Point(1, 0)], [Point(-3, 0), Point(0, 4), Point(1, 6), Point(-2, 4.5)]] # descr_list = ["Выход с обеих сторон", "Не входит с обеих сторон", "Входит с обеих сторон", "Выходит с одной стороны","desc", "desc2","desc3","desc4"] for j in range(len(point_list)): for i in range(len(segment_list)): # print(descr_list[i]) print("Case {0}:".format(i)) draw_figure(segment_list[i]) draw_figure(point_list[j]) t1, t2 = alg_Cyrus_Beck(segment_list[i], point_list[j]) print(t1, t2) draw_figure(change_line_with_params(segment_list[i], t1, t2)) plt.show()
def jarvis(original_points): points = deepcopy(original_points) points = [i for n, i in enumerate(points) if i not in points[:n]] core_point = min(points, key=attrgetter('y')) carcass = [core_point] while True: p1 = find_max_right(core_point, points) core_point = p1 points.remove(core_point) if p1 == carcass[0]: break carcass.append(p1) return carcass if __name__ == "__main__": random.seed(32) # 100 points = [ Point(random.uniform(-10, 10), random.uniform(-10, 10)) for i in range(30) ] carcass = jarvis(points) print(len(carcass)) draw_figure(carcass) for point in points: plt.plot(point.x, point.y, 'ro') for point in carcass: plt.plot(point.x, point.y, 'ro', color=(0, 0, 0)) plt.plot(carcass[0].x, carcass[0].y, 'ro', color=(1, 0, 0)) plt.show()