def test_hashs(): """ fonction permettant de tester le hachage des points dans les differents carres. elle affiche la syntaxe svg correspondant au plan quadrille et les differents points de couleurs differentes selon le carre. le cote du carre est une valeur fixee dans le programme. pre-conditions: - points est un vecteur d'elements de type Point """ distance = 40 nbr = 400 // distance points = point_aleatoire(200, [0, 400]) print("test h1") quadrillage_vertical = [ Segment([Point([i * distance, 0]), Point([i * distance, 400])]) for i in range(nbr + 1) ] quadrillage_horizontal = [ Segment([Point([0, j * distance]), Point([400, j * distance])]) for j in range(nbr + 1) ] quadrillage = quadrillage_horizontal + quadrillage_vertical tycat(points, quadrillage) tables = divide_into_squares(distance, points) tycat(list(tables[0][carre]) for carre in tables[0]) print("test h2") print("test h3") print("test h4")
def print_components_sizes(distance, points): """ affichage des tailles triees de chaque composante """ segments = [] research_base = [point for point in points] d = distance / sqrt(2) n = int(1 / d) segments_horizentaux = [ Segment([Point([i * d, 0]), Point([i * d, 1])]) for i in range(n + 1) ] segments_verticaux = [ Segment([Point([0, i * d]), Point([1, i * d])]) for i in range(n + 1) ] origine = Point([0.0, 0.0]) total = research_base.copy() s = 0 while len(research_base) > 0: current = research_base[0] research_base.pop(0) for point in research_base: if current.distance_to(point) < distance: s += 1 segments.append(Segment([current, point])) tycat(origine, total, segments + segments_horizentaux + segments_verticaux)
def remove_segment_and_save_it(self, top1, top2, vertices, cycle): """ Remove a segment in vertices of a graph and add it in the cycle of segments """ cycle.append(Segment([top1, top2])) vertices[top1].remove(Segment([top1, top2])) vertices[top2].remove(Segment([top1, top2]))
def intersection_test(): """ test the intersection on basic cases with one or two neighbours """ print("\n---------Intersection neighbour test---------") seg1 = Segment([Point([0, 0]), Point([2, 2])]) seg2 = Segment([Point([1, 0]), Point([1, 2])]) events = Events([seg1, seg2]) print("events:", events) Segment.current_point = Point([2.0, 2.0]) living_segments = SortedList() living_segments.add(seg1) living_segments.add(seg2) while not events.isempty(): current_event = events.event_list.pop(0) #print("current event: ", current_event.key) if current_event.key in events.begin_points: for segment in events.begin_points[current_event.key]: print("segment :", segment) print([point for point in intersect_with(segment, living_segments)]) print("-----------------------------------------\n")
def get_first_precision(self): """ Returns the length of the longer possible segment, to start the paving """ quadrant = self.bounding_quadrant() point_min = Point(quadrant.min_coordinates) point_max = Point(quadrant.max_coordinates) segmax = Segment([point_min, point_max]) maxlength = segmax.length() return 2 * maxlength
def events_init_test(): """ test the init of a segment in the series of event """ print("\n------------Segment init test------------") events = Events([Segment([Point([1.0, 2.0]), Point([3.0, 4.0])]), Segment([Point([-3.0, -4.0]), Point([3.0, -4.0])]), Segment([Point([-3.0, -4.0]), Point([2.0, 4.0])])]) print(events) print("-----------------------------------------\n")
def main(): NOMBRE = 100 segments = [] # contient des listes de deux points for new_pointa, new_pointb in zip(random_points_generator(NOMBRE), random_points_generator(NOMBRE)): new_segment = [new_pointa, new_pointb] for indice in range(len(segments)): pointc, pointd = segments[i] if Segment(new_segment).intersect(Segment([pointc, pointd])): # si intersection, on swap les points new_segment = [new_pointa, pointc] # segments[indice] devient un nouveau segment # on doit vérifier qu'il ne s'intersecte pas avec d'autres segments[indice] = [new_pointb, pointd] segments.append(new_segment) # on ajoute le segment dans tous les cas
def print_quadrant(root): """marche uniquement en 2D""" xmin, ymin = root.min_coordinates xmax, ymax = root.max_coordinates p1 = Point([xmin, ymin]) p2 = Point([xmin, ymax]) p3 = Point([xmax, ymax]) p4 = Point([xmax, ymin]) s1 = Segment([p1, p2]) s2 = Segment([p2, p3]) s3 = Segment([p3, p4]) s4 = Segment([p4, p1]) print_segment([s1, s2, s3, s4]) if root.childs: for child in root.childs: print_quadrant(child)
def quadratic_segments_iterator(self): """ retourne un itérateur sur les segments, du plus petit au plus grand """ liste_points = list(self.vertices.keys()) liste_segments = [] n = len(liste_points) for i in range(n): for j in range(i+1, n): liste_segments.append(Segment([liste_points[i], liste_points[j]])) for segment in sorted(liste_segments, key=lambda segment: segment.length()): yield segment
def quadratic_iterator(self, tops): """ Iterator on a quadratic number of segments """ segments = [] length = len(tops) for i in range(length): for j in range(i + 1, length): segments.append(Segment([tops[i], tops[j]])) for segment in sorted(segments, key=lambda segment: segment.length()): yield segment
def parcours_aleatoire(self, point_depart, liste, pos): """ à partir de point_depart, insère dans liste, à partir de l'indice pos, des sommets formant un cycle """ ajouts = 1 point = point_depart while self.vertices[point] != []: point_pre = point seg = self.vertices[point].pop() # supprime le segment de choix et le retourne point = seg.endpoint_not(point_pre) self.vertices[point].remove(Segment([point_pre, point])) # supprime le segment "doublon" liste.insert(pos + ajouts, point) ajouts += 1
def ordered_segments(points, t0=10): t = t0 tables = [hasher(points, t)] while collision(tables[-1]): t /= 2 tables.append(hasher(points, t)) for jeu in tables.reverse(): for table in jeu: for key in table.keys(): for couple in combinations(table[key], 2): yield Segment(list(couple))
def main(): point1 = Point([1, 1]) point2 = Point([1, 5]) s = Segment([point1, point2]) # print(s) # print(s.endpoints) # c = s.copy() # print(c) # print(c.endpoints) # q = s.bounding_quadrant() # print(q) # print(q.min_coordinates, q.max_coordinates) # print(s.svg_content()) # point = Point([1,2]) # print(s.endpoints[0]) # print(s.endpoints[1]) # print(s.endpoint_not(point1)) # print(s.endpoint_not(point2)) # print(s.contains(Point([2, 4.5]))) a = s.__repr__() print(a)
def print_components_sizes(distance, points): """ affichage des tailles triees de chaque composante """ SortedX = sorted([point for point in points], key = abscisse) result = prochesX(SortedX, distance) dernier_pointX_1 = result[len(result)-1] dernier_indice = SortedX.index(dernier_pointX_1) origine = Point([0.0, 0.0]) segment_1 = Segment([Point([dernier_pointX_1.x, 0]), Point([dernier_pointX_1.x, 1])]) SortedY = sorted([point for point in result], key = ordonnee) result_bis = prochesY(SortedY, distance) dernier_pointXbis_1 = result_bis[len(result_bis)-1] dernier_indice_bis = SortedX.index(dernier_pointXbis_1) segment_2 = Segment([Point([0, dernier_pointXbis_1.y]), Point([1, dernier_pointXbis_1.y])]) tycat(origine, points, (segment_1, segment_2)) """ affichage des tailles triees de chaque composante """ segments = [] research_base = [point for point in points] origine = Point([0.0, 0.0]) total = research_base.copy() s = 0 enveloppe = [] while len(research_base) > 0: current = research_base[0] research_base.pop(0) for point in research_base: if current.distance_to(point) < distance: s += 1 segments.append(Segment([current, point])) enveloppe.append(s) tycat(origine, total, segments)
def living_key(segment, current_point, adjuster): """ key """ point = Point([current_point.coordinates[0] + 10, current_point.coordinates[1]]) other_point = Point([current_point.coordinates[0] - 10, current_point.coordinates[1]]) swipe_line = Segment([point, other_point]) intersection = segment.line_intersection_with(swipe_line) intersection = adjuster.hash_point(intersection) if intersection.coordinates[0] < current_point.coordinates[0]: return(intersection.coordinates[0], angle(segment, adjuster)) else: return(intersection.coordinates[0], angle(segment, adjuster))
def main(): """ tycat example """ points = [[Point([random(), random()]) for _ in range(5)] for _ in range(2)] segments = [[Segment(endpoints) for endpoints in combinations(p, r=2)] for p in points] print("tycat(points, segments)") tycat(points, segments) print("tycat(zip(iter(points), iter(segments)))") tycat(zip(iter(points), iter(segments))) print("tycat(*zip(iter(points), iter(segments)))") tycat(*zip(iter(points), iter(segments))) intersections = filter(None, (c[0].intersection_with(c[1]) for c in product(*segments))) print("intersections entre rouge et vert") tycat(segments[0], segments[1], intersections)
def get_segments(polygones): max_length = 9999.0 couples_indice_segment = [] couples_indice_ligne = [] quadrants = [polygon.bounding_quadrant() for polygon in polygones] areas = [polygon.absolute_area for polygon in polygones] for indice, polygone in sorted(enumerate(polygones), key=lambda couple: couple[1].absolute_area): point = polygone.points[0].coordinates couples_indice_ligne.append(( indice, Segment( [Point([point[0], point[1]]), Point([max_length, point[1]])]), )) for segment in polygone.segments(): couples_indice_segment.append((indice, segment)) return couples_indice_segment, couples_indice_ligne, quadrants, areas
def main(): """ petit exemple sur l'utilisation de tycat """ print("lancez moi dans terminology") print("tycat permet d'afficher des points et des segments") print("chaque argument doit etre un iterable sur des points \ et/ou segments (ou juste un point/segment)") print("chaque argument est affiche d'une couleur differente") # un point origine = Point([0.0, 0.0]) # un vecteur de points cercle = [Point([cos(c * pi / 10), sin(c * pi / 10)]) for c in range(20)] # un iterateur sur des segments (crees a la volee) segments = (Segment([p1, p2]) for p1, p2 in zip(cercle, islice(cycle(cercle), 1, None))) tycat(origine, cercle, segments)
def ordered_segments(points, precision): """ Returns iterator on the hashed segments """ tables = [] tables_hash, collision = hashed_segments(points, precision) tables.append(tables_hash) while collision: precision = precision/2 tables_hash, collision = hashed_segments(points, precision) tables.append(tables_hash) for table in reversed(tables): for table_hash in table: for carre in table_hash.keys(): length = len(table_hash[carre]) for i in range(length): for j in range(i+1, length): yield Segment([table_hash[carre][i], table_hash[carre][j]])
def old_main(): """main function""" nombre_de_points = 100 coordonnees_des_points = 10 limit = random.randrange(2, nombre_de_points) polygones = [Polygon([Point([1, 2]), Point([2, 4]), Point([-1, 2])])] for _ in range(3): points = [ Point([ random.randrange(coordonnees_des_points), random.randrange(coordonnees_des_points), ]) ] segments = [] for _ in range(limit): # on ajoute le polygone si aucun de ses segments ne s'intersecte avec un segment déjà existant point = Point([ random.randrange(coordonnees_des_points), random.randrange(coordonnees_des_points), ]) print(points, segments) failed = is_failed(polygones, segments, points, point) if not failed: segment = Segment([points[-1], point]) if point not in points: points.append(point) if segment not in segments: segments.append(segment) if len(points) > 2: polygon = Polygon(points) if polygon.area() != 0: polygones.append(polygon) with open("generated.poly", "w") as file: for indice, polygone in enumerate(polygones): for point in polygone.points: file.write( f"{indice} {point.coordinates[0]} {point.coordinates[1]}\n" )
def print_components_sizes(distance, points): """ affichage des tailles triees de chaque composante """ segments = [] research_base = [point for point in points] origine = Point([0.0, 0.0]) total = research_base.copy() s = 0 enveloppe = [] while len(research_base) > 0: current = research_base[0] research_base.pop(0) for point in research_base: if current.distance_to(point) < distance: s += 1 segments.append(Segment([current, point])) enveloppe.append(s) print(enveloppe) tycat(origine, total, segments)
def hashed_segments_iterator(self): """ retourne un itérateur sur les segments en utilisant le hash """ tables = [] t = 3 * max(self.bounding_quadrant().max_coordinates[0] - \ self.bounding_quadrant().min_coordinates[0], \ self.bounding_quadrant().max_coordinates[1] - \ self.bounding_quadrant().min_coordinates[1]) # precision de depart table_hachage, collision = self.hasher(t) tables.append(table_hachage) while collision: t /= 2 table_hachage, collision = self.hasher(t) tables.append(table_hachage) tables.reverse() tables = tables[1:] for table_hachage in tables: for dico in table_hachage: for point_carre in dico.keys(): n = len(dico[point_carre]) for i in range(n): for j in range(i+1, n): yield Segment([dico[point_carre][i], dico[point_carre][j]])
def listpoints_to_listsegments(listpoints): """retourne une liste de segments à partir d'une liste de points""" listsegments = [] for i in range(len(listpoints)-1): listsegments.append(Segment([listpoints[i], listpoints[i+1]])) return listsegments
def is_failed(polygones, segments, points, point): for polygon in polygones: for segment in list(polygon.segments()) + segments: if segment.intersect(Segment([points[-1], point])): return True
def print_components_sizes(distance, points): origine = Point([0.0, 0.0]) SortedX = sorted([point for point in points], key = abscisse) print("nombre de point est :", len(SortedX)) # print(SortedX) result = prochesX(SortedX, distance) print("nombre de points dans result est :", len(result)) n = len(result) dernier_pointX_1 = result[-1] segment_1 = Segment([Point([dernier_pointX_1.x, 0]), Point([dernier_pointX_1.x, 1])]) result_droite = SortedX[n:] result_gauche = SortedX[:n] print(len(result_gauche), len(result_droite)) SortedY = sorted([point for point in result_gauche], key = ordonnee) result_bis = prochesY(SortedY, distance) n_bis = len(result_bis) print(n_bis) print(result_bis) dernierbis = result_bis[-1] # segment_2 = Segment([Point([0, dernierbis.y]), Point([dernier_pointX_1.x, dernierbis.y])]) result_bas = result_gauche[n_bis:] result_haut = result_gauche[:n_bis] print(len(result_haut), len(result_bas)) # dernier_pointXbis_1 = result_bis[-1] # dernier_indice_bis = result_gauche.index(dernier_pointXbis_1) # result_haut = result_gauche[dernier_indice_bis + 1:] result_bas = result_gauche[:dernier_indice_bis + 1] # segment_2 = Segment([Point([0, dernier_pointXbis_1.y]), Point([dernier_pointX_1.x, dernier_pointXbis_1.y])]) # # print("*********************************") # print(result_haut) # print("*********************************") # print(result_bas) # tycat(origine, points, segment_1) tycat(origine, points, (segment_1, segment_2)) """ affichage des tailles triees de chaque composante """ segments = [] research_base = [point for point in points] origine = Point([0.0, 0.0]) total = research_base.copy() s = 0 while len(research_base) > 0: current = research_base[0] research_base.pop(0) for point in research_base: if current.distance_to(point) < distance: s += 1 segments.append(Segment([current, point])) tycat(origine, total, segments)