Bonjour,
D'après un exercice de M. Swinnen: avec 2 classes Point et Rect, je n'arrive pas à afficher les coordonnées du centre d'un rectangle.
Merci pour votre aide.
Code python : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Point(object):        
    def __init__ (self, xx=0, yy=0):              
        self.x=xx
        self.y=yy        
    def coord(self) :
        return ("({}, {})".format (self.x,self.y))
 
class Rect (object):
    "rectangle défini avec long, larg et coord du sommet (haut gauche)"
    def __init__ (self, L,l,x,y):
        Point.__init__(self,x,y)
        self.long=L
        self.larg=l
        self.sommet=Point(x,y)
        self.centr=Point (0,0)
    def centre (self,centr): 
        x1=self.sommet.x+self.long /2                              
        y1=self.sommet.y+self.larg /2
        centr=Point(x1,y1)
        return centr
 
A, B = Point(3, 5), Point (2,6)    
r1 = Rect(50,35,12,27)     
print ('Le rectangle : L x l =',r1.long,'x',r1.larg,'de sommet ',\
        r1.sommet.coord(),' a pour centre ???',r1.centr.coord())