shadowm

Untitled

Mar 7th, 2014
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. class Point:
  2.     def __init__(self, (x, y) = (0, 0)):
  3.         self.x = x
  4.         self.y = y
  5.  
  6.  
  7. class Rectangle:
  8.     """A rectangle
  9.  
  10.    Attributes:
  11.        x: Abscissa of the top-left corner of the rectangle.
  12.        y: Ordinate of the top-left corner of the rectangle.
  13.        w: Width. Should be >= 0.
  14.        h: Height. Should be >= 0.
  15.    """
  16.     def __init__(self, x = 0, y = 0, w = 0, h = 0):
  17.         self.x = x
  18.         self.y = y
  19.         self.w = w
  20.         self.h = h
  21.  
  22.     def diagonal(self):
  23.         "Returns the diagonal of the rectangle."
  24.         return math.sqrt(self.w ** 2 + self.h ** 2)
  25.  
  26.     def surface(self):
  27.         "Returns the surface of the rectangle."
  28.         return self.w * self.h
  29.  
  30.     def is_null(self):
  31.         return self.surface() == 0
  32.  
  33.     def origin(self):
  34.         "Returns the coordinates for the top-left corner."
  35.         return (self.x, self.y)
  36.  
  37.     def end(self):
  38.         "Returns the coordinates for the bottom-right corner."
  39.         return (self.x + self.w, self.y + self.h)
  40.  
  41.     def p1(self):
  42.         return self.origin()
  43.  
  44.     def p2(self):
  45.         return (self.x + self.w, self.y)
  46.  
  47.     def p3(self):
  48.         return (self.x, self.y + self.h)
  49.  
  50.     def p4(self):
  51.         return self.end()
  52.  
  53.     def points(self):
  54.         "Returns a tuple of the corner points for this rectangle."
  55.         return (self.p1(), self.p2(), self.p3(), self.p4())
  56.  
  57.     def contains(self, x, y):
  58.         "Returns whether the given point is contained within the rectangle."
  59.         return x in range(self.x, self.x + self.w) and y in range(self.y, self.y + self.h)
  60.  
  61.     def overlaps(self, other):
  62.         "Returns whether both rectangles overlap."
  63.         my_points = self.points()
  64.         other_points = other.points()
  65.         for p in self.points():
  66.             if other.contains(p[0], p[1]):
  67.                 return True
  68.         for p in other.points():
  69.             if self.contains(p[0], p[1]):
  70.                 return True
  71.         return False
  72.  
  73.     def __lt__(self, other):
  74.         return self.surface() < other.surface()
  75.  
  76.     def __le__(self, other):
  77.         return self < other or self == other
  78.  
  79.     def __eq__(self, other):
  80.         return not self < other and not other < self
  81.  
  82.     def __ne__(self, other):
  83.         return not self == other
  84.  
  85.     def __gt__(self, other):
  86.         return other < self
  87.  
  88.     def __ge__(self, other):
  89.         return other < self or self == other
  90.  
  91.     def __str__(self):
  92.         return "(%d, %d) %dx%d" % (self.x, self.y, self.w, self.h)
Advertisement
Add Comment
Please, Sign In to add comment