def getSector(p, dot): start = 0 end = len(p) - 1 while end - start > 1: sep = (start + end) // 2 if point_relative(p[0], p[sep], dot) == -1: start = sep else: end = sep return start, end
def recursion_for_qh_algorithm1(ch, SL, sl, sr): if len(SL) == 0: return ch square = 0 SRnew = [] SLnew = [] max = SL[0] for i in range(len(SL)): if square < triangle_area(sl, sr, SL[i]): square = triangle_area(sl, sr, SL[i]) max = SL[i] # ch.append(max) SL.remove(max) for i in range(len(SL)): if point_relative(sl, max, SL[i]) != -1: SLnew.append(SL[i]) if point_relative(max, sr, SL[i]) != -1: SRnew.append(SL[i]) recursion_for_qh_algorithm1(ch, SLnew, sl, max) ch.append(max) recursion_for_qh_algorithm1(ch, SRnew, max, sr)
def is_convex(p): s = 0 g = 0 n = len(p) for i in range(n): k = i + 2 if (i == n - 2): k = 0 if (i == n - 1): k = 1 if point_relative(p[i], p[0], p[k]) < 0: s += 1 if point_relative(p[i], p[0], p[k]) > 0: g += 1 if i != n - 1: if point_relative(p[i], p[i + 1], p[k]) < 0: s += 1 if point_relative(p[i], p[i + 1], p[k]) > 0: g += 1 if g != 0 and s != 0: return False return True
def main_lab1_1(): p1 = Point(random.randint(100, 700), random.randint(100, 700)) p2 = Point(random.randint(100, 700), random.randint(100, 700)) p = Point(random.randint(100, 700), random.randint(100, 700)) point_relative_result = point_relative(p1, p2, p) if point_relative_result == 0: print("on line") else: if point_relative_result < 0: print("right") else: print("left") draw_lab1_1(p1, p2, p)
def QH_algorithm(points): points = points.copy() sl = min_of_points_of_x(points) sr = max_of_points_of_x(points) SL = [] SR = [] square = 0 max = 0 min = 0 SRnew = [] SLnew = [] ch = [] ch.append(sl) # ch.append(sr) points.remove(sl) points.remove(sr) for i in range(len(points)): if point_relative(sl, sr, points[i]) != 1: SR.append(points[i]) else: SL.append(points[i]) # for i in range(len(SL)): # if square < triangle_area(sl, sr, SL[i]): # square = triangle_area(sl, sr, SL[i]) # max = SL[i] # ch.append(max) # SL.remove(max) # for i in range(len(SL)): # if point_relative(sl, max, SL[i]) != -1: # SLnew.append(SL[i]) # if point_relative(max, sr, SL[i]) != -1: # SRnew.append(SL[i]) recursion_for_qh_algorithm1(ch, SL, sl, sr) ch.append(sr) recursion_for_qh_algorithm2(ch, SR, sl, sr) # square = 0 # sl = min_of_points_of_x(points) # sr = max_of_points_of_x(points) # for i in range(len(SR)): # if square < triangle_area(sl, sr, SR[i]): # square = triangle_area(sl, sr, SR[i]) # min = SR[i] # ch.append(min) # SR.remove(min) # recursion_for_qh_algorithm(ch, SRnew, SLnew, sr, min) return ch
def is_in_convex(p, dot): start = 0 end = len(p) - 1 if point_relative(p[0], p[1], dot) != point_relative(p[0], p[1], p[2]) or point_relative( p[len(p) - 1], p[0], dot) != point_relative(p[len(p) - 1], p[0], p[len(p) - 2]): return False while end - start > 1: sep = (start + end) // 2 if point_relative(p[0], p[sep], dot) == point_relative(p[0], p[sep], p[1]): end = sep else: start = sep if is_intersection(p[start], p[end], p[0], dot): return False else: return True
def ray_test(p, dot): S = 0 # plt.scatter(q.x, q.y, marker='.', color='red') # plt.text(q.x + 0.05, q.y + 0.05, 'q', fontsize="15") # ray = Line(q, dot) # ray.drow_line() i = 0 n = len(p) if dimensionalTest(p, dot): q = Point(dimensionalTest(p, dot) - 1, dot.y) while i < n: h = i if i == n - 1: h = 1 p_tmp = p.copy() p_tmp.append(p[0]) if is_intersection(q, dot, p_tmp[i], p_tmp[i + 1]): if point_relative(q, dot, p_tmp[i]) == 0: if point_relative(q, dot, p_tmp[i + 1]) == 0: if point_relative(q, dot, p_tmp[i - 1]) == point_relative( q, dot, p_tmp[h + 2]): S += 1 # i = i + 1 else: if point_relative(q, dot, p_tmp[i - 1]) == point_relative( q, dot, p_tmp[i + 1]): S += 1 else: S += 1 # i = i + 1 i += 1 if S % 2 == 0: return False else: return True else: return False
def dynamic_convex_hull_algorithm(points): points = points.copy() ch = [] result = [] for i in range(len(points)): if i == 0: ch.append(points[i]) ch_copy = ch.copy() result.append(ch_copy) continue if i == 1: if points[0] != points[1]: ch.append(points[1]) ch_copy = ch.copy() result.append(ch_copy) continue if i == 2: if points[2] == points[0] or points[2] == points[1]: continue if points[0] == points[1] and points[0] != points[2]: ch.append(points[i]) ch_copy = ch.copy() result.append(ch_copy) continue if point_relative(points[0], points[1], points[2]) == 0: if (points[1].x - points[0].x) * (points[2].x - points[0].x) + ( points[1].y - points[0].y) * (points[2].y - points[0].y) < 0: ch.clear() ch.append(points[1]) ch.append(points[2]) ch_copy = ch.copy() result.append(ch_copy) continue elif (points[0].x - points[1].x) * (points[2].x - points[1].x) + ( points[0].y - points[1].y) * (points[2].y - points[1].y) < 0: ch.clear() ch.append(points[0]) ch.append(points[2]) ch_copy = ch.copy() result.append(ch_copy) continue else: ch.clear() ch.append(points[0]) ch.append(points[1]) ch_copy = ch.copy() result.append(ch_copy) continue if point_relative(points[0], points[1], points[2]) == 1: ch.append(points[2]) ch_copy = ch.copy() result.append(ch_copy) continue else: ch.pop() ch.append(points[2]) ch.append(points[1]) ch_copy = ch.copy() result.append(ch_copy) continue visible = [] for j in range(len(ch)): tmp = ch.copy() tmp.append(tmp[0]) if point_relative(tmp[j], tmp[j + 1], points[i]) == -1: # if tmp[j] not in visible: visible.append(tmp[j]) # if tmp[j+1] not in visible: visible.append(tmp[j + 1]) index = j + 1 if len(visible) == 0: ch_copy = ch.copy() result.append(ch_copy) else: k = 0 ch.insert(index, points[i]) while k < len(visible) - 1: if visible.count(visible[k]) == 2: ch.remove(visible[k]) visible.remove(visible[k]) visible.remove(visible[k]) k += 1 ch_copy = ch.copy() result.append(ch_copy) return result