#------------------------------------------------------------------------------ # geometry.py # Definition of the Point, Line and Rectangle classes. #------------------------------------------------------------------------------ import math class Point(object): """Class representing a Point in the x-y coordinate plane.""" def __init__(self, x, y): """Initialize a Point object.""" self.xcoord = x self.ycoord = y # end def __str__(self): """Return the string representation of a Point.""" return '({}, {})'.format(self.xcoord, self.ycoord) # end def __repr__(self): """Return the detailed string representation of a Point.""" return 'geometry.Point({}, {})'.format(self.xcoord, self.ycoord) # end def __eq__(self, other): """ Return True if self and other have the same coordinates, False otherwise. """ eqx = (self.xcoord==other.xcoord) eqy = (self.ycoord==other.ycoord) return eqx and eqy # end def distance(self, other): """Return the distance between self and other.""" diffx = self.xcoord - other.xcoord diffy = self.ycoord - other.ycoord return math.sqrt( diffx**2 + diffy**2 ) # end def norm(self): """Return the distance from self to the origin (0, 0).""" return self.distance(Point(0,0)) # end def midpoint(self, other): """Return the midpoint of the line segment from self to other.""" midx = (self.xcoord + other.xcoord)/2 midy = (self.ycoord + other.ycoord)/2 return Point(midx, midy) # end # end class Line(object): """Class representing a Line in the x-y coordinate plane.""" pass # Exercise: fill in this definition # end class Rectangle(object): """ Class representing a Rectangle in the x-y coordinate plane whose sides are parallel to the coordinate axes. """ def __init__(self, UL, LR): """Initialize a Rectangle object.""" self.upper_left = UL self.lower_right = LR # end def __str__(self): """Return a string representation of self.""" UL = self.upper_left LR = self.lower_right UR = Point(LR.xcoord, UL.ycoord) LL = Point(UL.xcoord, LR.ycoord) return '{}--{}\n{}--{}\n'.format(UL, UR, LL, LR) # end def __repr__(self): """Return a detailed string representation of self.""" UL = self.upper_left LR = self.lower_right UR = Point(LR.xcoord, UL.ycoord) # upper right LL = Point(UL.xcoord, LR.ycoord) # lower left return 'geometry.Rectangle( {}, {}, {}, {} )'.format(UL, UR, LL, LR) def width(self): """Return the width of self.""" return abs( self.lower_right.xcoord - self.upper_left.xcoord ) # end def height(self): """Return the height of self.""" return abs( self.upper_left.ycoord - self.lower_left.ycoord ) # end def diagonal(self): """Return the length of the diagonal of the self.""" UL = self.upper_left LR = self.lower_right return abs( UL.distance(LR) ) # end def area(self): """Return the area of self.""" return self.width()*self.height() # end def contains_point(self, P): """Return True if self contains Point P, False otherwise.""" a = self.upper_left.xcoord b = P.xcoord c = self.lower_right.xcoord d = self.lower_right.ycoord e = P.ycoord f = self.upper_left.ycoord return a<=b<=c and d<=e<=f # end def contains_rect(self, other): """Return True if self contains Rectangle other, False otherwise.""" UL = other.upper_left LR = other.lower_right UR = Point(LR.xcoord, UL.ycoord) # upper right LL = Point(UL.xcoord, LR.ycoord) # lower left return self.contains_point(UL) and \ self.contains_point(UR) and \ self.contains_point(LL) and \ self.contains_point(LR) # end def intersects_rect(self, other): """Return True of self intersects Rectangle other, False otherwise.""" UL = other.upper_left LR = other.lower_right UR = Point(LR.xcoord, UL.ycoord) # upper right LL = Point(UL.xcoord, LR.ycoord) # lower left return self.contains_point(UL) or \ self.contains_point(UR) or \ self.contains_point(LL) or \ self.contains_point(LR) # end def intersection(self, other): """ Return the Rectangle that is the intersection of the Rectangles self and other. """ pass # end # end #------------------------------------------------------------------------------ # test the above classes #------------------------------------------------------------------------------ def main(): P = Point(1, 7) Q = Point(5, 7) R = Point(3, 5) S = Point(7, 5) T = Point(1, 3) U = Point(5, 3) V = Point(3, 1) W = Point(7, 1) A = Rectangle(P, U) B = Rectangle(R, W) print(A) print(B) print(A.contains_point(R)) print(B.contains_point(U)) print(A.contains_point(V)) print(A.contains_point(S)) print(B.contains_point(T)) print(B.contains_point(Q)) C = Rectangle(P, W) D = Rectangle(R, U) print(C.contains_rect(D)) print(D.contains_rect(C)) print(A.intersects_rect(B)) print(B.intersects_rect(A)) E = Rectangle(P, R) F = Rectangle(U, W) print(E.intersects_rect(F)) print(F.intersects_rect(E)) # end #------------------------------------------------------------------------------ if __name__=='__main__': main() # end