'''Box class for CITS5501''' class Box: def __init__(self, x1,y1,x2,y2): '''Creates a new Box with the given coordinates >>> b = Box(1,2,3,4) >>> print(b) (1,2),(3,4) ''' self.x1 = min(x1,x2) self.x2 = max(x1,x2) self.y1 = min(y1,y2) self.y2 = max(y1,y2) def __str__(self): return '('+str(self.x1)+','+str(self.y1)+'),('+str(self.x2)+','+str(self.y2)+')' def __eq__(self,other): if not isinstance(other, Box): return False return self.x1==other.x1 and self.x2==other.x2 and self.y1==other.y1 and self.y2==other.y2 def area(self): return (self.x2-self.x1)*(self.y2-self.y1) def intersect(self, box): '''Creates the intersection of two boxes or a zero box if the intersectio is empty >>> b = Box(1,1,4,4) >>> c = Box(3,3,5,5) >>> print(b.intersect(c)) (3,3),(4,4) ''' if not isinstance(box, Box): return Box(0,0,0,0) x1 = max(self.x1, box.x1) x2 = min(self.x2, box.x2) y1 = max(self.y1, box.y1) y2 = min(self.y2, box.y2) if x1>x2 or y1>y2: return Box(0,0,0,0) return Box(x1,y1,x2,y2) if __name__ == "__main__": import doctest doctest.testmod()