def catmull_rom_spline_variants(): points = [ Point2(0,0), Point2(1,1), Point2(2,1), Point2(2,-1), ] controls = control_points(points) # By default, catmull_rom_points() will return a closed smooth shape curve_points_closed = catmull_rom_points(points, close_loop=True) # If `close_loop` is False, it will return only points between the start and # end control points, and make a best guess about tangents for the first and last segments curve_points_open = catmull_rom_points(points, close_loop=False) # By specifying start_tangent and end_tangent, you can change a shape # significantly. This is similar to what you might do with Illustrator's Pen Tool. # Try changing these vectors to see the effects this has on the rightmost curve in the example start_tangent = Vector2(-2, 0) end_tangent = Vector2(3, 0) tangent_pts = [points[0] + start_tangent, *points, points[-1] + end_tangent] tangent_controls = control_points(tangent_pts) curve_points_tangents = catmull_rom_points(points, close_loop=False, start_tangent=start_tangent, end_tangent=end_tangent) closed = polygon(curve_points_closed) + controls opened = polygon(curve_points_open) + controls tangents = polygon(curve_points_tangents) + tangent_controls a = closed + right(3)(opened) + right(10)(tangents) return a
def __init__(self): pyxel.init(128, 128) self.posicao = Vector2(0, 0) pyxel.mouse(True) pyxel.load('mapa.pyxres') self.tipo_tile = None self.jogador = Jogador(Vector2(10, 10), Vector2(0, 1)) pyxel.run(self.update, self.draw)
def update(self): if pyxel.btnp(pyxel.KEY_LEFT): self.jogador.posicao += Vector2(-2, 0) if pyxel.btnp(pyxel.KEY_RIGHT): self.jogador.posicao += Vector2(2, 0) if pyxel.btnp(pyxel.KEY_SPACE): self.jogador.velocidade_y = -10 self.jogador.update()
def update(self): self.velocidade_y = min(self.velocidade_y + self.aceleracao, self.velocidade_maxima) posicao_pretendida = self.posicao + Vector2(self.velocidade_x, self.velocidade_y) impedido = True while impedido: impedido = False tile_jogador_x, tile_jogador_y = self.obter_coordenada_tile( posicao_pretendida.x, posicao_pretendida.y) #tile direita x_tile, y_tile = self.obter_x_y(tile_jogador_x + 1, tile_jogador_y) if self.eh_solido(x_tile, y_tile) and self.colidiu( posicao_pretendida.x + self.offset_caixa_x, posicao_pretendida.y + self.offset_caixa_y, self.largura_sprite, self.altura_sprite, x_tile, y_tile, self.tamanho_tile, self.tamanho_tile): posicao_pretendida += Vector2(-1, 0) impedido = True continue #tile esquerda x_tile, y_tile = self.obter_x_y(tile_jogador_x - 1, tile_jogador_y) if self.eh_solido(x_tile, y_tile) and self.colidiu( posicao_pretendida.x + self.offset_caixa_x, posicao_pretendida.y + self.offset_caixa_y, self.largura_sprite, self.altura_sprite, x_tile, y_tile, self.tamanho_tile, self.tamanho_tile): posicao_pretendida += Vector2(1, 0) impedido = True continue #tile superior x_tile, y_tile = self.obter_x_y(tile_jogador_x, tile_jogador_y - 1) if self.eh_solido(x_tile, y_tile) and self.colidiu( posicao_pretendida.x + self.offset_caixa_x, posicao_pretendida.y + self.offset_caixa_y, self.largura_sprite, self.altura_sprite, x_tile, y_tile, self.tamanho_tile, self.tamanho_tile): posicao_pretendida += Vector2(0, 1) impedido = True continue #tile inferior x_tile, y_tile = self.obter_x_y(tile_jogador_x, tile_jogador_y + 1) if self.eh_solido(x_tile, y_tile) and self.colidiu( posicao_pretendida.x + self.offset_caixa_x, posicao_pretendida.y + self.offset_caixa_y, self.largura_sprite, self.altura_sprite, x_tile, y_tile, self.tamanho_tile, self.tamanho_tile): posicao_pretendida += Vector2(0, -1) impedido = True continue self.posicao = posicao_pretendida
def __init__(self, speed=0, color=0, model=''): self._speed = speed self._color = color self._model = model self._postition = Vector2() self._orientation = Vector2() #TODO make getter/setter for next 3 objects self._belong_to = None self._previous_belong_to = None self._next_belong_to = None
def test_catmull_rom_points(self): expected = [ Vector2(0.00, 0.00), Vector2(0.38, 0.44), Vector2(1.00, 1.00), Vector2(1.62, 1.06), Vector2(2.00, 1.00) ] actual = catmull_rom_points(self.points, subdivisions=self.subdivisions, close_loop=False) self.assertPointsListsEqual(expected, actual)
def perpendicular_vector(v: Vector2, direction: DirectionLR = RIGHT_DIR, length: float = None) -> Vector2: perp_vec = Vector2(v.y, -v.x) result = perp_vec if direction == RIGHT_DIR else -perp_vec if length is not None: result.set_length(length) return result
def __init__(self, posicao, velocidade): self.posicao = posicao self.velocidade_x = velocidade.x self.velocidade_y = velocidade.y self.velocidade_maxima = 2 self.aceleracao = 1 # +2 e +7 no bounding box pq estamos tentando criar um retangulo interno dentro do sprite self.offset_caixa_x = 2 self.largura_sprite = 3 self.offset_caixa_y = 0 self.altura_sprite = 8 self.tamanho_tile = 8 self.tile = Vector2(int(self.posicao.x // self.tamanho_tile), int(self.posicao.y // self.tamanho_tile))
def project_to_2D(euc_obj:Union[Point23, Vector23]) -> Union[Vector2, Point2]: """ Given a Point3/Vector3, return a Point2/Vector2 ignoring the original Z coordinate """ result:Union[Vector2, Point2] = None if isinstance(euc_obj, (Point2, Vector2)): result = euc_obj elif isinstance(euc_obj, Point3): result = Point2(euc_obj.x, euc_obj.y) elif isinstance(euc_obj, Vector3): result = Vector2(euc_obj.x, euc_obj.y) else: raise ValueError(f"Can't transform object {euc_obj} to a Point2 or Vector2") return result
def centroid(points: Sequence[PointVec23]) -> PointVec23: if not points: raise ValueError(f"centroid(): argument `points` is empty") first = points[0] is_3d = isinstance(first, (Vector3, Point3)) if is_3d: total = Vector3(0, 0, 0) else: total = Vector2(0, 0) for p in points: total += p total /= len(points) if isinstance(first, Point2): return Point2(*total) elif isinstance(first, Point3): return Point3(*total) else: return total