jeudi 1 juin 2017

Python: how to call the method

I'm having issues calling the method aleatorio() of my class. I need to create a random point p using the aleatorio() method of my ponto class, so i can then use it in my retangulo class with the interior() method, to basically check if the random point falls in the interior of each of my two rectangles, r1 and r2. However, it seems that i'm not being able to generate the random point p that i need. Can anyone help me?

from random import uniform
class ponto:
    def __init__(self,x,y):
        self.x=float(x)
        self.y=float(y)

    def aleatorio(self):
        ''' Ponto aleatorio com coordenadas 0.0 a 10.0 '''
        self.x=uniform(0.0,10.0)
        self.y=uniform(0.0,10.0)

class retangulo():
    def __init__(self,a,b):
        self.a=a
        self.b=b

    def interior(self,p):
        ''' Verifica se ponto no interior do retangulo '''
        if p.x >= self.a.x and p.x <=self.b.x and p.y >=self.a.y and p.y<=self.b.y:
            return True
        return False

    def area(self):
        return self.a*self.b


a1=ponto(0.0,0.0)
b1=ponto(2.0,2.0)    
r1=retangulo(a1,b1)    
b2=ponto(4.0,4.0)    
r2=retangulo(a1,b2) 

p=ponto(0.4,0.9)
p.aleatorio()

d1=0
d2=0
for r in range(10000):
    if r1.interior(p)==True:
        d1+=1
    elif r2.interior(p)==True:
        d2+=1
print(d1,d2)


As suggested I added the print of d1,d2 which returns: 0 0. d1 and d2 are supposed to be the number of times my random point falls inside r1 and r2, respectively. I guess 0 either means i'm not generating the random point or that i'm simply not counting the number of times it falls inside correctly, but i'm not sure what the reason is.




Aucun commentaire:

Enregistrer un commentaire