Advertisement
msoo248

Untitled

Dec 12th, 2019
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | None | 0 0
  1. from math import pi as PI
  2.  
  3. class Point:
  4.     """Klasa opisująca punkt na płaszczyźnie"""
  5.     def __init__ (self,x1,y1):
  6.       self.x = x1
  7.       self.y = y1
  8.  
  9.     def __str__(self):
  10.       return "Lokalizacja punktu: x={}, y={}".format(self.x,self.y)
  11.  
  12.  
  13. class Figure(Point):
  14.     """Klasa opisująca figurę na płaszczyźnie o środku w punkcie Point"""
  15.     listOfFigures = []
  16.     def __init__ (self,x,y,
  17.                 colour = "czerwony",
  18.                 name="figura",
  19.                 lineWidth=2,
  20.                 lineStyle='-'):
  21.         super().__init__(x,y)
  22.         self.colour = colour
  23.         self.name = name
  24.         self.lineWidth = lineWidth
  25.         self.lineStyle = lineStyle
  26.         Figure.listOfFigures.append(self)
  27.  
  28.     def __str__(self):
  29.         return 'Środek figury: x={}, y={}\nKolor figury = "{}"\nTyp figury = {}\nGrubość linii = {} \nStyl linii = "{}"'.format(self.x,self.y, self.colour,self.name,self.lineWidth,self.lineStyle)
  30.  
  31.     def area(self):
  32.         """Metoda licząca pole figury"""
  33.         pass
  34.  
  35.     def perimeter(self):
  36.         """Metoda licząca obwód figury"""
  37.         pass
  38.  
  39.  
  40. class Triangle(Figure):
  41.     def __init__ (self,x,y,
  42.             base,
  43.             sideA,
  44.             sideB,
  45.             height,
  46.             colour = "czerwony",
  47.             name = "trójkąt",
  48.             lineWidth=2,
  49.             lineStyle='-'):
  50.             super().__init__(x,y,colour,name,lineWidth,lineStyle)
  51.             self.base = base
  52.             self.sideA = sideA
  53.             self.sideB = sideB
  54.             self.height = height
  55.  
  56.     def __str__(self):
  57.         return "{} \nPole = {}, obwód = {}".format(super().__str__(),self.area(),self.perimeter())
  58.  
  59.     def perimeter(self):
  60.         return self.base + self.sideA + self.sideB
  61.  
  62.     def area(self):
  63.         return 0.5*self.base*self.height
  64.  
  65.  
  66. class Rectangle(Figure):
  67.     def __init__ (self,x,y,
  68.         sideA,
  69.         sideB,
  70.         colour = "czerwony",
  71.         name = "prostokąt",
  72.         lineWidth=2,
  73.         lineStyle='-'):
  74.             super().__init__(x,y,colour,name,lineWidth,lineStyle)
  75.             self.sideA = sideA
  76.             self.sideB = sideB
  77.  
  78.     def __str__(self):
  79.         return "{} \nPole = {}, obwód = {}".format(super().__str__(),self.area(),self.perimeter())
  80.     def perimeter(self):
  81.         return 2*self.sideA + 2*self.sideB
  82.  
  83.     def area(self):
  84.         return self.sideA*self.sideB
  85.  
  86. class Circle(Figure):
  87.     def __init__ (self,x,y,
  88.         radius,
  89.         colour = "czerwony",
  90.         name = "koło",
  91.         lineWidth=2,
  92.         lineStyle='-'):
  93.         super().__init__(x,y,colour,name,lineWidth,lineStyle)
  94.         self.radius = radius
  95.  
  96.     def __str__(self):
  97.         return "{} \nPole = {}, obwód = {}".format(super().__str__(),self.area(),self.perimeter())
  98.  
  99.     def perimeter(self):
  100.         return 2*PI*self.radius
  101.  
  102.     def area(self):
  103.         return self.radius**2*PI
  104.  
  105.  
  106. def main():
  107.     pkt = Figure(3,4)      
  108.     trian = Triangle(2,2,4,5,6,7)
  109.     rect = Rectangle(2,3,2,2)
  110.     circ = Circle(2,3,12)
  111.  
  112.     listOfFigures = [pkt,trian,rect,circ]
  113.     for figure in listOfFigures:
  114.         print(figure)
  115.         print(30*'-')
  116.  
  117.     print("Liczba figur: {}" .format(len(listOfFigures)))    
  118.     print("Lista figur: {}" .format(listOfFigures))
  119.  
  120.  
  121. if __name__ == '__main__':
  122.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement