def analyzeFig(figs, reqEdge, hole=False): global TOT_PTS global TOT_SEGS edge = reqEdge started = False pts = [] # get all the current figure's vertices while edge.name != reqEdge.name or not started: started = True v = edge.origin # print(v.name) pts.append([v.pos.x, v.pos.y]) # as list instead of Point for plot TOT_PTS.append(v.pos) edge = edge.next # get all the current figure's segments for i in range(len(pts) - 1): start = pts[i % len(pts)] end = pts[(i + 1) % len(pts)] s = Segmento(Point(start[0], start[1]), Point(end[0], end[1])) TOT_SEGS.append(s) figs.append(pts) return figs
def main(): input = [Point(4, 1), Point(-3, 15), Point(14, 0)] input.sort() print(input) for i in range(STEPS): paint(input) animation = camera.animate() animation.save('animation.mp4')
def checkCircleEvent(l, c, r, h): cc, cr = Point.getCircumCenterRadius(l.value[0], c.value[0], r.value[0]) if cc.y <= h: # new circle event low_point = Point(cc.x, cc.y - cr) e = Event(low_point, cc, cr, c) c.pointer = e return e else: return None
def isLessThan(s1, s2, t1): t2 = Point(t1.x + 1, t1.y) tline = Line.points2Line(t1, t2) hit1 = tline.intersects(Line.points2Line(s1.start, s1.end)) hit2 = tline.intersects(Line.points2Line(s2.start, s2.end)) # if insert horizontal s1, insert at the end if not hit1: return False if hit1.x < hit2.x: return True else: return False
def activatePlace(p, h): global t global q if not t.root: t.root = Node([p.value]) return else: a = t.find(p, h) if a.pointer: circle_event = a.pointer q.delete(circle_event) nodes = t.insert(p, h) print(nodes) n1 = nodes[0] n2 = nodes[1] n3 = nodes[2] if n1: left = t.getLeft(n1) if left: cc, cr = Point.getCircumCenterRadius(left.value[0], n1.value[0], n2.value[0]) # new circle event in q points to n1 in t new_event1 = Event(Point(cc.x, cc.y - cr), cc, cr, n1) # n1 in t points to event in q n1.pointer = new_event1 q.push(new_event1) if n3: right = t.getRight(n3) if right: cc, cr, = Point.getCircumCenterRadius(n2.value[0], n3.value[0], right.value[0]) # new circle event in q points to n3 in t new_event2 = Event(Point(cc.x, cc.y - cr), cc, cr, n3) # n3 in t points to event in q n3.pointer = new_event2 q.push(new_event2) return
def __init__(self, p=Point(), seg=0, pos=0): self.point = p self.seg = seg self.pos = pos
vertFile = open(INPUT_VERTEX, 'r') vlines = vertFile.readlines() edgeFile = open(INPUT_EDGE, 'r') elines = edgeFile.readlines() faceFile = open(INPUT_FACE, 'r') flines = faceFile.readlines() # lines indexes 0 1 2 3 contain just headers # vertex reading for line in vlines[4:]: data = re.sub(' +', ' ', line).split() x = float(data[1]) if '.' in data[1] else int(data[1]) y = float(data[2]) if '.' in data[2] else int(data[2]) v = Vertex(data[0], Point(x, y)) vMap[v.name] = v # edges reading for line in elines[4:]: data = re.sub(' +', ' ', line).split() e = Edge(data[0]) eMap[e.name] = e # faces reading for line in flines[4:]: data = re.sub(' +', ' ', line).split() f = Face(data[0]) fMap[f.name] = f # after none init in map, go back and fill # fill vertices for line in vlines[4:]:
from glibrary import Point from etree import * from ttree import * etree = Q() e1 = Event(Point(10, 5), 0, 0) etree.insert(e1) e2 = Event(Point(10, 6), 0, 1) etree.insert(e2) etree.insert(Event(Point(9, 3), 1, 0)) etree.insert(Event(Point(14, 1), 1, 1)) in_array = Q.inorder(etree.root) print(in_array) print(etree.root.value) print(etree.root.left_child.value) print(etree.root.right_child.value) print(etree.root.right_child.right_child.value) #etree.deleteValue(Point(14, 1)) #etree.deleteValue(Point(10, 5)) ''' etree.deleteNode(etree.root) in_array = Q.inorder(etree.root) print(in_array, etree.root.value) ''' #node = etree.find(Point(10,6)) #print(node.value) node = etree.find(Point(10, 5)) next = etree.getNextInorder(node) print("next from point:", next.value) next = etree.getNext() print("next:", next)
return [] square = math.sqrt(inner_calc) double_a = 2 * a answers = [(-b - square) / double_a, (-b + square) / double_a] return answers gap = 40 fig = plt.figure() fig.add_subplot() ax1 = plt.gca() yValue = 1 input1 = [Point(5, 5), Point(7, 18)] input2 = [Point(7, 18), Point(5, 5)] xs = [p.x for p in input1] ys = [p.y for p in input1] ax1.scatter(xs, ys, s=20, zorder=10, color='blue') xMin, xMax = min(input1, key=lambda p: p.x).x, max(input1, key=lambda p: p.x).x yMin, yMax = min(input1, key=lambda p: p.y).y, max(input1, key=lambda p: p.y).y plt.setp(ax1, xlim=(xMin - gap, xMax + gap), ylim=(yMin - gap, yMax + gap)) # plot sweep line xlim = ax1.get_xlim() ylim = ax1.get_ylim() ax1.plot(list(xlim), [yValue, yValue], color="black") xp = list(np.linspace(xlim[0], xlim[1], 100))
def __init__(self, vname="", pos=Point()): self.name = vname self.pos = pos self.incident = None # edge
from glibrary import eps, Point, Line, Vector from math import sin, cos, pi # TESTS d = 1.4142 theta = 45 p1 = Point(1, 1) p2 = Point(p1.x + d * cos(theta * pi / 180), p1.y + d * sin(theta * pi / 180)) # (1.9999, 1.9999) p3 = Point(2, 2) # POINTS print(p2 == p3) print(p2) print("Point distance:", Point.distance(p1, p3)) # 1.4142 print("Point rotation:", p1.rotate(90)) # LINE FROM 2 POINTS p1 = Point(2, 2) p2 = Point(2, 4) line1 = Line.points2Line(p1, p2) print("Line from 2 points:", line1) # CHECK PARALLEL LINES p3 = Point(4, 1) p4 = Point(4, 3) line2 = Line.points2Line(p3, p4) print("Is Paralel:", line1.isParallelTo(line2)) # CHECK INTERSECTION line1 = Line.points2Line(Point(0, 0), Point(1, 1))
def __init__(self, value=Segment(), hit=Point()): self.value = value # now a segment self.hit = hit # hit with T self.left_child = None self.right_child = None self.parent = None
for i in range(odd, len(mountains), 2): curr_peak = mountains[i][1] max_peak = tuple([0, 0]) sun = True for j in range(i, len(mountains), 2): if (mountains[j][1] > curr_peak): sun = False break if sun == True: peaks_x.append(mountains[i][0]) peaks_y.append(mountains[i][1]) for k in range(i + 2, len(mountains), 2): if (mountains[k][1] > max_peak[1]): max_peak = mountains[k] other_point = Point(max_peak[0] + 1, max_peak[1]) block_point = Point(max_peak[0], max_peak[1]) sun_ray = Line.points2Line(block_point, other_point) peak_point = Point(mountains[i][0], mountains[i][1]) low_point = Point(mountains[i + 1][0], mountains[i + 1][1]) peak_slope = Line.points2Line(peak_point, low_point) hit_point = sun_ray.intersects(peak_slope) sunny_segs.append([ tuple([hit_point.x, hit_point.y]), tuple([peak_point.x, peak_point.y]) ]) sun_lows_x.append(hit_point.x) sun_lows_y.append(hit_point.y)
# Thus, return the empty list. if inner_calc < 0: return [] square = math.sqrt(inner_calc) double_a = 2 * a answers = [(-b - square) / double_a, (-b + square) / double_a] return answers fig = plt.figure() fig.add_subplot() ax1 = plt.gca() yValue = -0.2 input1 = [Point(-3, 15), Point(10, 10), Point(4,1), Point(-3,15), Point(14,0), Point(-3,15)] xs = [p.x for p in input1] ys = [p.y for p in input1] ax1.scatter(xs, ys, s=20, zorder=10, color='blue') xMin, xMax = min(input1, key=lambda p: p.x).x, max(input1, key=lambda p: p.x).x yMin, yMax = min(input1, key=lambda p: p.y).y, max(input1, key=lambda p: p.y).y plt.setp(ax1, xlim=(xMin - gap, xMax + gap), ylim=(yMin - gap, yMax + gap)) # plot sweep line xlim = ax1.get_xlim() ylim = ax1.get_ylim() ax1.plot(list(xlim), [yValue, yValue], color="black") xp = list(np.linspace(xlim[0], xlim[1], 100)) yps = [] for p in input1:
from glibrary import Point import math from matplotlib import pyplot as plt import numpy as np gap = 3 txt_offset = 0.1 fig = plt.figure() fig.add_subplot() ax1 = plt.gca() input = [Point(-3, 15), Point(10, 10), Point(4, 1)] xs = [p.x for p in input] ys = [p.y for p in input] cc, cr = Point.getCircumCenterRadius(input[0], input[1], input[2]) circle = plt.Circle((cc.x, cc.y), cr, color='black', fill=False) low_point = Point(cc.x, cc.y - cr) ax1.scatter(xs, ys, s=20, zorder=10, color='blue') ax1.scatter([cc.x], [cc.y], s=20, zorder=10, color='blue', marker='x') ax1.scatter([low_point.x], [low_point.y], s=20, zorder=10, color='red') ax1.add_patch(circle) xMin, xMax = min(input, key=lambda p: p.x).x, max(input, key=lambda p: p.x).x yMin, yMax = min(input, key=lambda p: p.y).y, max(input, key=lambda p: p.y).y plt.setp(ax1, xlim=(xMin - gap, xMax + gap), ylim=(yMin - gap, yMax + gap)) for i in range(len(input)):
def processEvent(p): global tot_seg global tLine global R global R_segs p = p.value.point if animated and not single_plot: paint(p) paint(p) U = [s for s in tot_seg if s.start == p] # tree finding if fast: U2, C, L = tLine.findByPoint(p) # brute force finding if not fast: in_array = tLine.inorder(tLine.root) #print("T line:", in_array) L = [s for s in in_array if s.end == p] C = [] for s in in_array: if s.isInSegment(p) == 2: C.append(s) U = U #print("U:{u}\nC:{c}\nL:{l}".format(u=U, c=C, l=L)) # lists to sets U = set(U) C = set(C) L = set(L) UCL = (U.union(C)).union(L) UC = U.union(C) #print("UCL: {0}".format(UCL)) if len(UCL) > 1: R.append(p) R_segs.append([s.index for s in UCL]) # print output on the go if live_output: ucl = list(UCL) segs_involved = "" for j in range(len(ucl)): segs_involved += str(ucl[j].name) + " " print(("Intersection at: ({x},{y}) -> " + segs_involved).format( x=p.x, y=p.y)) for s in (L.union(C)): tLine.deleteValue(s, p) for s in (UC): tLine.insert(s, p) if len(UC) == 0: s_l = tLine.getLeftFromP(p, tLine.root) s_r = tLine.getRightFromP(p, tLine.root) #print("########### new step neighbours:", s_l.value, s_r.value) if s_l != None and s_r != None: findEvent(s_l.value, s_r.value, p) else: uc = list(UC) hits = [] # UC in T: list of hits with segment index for s in uc: t2 = Point(p.x + 1, p.y) tempLine = Line.points2Line(p, t2) hit = tempLine.intersects(Line.points2Line(s.start, s.end)) if not hit: continue hits.append([hit, s.index]) # UC in T (ordered horizontally) hits = sorted(hits, key=lambda p: p[0].x, reverse=False) if len(hits) < 1: return else: s_prime = tot_seg[hits[0][1]] # left neighbour is the inorder predecessor s_left = tLine.getPredecessor(tLine.root, s_prime, p) if s_left != None: #print("left neighbour:", s_left.value) findEvent(s_left.value, s_prime, p) s_bprime = tot_seg[hits[len(hits) - 1][1]] # right neighbour is the inorder successor s_right = tLine.getSuccessor(tLine.root, s_bprime, p) if s_right != None: #print("right neighbour:", s_right.value) findEvent(s_bprime, s_right.value, p)
for line in flines[offset:]: pts = line.split(' ') segmentName = iteration # if file contains segment name (store it) or not (segment id is num of iteration) at the end of line if "s" in pts[len(pts) - 1]: segmentName = pts[len(pts) - 1].rstrip("\n") else: segmentName = "s" + str(iteration) seg = [] for i in range(0, len(pts) - 1, 2): x = float(pts[i]) if '.' in pts[i] else int(pts[i]) y = float(pts[i + 1]) if '.' in pts[i + 1] else int(pts[i + 1]) pt = Point(x, y) tot_pts.append(pt) seg.append(pt) for i in range(len(seg)): if i == 0: yVal = seg[i].y if seg[i].y != yVal: isHorizontal = False if not isHorizontal: seg_sorted = sorted(seg, key=lambda p: p.y, reverse=True) else: seg_sorted = sorted(seg, key=lambda p: p.x, reverse=False) for i in range(len(seg_sorted)): e = Event(seg_sorted[i], iteration, i) ev.append(e) s = Segment(seg_sorted[0], seg_sorted[1], iteration, segmentName) tot_seg.append(s)
from ptree import BST from glibrary import Point, Vector, Line ptree = BST() ptree.insert(Point(10, 5)) ptree.insert(Point(10, 6)) ptree.insert(Point(9, 3)) in_array = BST.inorder(ptree.root) print(in_array) # balance p tree bTreeRoot = BST.getBalancedBST(in_array, 0, len(in_array) - 1) pre_array = BST.preorder(bTreeRoot) print(pre_array) myBalancedBST = BST() myBalancedBST.root = bTreeRoot # now is saved as proper tree print(myBalancedBST.root.value) # 10,5 print(myBalancedBST.root.left_child.value) # 10,6 print(myBalancedBST.root.right_child.value) # 9,3
def main(): global t global q global ax1 #input = [Point(14, 0), Point(10, 10), Point(-3, 15), Point(4, 1), Point(-5, 6), Point(7, 18)] #input = [Point(10, 10), Point(-3, 15), Point(4, 1)] input = [Point(14, 0), Point(10, 10), Point(-3, 15), Point(4, 1)] #input = [Point(14, 0), Point(10, 10), Point(-3, 15), Point(4, 1), Point(20, 7)] #input = [Point(14, 0), Point(10, 10), Point(-3, 15), Point(4, 1), Point(-5, 6)] xs = [p.x for p in input] ys = [p.y for p in input] # get axis min and max xMin, xMax = min(input, key=lambda p: p.x).x, max(input, key=lambda p: p.x).x yMin, yMax = min(input, key=lambda p: p.y).y, max(input, key=lambda p: p.y).y # for plot ax1.scatter(xs, ys, s=30, zorder=10, color='tab:blue') plt.setp(ax1, xlim=(xMin - gap, xMax + gap), ylim=(yMin - gap, yMax + gap)) for p in input: e = Event(p) q.push(e) xlim = ax1.get_xlim() ylim = ax1.get_ylim() height = abs((ylim[1] + gap) - (ylim[0] - gap)) dh = (height * 1.0) / STEPS h = ylim[1] + gap #next = q.show() hits = [] while h > (ylim[0] - gap): # print("-> h:", h) if not q.isEmpty(): next = q.show() if abs(h - next.value.y) < dh and not q.isEmpty(): p = q.pop() # print("Pop Event:", p, "height:", h, "dh:", dh, "next:", next) if not p.center: activatePlace(p, h) else: activateCircle(p, h) tree = T.inorder(t.root) # print(tree) hits = plotEdges(tree, h, hits) h -= dh print(q.isEmpty()) print(len(q.data)) #p = q.pop() voronoi_x = [p.x for p in voronoi] voronoi_y = [p.y for p in voronoi] print(voronoi) ax1.scatter(voronoi_x, voronoi_y, s=30, zorder=10, color='red') for hit in hits: ax1.scatter([hit[0]], [hit[1]], s=5, zorder=5, color='black') plt.show()
import matplotlib.pyplot as plt from matplotlib import collections as mc import numpy as np from glibrary import eps, Point, Line, Vector file1 = open('3.in', 'r') flines = file1.readlines() hide_point = Point(float(flines[0]), float(flines[1])) n_stations = int(flines[2]) x = [] y = [] init_index = 3 for i in range(n_stations*2): if (init_index + i) % 2 == 1: x.append(float(flines[init_index + i])) else: y.append(float(flines[init_index + i])) min_dist = 0 perpPoint = Point() seg = [] fig = plt.figure() fig.add_axes() ax1 = plt.gca() ax1.plot(x,y, color="green") ax1.scatter(hide_point.x,hide_point.y, s=100, marker="P", color='blue')
flines = faceFile.readlines() vMap = {} eMap = {} fMap = {} verts = [] edges = [] faces = [] # indexes 0 1 2 3 contain just headers # vertex reading for line in vlines[4:]: data = re.sub(' +', ' ', line).split() x = float(data[1]) if '.' in data[1] else int(data[1]) y = float(data[2]) if '.' in data[2] else int(data[2]) v = Vertex(data[0], Point(x, y)) vMap[v.name] = v # edges reading for line in elines[4:]: data = re.sub(' +', ' ', line).split() e = Edge(data[0]) eMap[e.name] = e # faces reading for line in flines[4:]: data = re.sub(' +', ' ', line).split() f = Face(data[0]) fMap[f.name] = f # after none init in map, go back and fill # fill vertices for line in vlines[4:]:
def __init__(self, p1=Point(), p2=Point(), index=0, name=""): self.start = p1 self.end = p2 self.index = index self.name = name
def __init__(self, value=Point()): self.value = value # now a point self.left_child = None self.right_child = None self.parent = None
def getSegmentHit(s, p): t2 = Point(p.x + 1, p.y) tline = Line.points2Line(p, t2) hit = tline.intersects(Line.points2Line(s.start, s.end)) return hit
from glibrary import Point, Line, Vector, eps import matplotlib.pyplot as plt from matplotlib import collections as mc cont = 0 n = int(input()) pts = [] tuples = [] x = [] y = [] for i in range(n): row = str(input()).split() newPoint = Point(int(row[0]), int(row[1])) pts.append(newPoint) tuples.append(tuple([newPoint.x, newPoint.y])) x.append(newPoint.x) y.append(newPoint.y) sorted_lex = sorted(tuples, key=lambda k: (k[0], k[1])) print("sorted ", sorted_lex) L = [] U = [] ch_segs = [] x2 = [] y2 = [] x2 = [tup[0] for tup in sorted_lex] y2 = [tup[1] for tup in sorted_lex] def paint(x2, y2, x, y, cont):