def plot_task(cube, dir_p1, dir_p2, angle): dir = Vector3d(dir_p1, dir_p2, 0) line = [Point(dir_p1.x, dir_p1.y), Point(dir_p2.x * 800, dir_p2.y * 800)] while True: for i in pygame.event.get(): if i.type == pygame.QUIT: sys.exit() sc.fill(BLUE) # ортогональная проекция. new_basis - i столбец - координаты нового i базисного вектора new_basis = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] new_origin = [-100, 0, 0] # projection_cube = orth_proj(cube, new_basis, new_origin) # projection_line = orth_proj(line, new_basis, new_origin) center = Point(0, 0, 1000) projection_cube = center_proj(cube, center, new_basis, new_origin) projection_line = center_proj(line, center, new_basis, new_origin) draw_cube(sc, list(map(lambda point: [point.x, point.y], projection_cube))) draw_lines(sc, list(map(lambda point: [point.x, point.y], projection_line)), RED) for i in range(0, len(cube)): cube[i] = rotation(cube[i], dir, angle) pygame.display.update() clock.tick(FPS)
class SingleLinkageClusteringTest(unittest.TestCase): points = { 'P1': Point('P1', [1.5, 1.5]), 'P2': Point('P2', [2., 1.]), 'P3': Point('P3', [3., 3.]), 'P4': Point('P4', [4., 5.]), 'P5': Point('P5', [5., 3.]) } correct_clusterings = [{ 'C1': Cluster('C1', [points['P1']]), 'C2': Cluster('C2', [points['P2']]), 'C3': Cluster('C3', [points['P3']]), 'C4': Cluster('C4', [points['P4']]), 'C5': Cluster('C5', [points['P5']]) }, { 'C1': Cluster('C1', [points['P1'], points['P2']]), 'C3': Cluster('C3', [points['P3']]), 'C4': Cluster('C4', [points['P4']]), 'C5': Cluster('C5', [points['P5']]) }, { 'C1': Cluster('C1', [points['P1'], points['P2']]), 'C3': Cluster('C3', [points['P3'], points['P5']]), 'C4': Cluster('C4', [points['P4']]) }, { 'C1': Cluster('C1', [points['P1'], points['P2'], points['P3'], points['P5']]), 'C4': Cluster('C4', [points['P4']]) }, { 'C1': Cluster('C1', [ points['P1'], points['P2'], points['P3'], points['P5'], points['P4'] ]) }] def test_first_clustering_version_with_toy_case(self): slc = SingleLinkageClusteringV1(self.points.values()) clusterings = slc.clustering(k=1) self.assertEqual(clusterings, self.correct_clusterings) def test_second_clustering_version_with_toy_case(self): slc = SingleLinkageClusteringV2(self.points.values()) clusterings = slc.clustering(k=1) self.assertEqual(clusterings, self.correct_clusterings) def test_third_clustering_version_with_toy_case(self): slc = SingleLinkageClusteringV3(self.points.values()) clusterings = slc.clustering(k=1) self.assertEqual(clusterings, self.correct_clusterings) def test_fourth_clustering_version_with_toy_case(self): slc = SingleLinkageClusteringV4(self.points.values(), 2, 3, dimension_size=6) clusterings = slc.clustering(k=1) self.assertEqual(clusterings, self.correct_clusterings)
def alg_Cyrus_Beck(segment, points): reverse_if_left_orientation(points) A = segment[0] B = segment[1] # инициализируем пределы значений параметра, предполагая что отрезок полностью видимый # T = {t0 = 0, t1 = 1} tA = 0 tB = 1 for i in range(len(points)): # зациклинность массива # edge = PiPi+1 try: edge = points[i], points[i + 1] except: edge = points[i], points[0] # --- # находи нормаль normal = Point(edge[1].y - edge[0].y, edge[0].x - edge[1].x) t1 = (normal.x * (edge[1].x - A.x) + normal.y * (edge[1].y - A.y)) t2 = (normal.x * (B.x - A.x) + normal.y * (B.y - A.y)) if t2 != 0: t = t1 / t2 else: continue scalar_product = get_scalar_product(Point(B.x - A.x, B.y - A.y), normal) # Если тип точки потенциально вход. то: if scalar_product > 0: tA = max(tA, t) else: tB = min(tB, t) if tA > tB: return 0, 0 return tA, tB
def main(): # угол поворота angle = 0.05 cube = init_cube() # Единичный вектор angle_dir = 35 dir_p1 = Point(0, 0, 0) dir_p2 = Point(cos(radians(angle_dir)), sin(radians(angle_dir)), 0) dir = Vector3d(dir_p1, dir_p2, 0) line = [Point(dir_p1.x + 100, dir_p1.y + 100), Point(dir_p2.x * 800, dir_p2.y * 800)] while True: for i in pygame.event.get(): if i.type == pygame.QUIT: sys.exit() sc.fill(BLUE) # projection_cube = orthogonal_projection(cube) # projection_line = orthogonal_projection(line) center = Point(0, 0, 600) projection_cube = center_projection(cube, center) projection_line = center_projection(line, center) draw_cube(sc, list(map(lambda point: [point.x, point.y], projection_cube))) draw_lines(sc, list(map(lambda point: [point.x, point.y], projection_line)), RED) for i in range(0, len(cube)): cube[i] = rotation(cube[i], dir, angle) pygame.display.update() clock.tick(FPS)
def point_on_line(point, p1, p2): p_max = Point(max(p1.x, p2.x), max(p1.y, p2.y)) p_min = Point(min(p1.x, p2.x), min(p1.y, p2.y)) if define_orientation( p1, p2, point ) == "on" and p_min.x <= point.x <= p_max.x and p_min.y <= point.y <= p_max.y: return True else: return False
def __init__(self, owner: int, id: int, type: int, speedTurnsLeft: int, abilityCooldown: int, x: int, y: int, tour: int): self.owner = owner self.id = id self.type = type self.speedTurnsLeft = speedTurnsLeft self.abilityCooldown = abilityCooldown self.action = '' self.currentDestination = None # Type: int self.lastRoundSeen = tour Point.__init__(self, x, y)
def center_arc(pt1, pt2, bulge): if bulge > 0.: inc_angle = 4. * np.arctan(bulge) elif bulge < 0.: inc_angle = -4. * np.arctan(bulge) chord = Vector([pt2[i] - pt1[i] for i in range(3)]) mid = Point([0.5 * (pt1[i] + pt2[i]) for i in range(3) ]) vec = (chord.norm * 0.5 * bulge * cross(chord, Vector((0., 0., 1.))).unit()) summit = Point([mid[i] + vec[i] for i in range(3) ]) radius = chord.norm / (2. * np.sin(inc_angle/2.)) vec = radius * Vector([mid[i] - summit[i] for i in range(3)]).unit() center = Point([summit[i] + vec[i] for i in range(3) ]) return center
def get_intersection(p1, p2, p3, p4): # p = p3 + t(p4 - p3) n = Vector2d(-(p2.y - p1.y), p2.x - p1.x) t = (Vector2d.scalar_product(n, Vector2d( p3, p1))) / (Vector2d.scalar_product(n, Vector2d(p3, p4))) p = Vector2d(p3, p4) return Point(p3.x + t * p.x, p3.y + t * p.y)
def rotation(point, n, angle): s = sin(angle / 2) n_q = get_quaternion(cos(angle / 2), Point(n.x * s, n.y * s, n.z * s)) n_q_conj = get_conj_quaternion(n_q) p_q = get_quaternion(0, point) return quaternions_multiplication(quaternions_multiplication(n_q, p_q), n_q_conj)[1]
def center_projection(points, center): projection = [] for p in points: x = p.x * (center.z / (center.z - p.z)) + center.x * (p.z / (center.z - p.z)) y = p.y * (center.z / (center.z - p.z)) + center.y * (p.z / (center.z - p.z)) projection.append(Point(x, y)) return projection
def get_min_point(polygon): min_x = polygon[0].x min_y = polygon[0].y for i in range(0, len(polygon)): if polygon[i].x < min_x: min_x = polygon[i].x if polygon[i].y < min_y: min_y = polygon[i].y return Point(min_x, min_y)
def get_max_point(polygon): max_x = polygon[0].x max_y = polygon[0].y for i in range(0, len(polygon)): if polygon[i].x > max_x: max_x = polygon[i].x if polygon[i].y > max_y: max_y = polygon[i].y return Point(max_x, max_y)
def quaternions_multiplication(q1, q2): p0 = q1[0] * q2[0] - q1[1].x * q2[1].x - q1[1].y * q2[1].y - q1[1].z * q2[ 1].z p1 = q1[0] * q2[1].x + q1[1].x * q2[0] + q1[1].y * q2[1].z - q1[1].z * q2[ 1].y p2 = q1[0] * q2[1].y + q1[1].y * q2[0] + q1[1].z * q2[1].x - q1[1].x * q2[ 1].z p3 = q1[0] * q2[1].z + q1[1].z * q2[0] + q1[1].x * q2[1].y - q1[1].y * q2[ 1].x return get_quaternion(p0, Point(p1, p2, p3))
def __init__(self, cell): super().__init__(cell) self.body = [Point(0, 1), Point(1, 1), Point(1, 0), Point(2, 0)] self.bodies = [ self.body, [ Point(1, 2), Point(1, 1), Point(0, 1), Point(0, 0), ] ]
def orthogonal_projection(points): new_basis = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] new_origin = [-100, 0, 0] projection = [] for p in points: new_x = new_basis[0][0] * (p.x - new_origin[0]) + new_basis[1][0] * (p.y - new_origin[1]) + new_basis[2][0] * ( p.z - new_origin[2]) new_y = new_basis[0][1] * (p.x - new_origin[0]) + new_basis[1][1] * (p.y - new_origin[1]) + new_basis[2][1] * ( p.z - new_origin[2]) projection.append(Point(new_x, new_y)) return projection
def orth_proj(points, new_basis, new_origin): projection = [] for p in points: matrix = multiply_matrix(new_basis, [[p.x - new_origin[0]], [p.y - new_origin[1]], [p.z - new_origin[2]]]) new_x = matrix[0][0] new_y = matrix[1][0] new_z = matrix[2][0] projection.append(Point(new_x, new_y, new_z)) return projection
def center_proj(points, center, new_basis, new_origin): new_points = orth_proj(points, new_basis, new_origin) new_center = orth_proj([center], new_basis, new_origin)[0] projection = [] for p in new_points: x = p.x * (new_center.z / (new_center.z - p.z)) + new_center.x * ( p.z / (new_center.z - p.z)) y = p.y * (new_center.z / (new_center.z - p.z)) + new_center.y * ( p.z / (new_center.z - p.z)) projection.append(Point(x, y)) return projection
def parser(points): print("⚙️ Parsing process"+utils.OKGREEN, os.getpid(), utils.ENDC + "started") trips = [] # Add the point to an existing trip or creating a new one if it doesn't exist for point in points: new_point = Point(point[0], point[2], point[3], point[4]) exists = False for trip in trips: if (trip.id == point[1]): trip.points.append(new_point) exists = True if (exists == False): new_trip = Trip(point[1], [new_point]) trips.append(new_trip) return trips
def init_cube(): start_point = Point(300, 200, 100) side_length = 50 # Куб cube = [ start_point, Point(start_point.x + side_length, start_point.y, start_point.z), Point(start_point.x, start_point.y + side_length, start_point.z), Point(start_point.x + side_length, start_point.y + side_length, start_point.z), Point(start_point.x, start_point.y, start_point.z + side_length), Point(start_point.x + side_length, start_point.y, start_point.z + side_length), Point(start_point.x, start_point.y + side_length, start_point.z + side_length), Point(start_point.x + side_length, start_point.y + side_length, start_point.z + side_length) ] return cube
def calculateDistanceMap(macarte: numpy.array, murs) -> int: ## <DONTCOPY> ## from classes.Point import Point from classes.Game import Game from classes.Debug import Debug import time ## </DONTCOPY> ## #Pathfinding.distanceMap = {{None} * len(macarte[0])} * len(macarte) #Pathfinding.distanceMap = numpy.empty((len(macarte), len(macarte[0]), len(macarte), len(macarte[0])), dtype=object) Pathfinding.distanceMap = {} nbCalcMap = 0 totalCalcExpected = 0 casesOk = [] for x in range(len(macarte)): Pathfinding.distanceMap[x] = {} for y in range(len(macarte[0])): if macarte[x][y] not in murs: Pathfinding.distanceMap[x][y] = None casesOk.append((x, y)) totalCalcExpected += 1 else: Pathfinding.distanceMap[x][y] = None for x, y in casesOk: if not Game.check_timeout(): tmp = Pathfinding.buildDistanceMap(macarte, Point(x, y), [False], 15) assert tmp[x][y] == 0 Pathfinding.distanceMap[x][y] = tmp nbCalcMap += 1 else: Debug.msg( f"checkDistanceMap stopped at {x},{y} ({round(nbCalcMap*100/totalCalcExpected)} %) with {time.time() - Game.startTime}" ) return nbCalcMap return nbCalcMap
def convex_hull(points): if(len(points) < 2): print("insufficient points") return 0 pointsCopy = points.copy() pointsCopy.sort(key=sorter) hull = [] point_to_add = points[0] endpoint = Point(0, 0) while endpoint != pointsCopy[0]: hull.append(point_to_add) endpoint = pointsCopy[0] temp_line = Line_Segment(hull[-1], endpoint) for a_point in pointsCopy: if lr(temp_line, a_point) == 1 or point_to_add == endpoint: endpoint = a_point temp_line = Line_Segment(hull[-1], endpoint) point_to_add = endpoint return hull