Advertisement
FranzVuttke

gently class example

Jan 13th, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. #
  2. # https://www.facebook.com/groups/python/posts/1358040128369881/?#__cft__[0]=AZVWhQT874BcUFK_Nin8pNnbzSjOu2jFt8SAJuHRuRKN983HbvO5GeylsexILySHLs7IQaoY_RrOCrWPwVAu7NRZ5GPHNhyunM_427cCD4ibWHTFGRkRx_G1sbW# 8MdXJ8B457UDE-W7j7mhpjYslSO0q&__tn__=%2CO%2CP-R
  3. #
  4.  
  5. from math import sqrt
  6.  
  7. class Point:
  8.     def __init__(self, x=0, y=0):
  9.         self.x = x
  10.         self.y = y
  11.  
  12.     def distancce(self, other):
  13.         dX, dY = other.x - self.x, other.y - self.y
  14.         return sqrt(dX**2 + dY**2)
  15.  
  16.     def __repr__(self):
  17.         return f'({(self.x, self.y)})'
  18.  
  19.  
  20. class Circle:
  21.     def __init__(self, center = Point(0,0), radius = 1):
  22.         self.center = center
  23.         self.radius = radius
  24.  
  25.     def intersects(self, other):
  26.         return self.center.distancce(other.center) <= self.radius + other.radius
  27.  
  28.     def __lt__(self, other):
  29.         return self.radius < other.radius
  30.  
  31.  
  32.     def __repr__(self):
  33.         return "Circle with center {}, radius {}".format(self.center, self.radius)
  34.  
  35. # TEST
  36.  
  37. def main(args):
  38.  
  39.     c1 = Circle()
  40.     c2 = Circle(radius=6, center=Point(3,4))
  41.  
  42.     print("c1 = the {}".format(c1))
  43.     print("c2 = the {}".format(c2))
  44.  
  45.     print("Their centers area {} units apart".format(c1.center.distancce(c2.center)))
  46.     if c1.intersects(c2):
  47.         print("The given circles intersects each other.")
  48.     else:
  49.         print("No intersection!")
  50.  
  51.     print("c1 is","smaller then " if c1 < c2 else "greater then","c2")
Tags: class Example
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement