예제 #1
0
def montecarlo(n: int):
    puntos = (Punto2D.of(random.uniform(-1., 1.), random.uniform(-1., 1.))
              for _ in range(n))
    pt = 0
    pi = 0
    for p in puntos:
        pt = pt + 1
        if p.distancia_al_origen <= 1:
            pi = pi + 1
    return 4 * pi / pt
예제 #2
0
def montecarlo_list(n: int):
    ls = []
    puntos = (Punto2D.of(random.uniform(-1., 1.), random.uniform(-1., 1.))
              for _ in range(n))
    pt = 0
    pi = 0
    i = 0
    for p in puntos:
        pt = pt + 1
        i = i + 1
        if p.distancia_al_origen <= 1:
            pi = pi + 1
        if i % 500 == 0:
            ls.append((i, 4 * pi / pt))
    return ls
예제 #3
0
    def rota(self, p: Punto2D, angulo: float) -> Circulo2D:
        return Circulo2D.of(self.centro.rota(p, angulo), self.radio)

    def traslada(self, v: Vector2D) -> Circulo2D:
        return Circulo2D.of(self.centro.traslada(v), self.radio)

    def homotecia(self, p: Punto2D, factor: float) -> Circulo2D:
        return Circulo2D.of(self.centro.homotecia(p, factor),
                            self.radio * factor)

    def proyecta_sobre_recta(self, r: Recta2D) -> Segmento2D:
        c = self.centro.proyectaSobre(r)
        u = r.vector.unitario()
        return Segmento2D.of_puntos(c + u * self.radio, c - u * (self.radio))

    def simetrico_con_respecto_a_recta(self, r: Recta2D) -> Circulo2D:
        return Circulo2D.of(self.centro.simetrico(r), self.radio)

    @property
    def shape(self) -> list[Patch]:
        return Draw.shape_circle([self.center.x, self.center.y],
                                 self.radio,
                                 fill=False)


if __name__ == '__main__':
    p = Punto2D.of(2.1, 4.5)
    c = Circulo2D.of(p, 5.6)
    print(c)
    print(c.area)
예제 #4
0
 def rectanguloHorizontal(x_min:Punto2D, x_max:Punto2D, y_min:Punto2D, y_max:Punto2D) -> Poligono2D:
     p0 = Punto2D.of(x_min, y_min)
     p1 = Punto2D.of(x_max, y_min)
     p2 = Punto2D.of(x_max, y_max)
     p3 = Punto2D.of(x_min, y_max)
     return Poligono2D.of([p0,p1,p2,p3])
예제 #5
0
    def rota(self, p:Punto2D, angulo:float) -> Poligono2D:
        return Poligono2D.of([x.rota(p,angulo) for x in self.vertices])

    def traslada(self, v:Vector2D) -> Poligono2D:
        return Poligono2D.of([x.traslada(v) for x in self.vertices])
    
    def homotecia(self, p:Punto2D, factor:float) -> Poligono2D:
        return Poligono2D.of([x.homotecia(p,factor) for x in self.vertices])
        
    def proyecta_sobre_recta(self,r:Recta2D) -> set[Punto2D]:
        return Poligono2D.of([x.proyecta_sobre_recta(r) for x in self.vertices])
    
    def simetrico_con_respecto_a_recta(self, r:Recta2D) -> Poligono2D:
        return Poligono2D.of([x.simetrico_con_respecto_a_recta(r) for x in self.vertices])
    
    @property
    def shape(self)->Patch:
        return Draw.shape_multiline([[p.x,p.y] for p in self.vertices],closed=True)


if __name__ == '__main__':
    v = Vector2D.of(1., 0.)
    pol = Poligono2D.cuadrado(Punto2D.origen(),v)
    print(pol)
    print(pol.area)
    print(pol.perimetro)
    print(pol.rota(Punto2D.of(1.,1.), pi/2))
    r = Poligono2D.rectangulo(Punto2D.of(1.,1.),v,2.)
    print(r)
    print("{0:.2f}".format(r.area))
    
예제 #6
0
        return Segmento2D(self.p1,self.p2)
    
    @property
    def vector(self) -> Vector2D:
        return self.p1.vector_to(self.p2)
    
    @property
    def modulo(self) -> float:        
        return self.p1.distancia_a(self.p2)

    def rota(self,p:Punto2D,angulo) -> Segmento2D:
        return Segmento2D.of_puntos(self.p1.rota(p,angulo),self.p2.rota(p,angulo))

    def traslada(self, v:Vector2D) -> Segmento2D:
        return Segmento2D.of_puntos(self.p1.traslada(v), self.p2.traslada(v))
    
    def homotecia(self, p:Punto2D, factor) -> Segmento2D:
        return Segmento2D.of(self.p1.homotecia(p,factor), self.p2.homotecia(p,factor))
    
    def proyecta_sobre_recta(self,r:Recta2D) -> Segmento2D:
        return Segmento2D.of(self.p1.proyecta_sobre_recta(r), self.p2.proyecta_sobre_recta(r))
    
    def simetrico_con_respecto_a_recta(self,r:Recta2D) -> Segmento2D:
        return Segmento2D.of(self.p1.simetrico(r), self.p2.simetrico(r))    
    @property
    def shape(self)->Patch:
        return Draw.shape_multiline([[self.p1.x,self.p1.y],[self.p2.x,self.p2.y]],closed=False)

if __name__ == '__main__':
    print(Segmento2D.of_puntos(Punto2D.of(1., 1.),Punto2D.of(-1., -1.)))
예제 #7
0
    vector: Vector2D

    @staticmethod
    def of(p: Punto2D, v: Vector2D) -> Recta2D:
        return Recta2D(p, v)

    @staticmethod
    def of_puntos(p1: Punto2D, p2: Punto2D) -> Recta2D:
        return Recta2D(p1, p2.vector_to(p1))

    def __str__(self) -> str:
        return '({0},{1})'.format(str(self.punto), str(self.vector))

    def punto_en_recta(self, factor: float = 0.) -> Punto2D:
        return self.punto + self.vector * factor

    def paralela(self, p: Punto2D) -> Recta2D:
        return Recta2D.of(p, self.vector)

    def ortogonal(self, p: Punto2D) -> Recta2D:
        return Recta2D.of(p, self.vector.ortogonal)


if __name__ == '__main__':
    p1 = Punto2D.origen()
    p2 = Punto2D.of(0., 1.)
    r = Recta2D.of_puntos(p1, p2)
    print(r)
    p3 = Punto2D.of(1.0, 7.0)
    print(p3.proyecta_sobre_recta(r))
    print(p3.simetrico_con_respecto_a_recta(r))