Exercises

  1. We can represent a rectangle by knowing three things: the location of its lower left corner, its width, and its height. Create a class definition for a Rectangle class using this idea. To create a Rectangle object at location (4,5) with width 6 and height 5, we would do the following:

    r = Rectangle(Point(4, 5), 6, 5)
    

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
     
    class Point:
        """ Point class for representing and manipulating x,y coordinates. """
     
        def __init__(self, initX, initY):
     
            self.x = initX
            self.y = initY
     
        def getX(self):
            return self.x
     
        def getY(self):
            return self.y
     
        def __str__(self):
            return "x=" + str(self.x) + ", y=" + str(self.y)
     
     
    class Rectangle:
        """Rectangle class using Point, width and height"""
     
        def __init__(self, initP, initW, initH):
     
            self.location = initP
            self.width = initW
            self.height = initH
     
    loc = Point(4, 5)
    r = Rectangle(loc, 6, 5)
    print(r)
     

    (ch_cl2_answer1)

    
    
    
    
    Show Comments
  2. Add the following accessor methods to the Rectangle class: getWidth, getHeight, __str__.


    1
    2
    3
     
     
     
     

    (ch_cl2_q2)

    
    
    
    
  3. Add a method area to the Rectangle class that returns the area of any instance:

    r = Rectangle(Point(0, 0), 10, 5)
    test(r.area(), 50)
    

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
     
    class Point:
        """ Point class for representing and manipulating x,y coordinates. """
     
        def __init__(self, initX, initY):
     
            self.x = initX
            self.y = initY
     
        def getX(self):
            return self.x
     
        def getY(self):
            return self.y
     
        def __str__(self):
            return "x=" + str(self.x) + ", y=" + str(self.y)
     
     
    class Rectangle:
        """Rectangle class using Point, width and height"""
     
        def __init__(self, initP, initW, initH):
     
            self.location = initP
            self.width = initW
            self.height = initH
     
        def area(self):
            return self.width * self.height
     

    (ch_cl2_q3answer)

    
    
    
    
    Show Comments
  4. Write a perimeter method in the Rectangle class so that we can find the perimeter of any rectangle instance:

    r = Rectangle(Point(0, 0), 10, 5)
    test(r.perimeter(), 30)
    

    1
    2
    3
     
     
     
     

    (ch_cl2_q4)

    
    
    
    
  5. Write a transpose method in the Rectangle class that swaps the width and the height of any rectangle instance:

    r = Rectangle(Point(100, 50), 10, 5)
    test(r.width, 10)
    test(r.height, 5)
    r.transpose()
    test(r.width, 5)
    test(r.height, 10)
    

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
     
    class Point:
        """ Point class for representing and manipulating x,y coordinates. """
     
        def __init__(self, initX, initY):
     
            self.x = initX
            self.y = initY
     
        def getX(self):
            return self.x
     
        def getY(self):
            return self.y
     
        def __str__(self):
            return "x=" + str(self.x) + ", y=" + str(self.y)
     
     
    class Rectangle:
        """Rectangle class using Point, width and height"""
     
        def __init__(self, initP, initW, initH):
     
            self.location = initP
            self.width = initW
            self.height = initH
     
        def transpose(self):
            temp = self.width
            self.width = self.height
            self.height = temp
     

    (ch_cl2_q5answer)

    
    
    
    
    Show Comments
  6. Write a new method in the Rectangle class to test if a Point falls within the rectangle. For this exercise, assume that a rectangle at (0,0) with width 10 and height 5 has open upper bounds on the width and height, i.e. it stretches in the x direction from [0 to 10), where 0 is included but 10 is excluded, and from [0 to 5) in the y direction. So it does not contain the point (10, 2). These tests should pass:

    r = Rectangle(Point(0, 0), 10, 5)
    test(r.contains(Point(0, 0)), True)
    test(r.contains(Point(3, 3)), True)
    test(r.contains(Point(3, 7)), False)
    test(r.contains(Point(3, 5)), False)
    test(r.contains(Point(3, 4.99999)), True)
    test(r.contains(Point(-3, -3)), False)
    
  7. Write a new method called diagonal that will return the length of the diagonal that runs from the lower left corner to the opposite corner.


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
     
    class Point:
        """ Point class for representing and manipulating x,y coordinates. """
     
        def __init__(self, initX, initY):
     
            self.x = initX
            self.y = initY
     
        def getX(self):
            return self.x
     
        def getY(self):
            return self.y
     
        def __str__(self):
            return "x=" + str(self.x) + ", y=" + str(self.y)
     
     
    class Rectangle:
        """Rectangle class using Point, width and height"""
     
        def __init__(self, initP, initW, initH):
     
            self.location = initP
            self.width = initW
            self.height = initH
     
        def diagonal(self):
     
            d = (self.width**2 + self.height**2) ** 0.5
            return d
     

    (ch_cl2_answer7)

    
    
    
    
    Show Comments
  8. In games, we often put a rectangular “bounding box” around our sprites in the game. We can then do collision detection between, say, bombs and spaceships, by comparing whether their rectangles overlap anywhere.

    Write a function to determine whether two rectangles collide. Hint: this might be quite a tough exercise! Think carefully about all the cases before you code.


    1
    2
    3
     
     
     
     

    (ch_cl2_q8)