def main(): x1, y1, x2, y2 = map(int, input().split()) line = Segment(Point(x1, y1), Point(x2, y2)) q = int(input()) for _ in range(q): x0, y0 = map(int, input().split()) p = line.project(Point(x0, y0)) print(p.x, p.y)
def main(): from geometric import Segment, Point x1, y1, x2, y2 = map(int, input().split()) line = Segment(Point(x1, y1), Point(x2, y2)) q = int(input()) for _ in range(q): x0, y0 = map(int, input().split()) p = line.reflect(Point(x0, y0)) print(p.x, p.y)
def main(): q = int(input()) for _ in range(q): x0, y0, x1, y1, x2, y2, x3, y3 = map(int, input().split()) s1 = Segment(Point(x0, y0), Point(x1, y1)) s2 = Segment(Point(x2, y2), Point(x3, y3)) if s1.isOrthogonal(s2): print(2) elif s1.isParallel(s2): print(1) else: print(0)
def line_to_segment(self, line): """ Place onto the polygon and compute the segment. :param line: :return: """ seg = Segment(line, -math.inf, math.inf) for segment, orient in zip(self.border_lines, self.orientation): if segment.crossed_by_closed(seg): up_edge, low_edge = seg.simple_split(segment) if orient: seg = up_edge else: seg = low_edge return seg
def zone(self, line, curr_node=None, is_seg=False): """ Returns all the cells this line crosses. """ curr_node = self.root if curr_node is None else curr_node if not is_seg: full_segment = Segment(line, -float("inf"), float("inf")) else: full_segment = line node_stack = deque([(curr_node, None, full_segment)]) while node_stack: curr_node, parent_node, curr_segment = node_stack.pop() if curr_node.is_terminal(): yield curr_node, parent_node, curr_segment elif curr_node.segment.same_line(curr_segment): continue elif curr_segment.crossed_by(curr_node.segment): upper_split, lower_split = curr_segment.simple_split( curr_node.segment) node_stack.append((curr_node.get_b(), curr_node, lower_split)) node_stack.append((curr_node.get_a(), curr_node, upper_split)) elif curr_segment.above_closed(curr_node.segment): node_stack.append((curr_node.get_a(), curr_node, curr_segment)) else: node_stack.append((curr_node.get_b(), curr_node, curr_segment))
def find_pretty_good_split_v(self): """ Checks all the pairs of vertices and finds the best pair of vertices with which to split this polygon with :return: """ min_val = float("inf") max_segment = None vertices = self.get_border_vertices() #print(vertices) vertices = [ p for p in vertices if not (math.isinf(p[0]) or math.isinf(p[1])) ] p1 = random.choice(vertices) vertices.remove(p1) for p2 in vertices: try: segment = Segment(to_line(p1, p2), min(p1[0], p2[0]), max(p1[0], p2[0])) tmp_val = self.score_split(segment, vertices) if min_val > tmp_val: max_segment = segment min_val = tmp_val except ZeroDivisionError: pass return max_segment
def count_line(self, line): """ Measures the number of points underneath a line. If you use the chan partitioning code then in theory this will take sqrt(n) time. :param line: :return: """ curr_node = self.root full_segment = Segment(line, -float("inf"), float("inf")) node_stack = deque([(curr_node, None, full_segment)]) count = 0 while node_stack: curr_node, parent_node, curr_segment = node_stack.pop() if curr_node.is_terminal(): count += sum( line.pt_eq_below(pt) for pt in curr_node.get_pts()) elif curr_node.segment.same_line(curr_segment): count += curr_node.get_b().pt_count() elif curr_segment.crossed_by(curr_node.segment): upper_split, lower_split = curr_segment.simple_split( curr_node.segment) node_stack.append((curr_node.get_b(), curr_node, lower_split)) node_stack.append((curr_node.get_a(), curr_node, upper_split)) elif curr_segment.above_closed(curr_node.segment): node_stack.append((curr_node.get_a(), curr_node, curr_segment)) count += curr_node.get_b().pt_count() else: node_stack.append((curr_node.get_b(), curr_node, curr_segment))
def __init__(self, points=list(), lines=list(), k=8): li = -float("inf") ri = float("inf") inter_lines = [] for l in lines: if l.is_segment(): inter_lines.append(l) else: inter_lines.append(Segment(l, li, ri)) self.root = Polygon2(points=points, lines=inter_lines) self.k = k
def __init__(self, weighted_lines, points=list(), min_weight=-1, k=8, seg_cutting=True): li = -float("inf") ri = float("inf") if seg_cutting: w_lines = [] for l, w in weighted_lines: if l.is_segment(): w_lines.append((l, w)) else: w_lines.append((Segment(l, li, ri), w)) self.root = Polygon(w_lines=w_lines, points=points, k=k) else: self.root = Polygon(w_lines=[(Segment(l, li, ri), w) for l, w in weighted_lines], points=points, k=k) self.min_weight = min_weight
from geometric import Segment, Point q = int(input()) for _ in range(q): x0, y0, x1, y1, x2, y2, x3, y3 = map(int, input().split()) s1 = Segment(Point(x0, y0), Point(x1, y1)) s2 = Segment(Point(x2, y2), Point(x3, y3)) print(s1.getDistance(s2))