Example #1
0
def run_test_draw_lines_from_rectangles():
    """ Tests the   draw_lines_from_rectangles  function. """
    print()
    print('--------------------------------------------------')
    print('Testing the  draw_lines_from_rectangles  function:')
    print('  See the graphics windows that pop up.')
    print('--------------------------------------------------')

    # TWO tests on ONE window.
    title = 'Tests 1 & 2 of DRAW_LINES_FROM_RECTANGLES:'
    title += '  5 lines, 8 lines!'
    window1 = rg.RoseWindow(900, 400, title)

    rectangle1 = rg.Rectangle(rg.Point(100, 25), rg.Point(150, 125))
    rectangle2 = rg.Rectangle(rg.Point(300, 150), rg.Point(400, 175))
    rectangle1.outline_color = 'red'
    rectangle2.outline_color = 'blue'
    draw_lines_from_rectangles(rectangle1, rectangle2, 5, window1)

    rectangle1 = rg.Rectangle(rg.Point(870, 30), rg.Point(750, 100))
    rectangle2 = rg.Rectangle(rg.Point(700, 90), rg.Point(650, 60))
    rectangle2.outline_color = 'green'
    draw_lines_from_rectangles(rectangle1, rectangle2, 8, window1)

    window1.close_on_mouse_click()

    # A third test on ANOTHER window.
    title = 'Test 3 of DRAW_LINES_FROM_RECTANGLES:  11 lines!'
    window2 = rg.RoseWindow(700, 700, title)

    rectangle1 = rg.Rectangle(rg.Point(550, 200), rg.Point(650, 100))
    rectangle2 = rg.Rectangle(rg.Point(600, 50), rg.Point(650, 75))
    rectangle1.outline_color = 'brown'
    rectangle2.outline_color = 'cyan'
    rectangle2.outline_thickness = 10
    draw_lines_from_rectangles(rectangle1, rectangle2, 11, window2)

    window2.close_on_mouse_click()
Example #2
0
def run_test_draw_lines_from_rectangles():
    """ Tests the   draw_lines_from_rectangles  function. """
    print()
    print('--------------------------------------------------')
    print('Testing the  draw_lines_from_rectangles  function:')
    print('  See the graphics windows that pop up.')
    print('--------------------------------------------------')

    # TWO tests on ONE window.
    title = 'Tests 1 & 2 of DRAW_LINES_FROM_RECTANGLES:'
    title += '  10 lines, 14 lines.'
    window1 = rg.RoseWindow(1000, 1000, title)

    rectangle1 = rg.Rectangle(rg.Point(575, 550), rg.Point(425, 450))
    rectangle2 = rg.Rectangle(rg.Point(445, 400), rg.Point(450, 300))
    rectangle1.outline_color = 'red'
    rectangle2.outline_color = 'blue'
    draw_lines_from_rectangles(rectangle1, rectangle2, 10, window1)

    rectangle1 = rg.Rectangle(rg.Point(140, 100), rg.Point(600, 200))
    rectangle2 = rg.Rectangle(rg.Point(100, 140), rg.Point(625, 35))
    rectangle2.outline_color = 'green'
    draw_lines_from_rectangles(rectangle1, rectangle2, 14, window1)

    window1.close_on_mouse_click()

    # A third test on ANOTHER window.
    title = 'Test 3 of DRAW_LINES_FROM_RECTANGLES:  14 lines.'
    window2 = rg.RoseWindow(700, 700, title)

    rectangle1 = rg.Rectangle(rg.Point(550, 200), rg.Point(650, 100))
    rectangle2 = rg.Rectangle(rg.Point(600, 50), rg.Point(650, 75))
    rectangle1.outline_color = 'blue'
    rectangle2.outline_color = 'green'
    rectangle2.outline_thickness = 10
    draw_lines_from_rectangles(rectangle1, rectangle2, 14, window2)

    window2.close_on_mouse_click()
Example #3
0
    def __init__(self, intersection_center, width, height, letter_thickness):
        """
        *** See   dimensions.pdf   to understand the following! ***

        What comes in:
           -- self
           -- an rg.Point for the intersection center of the CapitalT
              -- This point is also center of the horizontal rectangle.
           -- a int for the width of the CapitalT
                 (that is, the width of the horizontal rectangle)
           -- a int for the height of the CapitalT
                 (that is, the height of the vertical rectangle)
           -- a int for the CapitalT's thickness, that is,
                 the height of the horizontal rectangle and also
                 the width of the vertical rectangle.
        What goes out:  Nothing (i.e., None).
        Side effects: Sets two instance variables named:
          -- h_rect  (to represent the horizontal rectangle in the T,
                      that is, the top bar)
          -- v_rect  (to represent the vertical rectangle in the T,
                      that is, the | part of the T)

           *** See   dimensions.pdf   to understand the above! ***

        Each rectangle is an rg.Rectangle.

        IMPORTANT RESTRICTION:  Unlike prior modules you are NOT allowed
            to make any other instance variables than h_rect and v_rect.
            You must figure out how to do the problem with ONLY
            those two instance variables.

        Example:
            t1 = CapitalT(rg.Point(300, 50), 100, 200, 20)
                -- t1.h_rect would have an upper left corner of (250, 40)
                -- t1.h_rect would have an lower right corner of (350, 60)
                -- t1.v_rect would have an upper left corner of (290, 40)
                -- t1.v_rect would have an lower right corner of (310, 240)

            *** Make sure that you understand this example before     ***
            *** proceeding. See    dimensions.pdf   to understand it! ***

        Type hints:
          :type intersection_center: rg.Point
          :type width:               int
          :type height:              int
          :type letter_thickness:    int
        """
        # ---------------------------------------------------------------------
        # DONE: 3.
        #   READ the above specification, including the Example.
        #   Implement this method, using the instance variables
        #      h_rect
        #      v_rect
        #   and *** NO OTHER INSTANCE VARIABLES. ***
        #   Note: Implement   attach_to   before testing this __init__ method.
        # ---------------------------------------------------------------------
        self.h_rect = rg.Rectangle(
            rg.Point(intersection_center.x - width / 2, intersection_center.y + letter_thickness / 2),
            rg.Point(intersection_center.x + width / 2, intersection_center.y - letter_thickness / 2))
        self.v_rect = rg.Rectangle(
            rg.Point(intersection_center.x - letter_thickness / 2, intersection_center.y - letter_thickness / 2),
            rg.Point(intersection_center.x + letter_thickness / 2,
                     intersection_center.y + (height - letter_thickness / 2)))
    def __init__(self, intersection_center, width, height, letter_thickness):
        """
        What comes in:
           -- self
           -- an rg.Point for the intersection center of the CapitalT
              -- This point is also center of the horizontal rectangle.
           -- a int for the width of the CapitalT (the width of the horizontal rectangle)
           -- a int for the height of the CapitalT (the height of the vertical rectangle)
           -- a int for the thickness of each rectangle (the letter's thickness)
        What goes out:  Nothing (i.e., None).
        Side effects: Sets two instance variables named:
          -- h_rect  (to represent the horizontal rectangle in the T, the top bar)
          -- v_rect  (to represent the vertical rectangle in the T, the | part of the T)

           *** See the dimensions PDF for the exact placement of the rectangles in the T. ***

        Each rectangle is an rg.Rectangle. Unlike prior modules you are NOT
            allowed to make any other instance variables. You may only use
            exactly these two and must figure out how to do the problem with ONLY
            those two instance variables.

        Example:
            t1 = CapitalT(rg.Point(300, 50), 100, 200, 20)
                -- t1.h_rect would have an upper left corner of (250, 40)
                -- t1.h_rect would have an lower right corner of (350, 60)
                -- t1.v_rect would have an upper left corner of (290, 40)
                -- t1.v_rect would have an lower right corner of (310, 240)

        Type hints:
          :type intersection_center: rg.Point
          :type width:   int
          :type height:   int
          :type letter_thickness:   int
        """
        # --------------------------------------------------------------
        # Done: 3.
        #   READ the above specification, including the Example.
        #   Implement this method
        #   Note: you will need to also implement attach_to before testing
        # --------------------------------------------------------------
        self.intersection_center = intersection_center
        self.width = width
        self.height = height
        self.letter_thickness = letter_thickness

        h_rect_point_start = (rg.Point(
            self.intersection_center.x - (self.width / 2),
            self.intersection_center.y - self.letter_thickness / 2))
        h_rect_point_end = (rg.Point(
            self.intersection_center.x + (self.width / 2),
            self.intersection_center.y + self.letter_thickness / 2))

        h_rect = rg.Rectangle(h_rect_point_start, h_rect_point_end)

        v_rect_point_start = (rg.Point(
            self.intersection_center.x - (self.letter_thickness / 2),
            self.intersection_center.y - self.letter_thickness / 2))
        v_rect_point_end = (rg.Point(
            self.intersection_center.x + (self.letter_thickness / 2),
            self.intersection_center.y +
            (self.height - self.letter_thickness / 2)))

        v_rect = rg.Rectangle(v_rect_point_start, v_rect_point_end)

        self.h_rect = h_rect
        self.v_rect = v_rect
def many_hourglasses(window, square, m, colors):
    """
    See   many_hourglasses_picture.pdf   in this project for pictures that may
    help you better understand the following specification:

    Displays  m  rectangles, where:
      -- Each rectangle has an hourglass of circles inside it,
           per the  hourglass  function above.
      -- The circles in the hourglasses are all the same size.
      -- The leftmost rectangle is the given square, and it contains
           an hourglass with a single circle that fills the square.
      -- Each successive rectangle is immediately to the right of the
           previous rectangle, and each contains an hourglass with
           the hourglass'  n   being one greater than the  n  used
           for the previous rectangle.
      -- The colors for the hourglass figures use the given sequence of
           colors, "wrapping" if m exceeds the length of the sequence.

    Preconditions:
      :type window: rg.RoseWindow
      :type square: rg.Square
      :type m: int
      :type colors: (list | tuple) of str
    where m is positive and colors is a sequence of strings,
    each of which denotes a color that rosegraphics understands.
    """
    # ------------------------------------------------------------------
    # TODO: 3. Implement and test this function.
    #       We provided some tests for you (above).
    # ------------------------------------------------------------------
    ####################################################################
    # IMPORTANT:
    #   1. Partial credit if you draw JUST the rectangles.
    #   2. No additional credit unless you CALL the  hourglass  function
    #        in the PREVIOUS problem appropriately
    #        to draw the hourglass figures.
    ####################################################################
    # ------------------------------------------------------------------
    # DIFFICULTY AND TIME RATINGS (see top of this file for explanation)
    #    DIFFICULTY:      7  (assuming that you already have
    #                         a correct "hourglass" function above)
    #    TIME ESTIMATE:  20 minutes (warning: this problem is challenging)
    # ------------------------------------------------------------------
    center = square.center
    centerx = square.center.x
    centery = square.center.y
    length = square.length_of_each_side
    original_length = length
    ul = rg.Point(square.center.x - (original_length / 2), square.center.y - (original_length / 2))
    original_ul = ul
    lr = rg.Point(square.center.x + (original_length / 2), square.center.y + (original_length / 2))
    original_lr = lr
    ulx = ul.x
    original_ulx = ulx
    uly = ul.y
    original_uly = uly
    lrx = lr.x
    original_lrx = lrx
    lry = lr.y
    original_lry = lry
    index = 0

    for k in range(m):
        rect = rg.Rectangle(rg.Point(ulx, uly), rg.Point(lrx, lry))
        rect.attach_to(window)
        hourglass(window, k + 1, rect.get_center(), length / 2, colors[index])
        window.render()
        index = index + 1
        if index == len(colors):
            index = 0
        ulx = ulx + (k + 1) * length
        uly = uly - (length) * (1 / 1.15)
        lrx = lrx + ((1 + (k + 1)) * length)
        lry = lry + length * (1 / 1.15)
def circle_and_rectangle():
    """
    -- Constructs an rg.RoseWindow.
    -- Constructs and draws a rg.Circle and rg.Rectangle
       on the window such that:
          -- They fit in the window and are easily visible.
          -- The rg.Circle is filled with 'blue'
    -- Prints (on the console, on SEPARATE lines) the following data
         associated with your rg.Circle:
          -- Its outline thickness.
          -- Its fill color.
          -- Its center.
          -- Its center's x coordinate.
          -- Its center's y coordinate.
    -- Prints (on the console, on SEPARATE lines) the same data
         but for your rg.Rectangle.
    -- Waits for the user to press the mouse, then closes the window.

    Here is an example of the output on the console,
    for one particular circle and rectangle:
           1
           blue
           Point(180.0, 115.0)
           180
           115
           1
           None
           Point(75.0, 150.0)
           75.0
           150.0
    """

    window = rg.RoseWindow(500, 500)

    cp1 = rg.Point(400, 400)
    circle1 = rg.Circle(cp1, 30)
    circle1.fill_color = 'blue'
    circle1.attach_to(window)

    p1 = rg.Point(50, 50)
    p2 = rg.Point(200, 200)
    rect = rg.Rectangle(p1, p2)
    rect.attach_to(window)

    window.render()

    cthickness = circle1.outline_thickness
    ccolor = circle1.fill_color
    ccenter = circle1.center
    cxcoor = ccenter.x
    cycoor = ccenter.y
    print(cthickness)
    print(ccolor)
    print(ccenter)
    print(cxcoor)
    print(cycoor)

    rthickness = rect.outline_thickness
    rcolor = rect.fill_color
    rcenter = rect.get_center()
    rxcoor = rcenter.x
    rycoor = rcenter.y
    print(rthickness)
    print(rcolor)
    print(rcenter)
    print(rxcoor)
    print(rycoor)

    window.close_on_mouse_click()
def draw_squares_from_circle(n, circle, window):
    """
    What comes in:  Three arguments:
      -- A positive integer n.
      -- An rg.Circle.
      -- An rg.RoseWindow.
    What goes out:  Nothing (i.e., None).
    Side effects:
      See   draw_squares_from_circle.pdf   in this project for pictures
        that may help you better understand the following specification:

      First draws the given rg.Circle on the given rg.RoseWindow.
      Then draws  n  rg.Squares on the given rg.RoseWindow, such that:
        -- The first rg.Square circumscribes the given rg.Circle.
        -- Each subsequent rg.Square has its upper-left quarter
             on top of the lower-right quarter of the previous rg.Square,
             so that the squares form an overlapping sequence
             that goes down and to the right.
      Must  ** render **     but   ** NOT close **   the window.

    Type hints:
      :type n: int
      :type circle: rg.Circle
      :type window: rg.RoseWindow
    """
    # ------------------------------------------------------------------
    # done: 2. Implement and test this function.
    #          Tests have been written for you (above).
    #
    # C ONSIDER using the ACCUMULATOR IN GRAPHICS pattern,
    #             as in   draw_row_of_circles   in m1e,
    #             instead of directly using the loop variable.
    #
    ####################################################################
    # HINT: To figure out the code that computes the necessary
    #       positions of each square,
    #          ** FIRST DO A CONCRETE EXAMPLE BY HAND! **
    ####################################################################
    # ------------------------------------------------------------------

    center = circle.center
    fill_color = circle.fill_color
    x = center.x
    y = center.y
    radius = circle.radius
    circle = rg.Circle(center, radius)
    circle.fill_color = fill_color

    # Attach the object(s) to the window.
    circle.attach_to(window)

    for k in range(n):
        # ------------------------------------------------------------------
        # Rectangle: needs two opposite corners.
        # ------------------------------------------------------------------
        point1 = rg.Point(x - radius, y - radius)
        point2 = rg.Point(x + radius, y + radius)
        rectangle = rg.Rectangle(point1, point2)
        rectangle.attach_to(window)
        x = x + radius
        y = y + radius

        # ------------------------------------------------------------------
        # render: Draw ALL the objects attached to this window.
        # ------------------------------------------------------------------
        window.render()
def draw_circles_from_rectangle(m, n, rectangle, window):
    """
    What comes in:  Four arguments:
      -- Positive integers m and n.
      -- An rg.Rectangle.
      -- An rg.RoseWindow.
    What goes out:  Nothing (i.e., None).
    Side effects:
      See   draw_circles_from_rectangle.pdf   in this project for pictures
        that may help you better understand the following specification:

      First draws the given rg.Rectangle on the given rg.RoseWindow.
      Then draws  m  rg.Circles on the given rg.RoseWindow, such that:
        -- The diameter of each rg.Circle is the same as the height
             of the given rg.Rectangle.
        -- The first rg.Circle is immediately to the left of the
             given rg.Rectangle
        -- Each subsequent rg.Circle is immediately to the left
             of the previous rg.Circle, so that the circles form a row
             that goes to the left.
        -- Each rg. Circle has the same fill_color as the given
             rg.Rectangle (and has no outline_color).
      Then draws  n  rg.Circles on the given RoseWindow, such that:
        -- The diameter of each rg.Circle is the same as the width
             of the given rg.Rectangle.
        -- The first rg.Circle is immediately above the
             given rg.Rectangle
        -- Each subsequent rg.Circle is immediately above the previous
             rg.Circle, so that the circles form a column that goes up.
        -- Each rg.Circle has the same outline_color as the given
             rg.Rectangle (and has no fill_color).
      Must  ** render **     but   ** NOT close **   the window.

    Type hints:
      :type m: int
      :type n: int
      :type rectangle: rg.Rectangle
      :type window: rg.RoseWindow
    """
    # ------------------------------------------------------------------
    # done: 4. Implement and test this function.
    #          Tests have been written for you (above).
    #
    # C ONSIDER using the ACCUMULATOR IN GRAPHICS pattern,
    #             as in   draw_row_of_circles   in m1e,
    #             instead of directly using the loop variable.
    #
    ####################################################################
    # HINT: To figure out the code that computes the necessary
    #       positions of each circle,
    #          ** FIRST DO A CONCRETE EXAMPLE BY HAND! **
    ####################################################################
    # ------------------------------------------------------------------

    point1 = rectangle._upper_left_corner
    point2 = rectangle._lower_right_corner
    fill_color = rectangle.fill_color
    outline = rectangle.outline_color
    thick = rectangle.outline_thickness
    rectangle = rg.Rectangle(point1, point2)
    rectangle.fill_color = fill_color
    rectangle.outline_color = outline
    rectangle.outline_thickness = thick
    rectangle.attach_to(window)

    # Horizontal Circles
    radius = (point2.y - point1.y) / 2
    x = point1.x - radius
    for k in range(m):
        y = (point1.y + point2.y) / 2
        center = rg.Point(x, y)
        circle = rg.Circle(center, radius)
        circle.fill_color = fill_color
        circle.attach_to(window)
        x = x - 2 * radius

    # Vertical Circles
    radius = (point2.x - point1.x) / 2
    y = point1.y - radius
    for k in range(n):
        x = (point1.x + point2.x) / 2
        center = rg.Point(x, y)
        circle = rg.Circle(center, radius)
        circle.outline_color = outline
        circle.attach_to(window)
        y = y - 2 * radius

    # ------------------------------------------------------------------
    # render: Draw ALL the objects attached to this window.
    # ------------------------------------------------------------------
    window.render()
Example #9
0
def circle_and_rectangle():
    """
    -- Constructs an rg.RoseWindow.
    -- Constructs and draws a rg.Circle and rg.Rectangle
       on the window such that:
          -- They fit in the window and are easily visible.
          -- The rg.Circle is filled with 'blue'
    -- Prints (on the console, on SEPARATE lines) the following data
         associated with your rg.Circle:
          -- Its outline thickness.
          -- Its fill color.
          -- Its center.
          -- Its center's x coordinate.
          -- Its center's y coordinate.
    -- Prints (on the console, on SEPARATE lines) the same data
         but for your rg.Rectangle.
    -- Waits for the user to press the mouse, then closes the window.

    Here is an example of the output on the console,
    for one particular circle and rectangle:
           1
           blue
           Point(180.0, 115.0)
           180
           115
           1
           None
           Point(75.0, 150.0)
           75.0
           150.0
    """
    # -------------------------------------------------------------------------
    # DONE: 3. Implement this function, per its green doc-string above.
    #   -- ANY objects that meet the criteria are fine.
    # Put a statement in   main   to test this function
    #    (by calling this function).
    #
    # IMPORTANT: Use the DOT TRICK to guess the names of the relevant
    #       instance variables for outline thickness, etc.
    # -------------------------------------------------------------------------
    window = rg.RoseWindow(400, 400)
    circle = rg.Circle(rg.Point(50, 50), 10)
    color = 'red'
    circle.fill_color = color
    print('circle color :', color)
    thick = 5
    circle.outline_thickness = thick
    print('thickness:', thick)
    x_coor = 50
    y_coor = 50
    print('center point :', x_coor, ',', y_coor)
    circle.center = rg.Point(x_coor, y_coor)
    print("x coorinate of circle :", x_coor)
    print('y coordinate of circle :', y_coor)
    circle.attach_to(window)

    print(' ')

    rec = rg.Rectangle(rg.Point(100, 100), rg.Point(200, 20))
    color2 = 'blue'
    rec.fill_color = color2
    print('rectangle color:', color2)
    thick_rec = 10
    rec.outline_thickness = thick_rec
    print('thickness of rec', thick_rec)
    x = 100 + (50 / 2)
    y = 20 + (80 / 2)
    print('center point rec', x, ',', y)
    print('x coor. center of rec:', x)
    print('y coor. center of rec :', y)
    rec.attach_to(window)

    window.render()
    window.close_on_mouse_click()
def circle_and_rectangle():
    """
    -- Constructs an rg.RoseWindow.
    -- Constructs and draws a rg.Circle and rg.Rectangle
       on the window such that:
          -- They fit in the window and are easily visible.
          -- The rg.Circle is filled with 'blue'
    -- Prints (on the console, on SEPARATE lines) the following data
         associated with your rg.Circle:
          -- Its outline thickness.
          -- Its fill color.
          -- Its center.
          -- Its center's x coordinate.
          -- Its center's y coordinate.
    -- Prints (on the console, on SEPARATE lines) the same data
         but for your rg.Rectangle.
    -- Waits for the user to press the mouse, then closes the window.

    Here is an example of the output on the console,
    for one particular circle and rectangle:
           1
           blue
           Point(180.0, 115.0)
           180
           115
           1
           None
           Point(75.0, 150.0)
           75.0
           150.0
    """
    # -------------------------------------------------------------------------
    # Done: 3. Implement this function, per its green doc-string above.
    #   -- ANY objects that meet the criteria are fine.
    # Put a statement in   main   to test this function
    #    (by calling this function).
    #
    # IMPORTANT: Use the DOT TRICK to guess the names of the relevant
    #       instance variables for outline thickness, etc.
    # -------------------------------------------------------------------------
    window = rg.RoseWindow(400, 400)
    circlecenter = rg.Point(100,100)
    circleradius = 10
    circle = rg.Circle(circlecenter, circleradius)
    circle.fill_color = "blue"
    circle.attach_to(window)
    print(1)
    print("blue")
    print(circlecenter)
    print(100)
    print(100)

    rectanglebotleft = rg.Point(350, 200)
    rectangletopright = rg.Point(375, 150)
    rectangle = rg.Rectangle(rectanglebotleft,rectangletopright)
    rectangle.attach_to(window)
    print(1)
    print("None")
    print("Point(363.0, 175.0)")
    print(363)
    print(175)

    window.render()
    window.close_on_mouse_click()
Example #11
0
def circle_and_rectangle():
    """
    -- Constructs an rg.RoseWindow.
    -- Constructs and draws a rg.Circle and rg.Rectangle
       on the window such that:
          -- They fit in the window and are easily visible.
          -- The rg.Circle is filled with 'blue'
    -- Prints (on the console, on SEPARATE lines) the following data
         associated with your rg.Circle:
          -- Its outline thickness.
          -- Its fill color.
          -- Its center.
          -- Its center's x coordinate.
          -- Its center's y coordinate.
    -- Prints (on the console, on SEPARATE lines) the same data
         but for your rg.Rectangle.
    -- Waits for the user to press the mouse, then closes the window.

    Here is an example of the output on the console,
    for one particular circle and rectangle:
           1
           blue
           Point(180.0, 115.0)
           180
           115
           1
           None
           Point(75.0, 150.0)
           75.0
           150.0
    """
    window = rg.RoseWindow()

    # Sets circle Instance Variables
    center = rg.Point(180, 115)
    radius1 = 20

    # Sets rectangle Instance Variables
    corner1 = rg.Point(70, 50)
    corner2 = rg.Point(300, 200)

    # Constructs and attaches both objects to window
    # Fills circle
    circle = rg.Circle(center, radius1)
    circle.fill_color = 'blue'
    circle.attach_to(window)
    rectangle = rg.Rectangle(corner1, corner2)
    rectangle.attach_to(window)

    # prints circle information
    print(circle.outline_thickness)
    print(circle.fill_color)
    print(circle.center)
    print(circle.center.x)  # X coordinate
    print(circle.center.y)  # Y coordinate

    # prints rectangle information
    print(rectangle.outline_thickness)
    print(rectangle.fill_color)
    print(rectangle.get_center())
    print(rectangle.get_center().x)  # X coordinate
    print(rectangle.get_center().y)  # Y coordinate

    window.render()
    window.close_on_mouse_click()
Example #12
0
def circle_and_rectangle():
    """
    -- Constructs an rg.RoseWindow.
    -- Constructs and draws a rg.Circle and rg.Rectangle
       on the window such that:
          -- They fit in the window and are easily visible.
          -- The rg.Circle is filled with 'blue'
    -- Prints (on the console, on SEPARATE lines) the following data
         associated with your rg.Circle:
          -- Its outline thickness.
          -- Its fill color.
          -- Its center.
          -- Its center's x coordinate.
          -- Its center's y coordinate.
    -- Prints (on the console, on SEPARATE lines) the same data
         but for your rg.Rectangle.
    -- Waits for the user to press the mouse, then closes the window.

    Here is an example of the output on the console,
    for one particular circle and rectangle:
           1
           blue
           Point(180.0, 115.0)
           180
           115
           1
           None
           Point(75.0, 150.0)
           75.0
           150.0
    """
    # -------------------------------------------------------------------------
    # DONE: 3. Implement this function, per its green doc-string above.
    #   -- ANY objects that meet the criteria are fine.
    # Put a statement in   main   to test this function
    #    (by calling this function).
    #
    # IMPORTANT: Use the DOT TRICK to guess the names of the relevant
    #       instance variables for outline thickness, etc.
    # -------------------------------------------------------------------------

    window2 = rg.RoseWindow()

    gordan = rg.Circle(rg.Point(145, 79), 5)
    gordan.fill_color = 'blue'
    gordan.attach_to(window2)

    garfield = rg.Rectangle(rg.Point(132, 45), rg.Point(76, 184))
    garfield.attach_to(window2)

    window2.render()
    window2.close_on_mouse_click()

    print('The thickness of the circle is:', gordan.outline_thickness)
    print('The fill color is', gordan.outline_color)
    print('The Point is', gordan.center)
    print('X coordinate is', gordan.center.x)
    print('Y coordinate is', gordan.center.y)
    print('The thickness of the square is', garfield.outline_thickness)
    print('The fill color is', garfield.fill_color)
    print('The Point is', garfield.corner_1)
    print('X coordinate', garfield.corner_1.x)
    print('Y coordinate', garfield.corner_2.y)
def circle_and_rectangle(x, y, r, a, b, c, d):
    """
    -- Constructs an rg.RoseWindow.
    -- Constructs and draws a rg.Circle and rg.Rectangle
       on the window such that:
          -- They fit in the window and are easily visible.
          -- The rg.Circle is filled with 'blue'
    -- Prints (on the console, on SEPARATE lines) the following data
         associated with your rg.Circle:
          -- Its outline thickness.
          -- Its fill color.
          -- Its center.
          -- Its center's x coordinate.
          -- Its center's y coordinate.
    -- Prints (on the console, on SEPARATE lines) the same data
         but for your rg.Rectangle.
    -- Waits for the user to press the mouse, then closes the window.

    Here is an example of the output on the console,
    for one particular circle and rectangle:
           1
           blue
           Point(180.0, 115.0)
           180
           115
           1
           None
           Point(75.0, 150.0)
           75.0
           150.0
    """
    # -------------------------------------------------------------------------
    # DONE: 3. Implement this function, per its green doc-string above.
    #   -- ANY objects that meet the criteria are fine.
    # Put a statement in   main   to test this function
    #    (by calling this function).
    #
    # IMPORTANT: Use the DOT TRICK to guess the names of the relevant
    #       instance variables for outline thickness, etc.
    # -------------------------------------------------------------------------
    window = rg.RoseWindow()
    p = rg.Point(x, y)
    circle = rg.Circle(p, r)
    circle.attach_to(window)
    circle.fill_color = 'pink'
    circle.outline_thickness = 4
    print(circle.outline_thickness)
    print(circle.fill_color)
    print(p)
    print(x)
    print(y)

    e = rg.Point(a, b)
    f = rg.Point(c, d)
    rec = rg.Rectangle(e, f)
    rec.outline_thickness = 5
    rec.attach_to(window)
    print(rec.outline_thickness)
    print(rec.fill_color)
    print(e)
    print(c - a)
    print(b - d)
    window.render()
    window.close_on_mouse_click()
Example #14
0
def many_hourglasses(window, square, m, colors):
    """
    See   many_hourglasses_picture.pdf   in this project for pictures that may
    help you better understand the following specification:

    Displays  m  rectangles, where:
      -- Each rectangle has an hourglass of circles inside it,
           per the  hourglass  function above.
      -- The circles in the hourglasses are all the same size.
      -- The leftmost rectangle is the given square, and it contains
           an hourglass with a single circle that fills the square.
      -- Each successive rectangle is immediately to the right of the
           previous rectangle, and each contains an hourglass with
           the hourglass'  n   being one greater than the  n  used
           for the previous rectangle.
      -- The colors for the hourglass figures use the given sequence of
           colors, "wrapping" if m exceeds the length of the sequence.

    Preconditions:
      :type window: rg.RoseWindow
      :type square: rg.Square
      :type m: int
      :type colors: (list | tuple) of str
    where m is positive and colors is a sequence of strings,
    each of which denotes a color that rosegraphics understands.
    """
    # -------------------------------------------------------------------------
    # DONE: 3. Implement and test this function.
    #       We provided some tests for you (above).
    # -------------------------------------------------------------------------
    ###########################################################################
    # IMPORTANT:
    #   1. Partial credit if you draw JUST the rectangles.
    #   2. No additional credit unless you CALL the  hourglass  function
    #        in the PREVIOUS problem appropriately
    #        to draw the hourglass figures.
    ###########################################################################
    # -------------------------------------------------------------------------
    # DIFFICULTY AND TIME RATINGS (see top of this file for explanation)
    #    DIFFICULTY:      7  (assuming that you already have
    #                         a correct "hourglass" function above)
    #    TIME ESTIMATE:  20 minutes (warning: this problem is challenging)
    # -------------------------------------------------------------------------
    #square.attach_to(window)
    radius = square.length_of_each_side / 2
    x = square.center.x
    y = square.center.y
    count = 0
    for k in range(m):
        if k % len(colors) == 0:
            count = count + 1
        newk = k - len(colors) * count + len(colors)
        #print(len(colors),k,newk)
        blc = rg.Point(x - radius * (k + 1),
                       y + radius * (math.sqrt(3) * k + 1))
        trc = rg.Point(x + radius * (k + 1),
                       y - radius * (math.sqrt(3) * k + 1))
        rectangle = rg.Rectangle(blc, trc)
        rectangle.attach_to(window)
        hourglass(window, k + 1, rg.Point(x, square.center.y), radius,
                  colors[newk])
        x = x + radius * (k + 1) + radius * (k + 2)
Example #15
0
def many_hourglasses(window, square, m, colors):
    """
    See   many_hourglasses_picture.pdf   in this project for pictures that may
    help you better understand the following specification:

    Displays  m  rectangles, where:
      -- Each rectangle has an hourglass of circles inside it,
           per the  hourglass  function above.
      -- The circles in the hourglasses are all the same size.
      -- The leftmost rectangle is the given square, and it contains
           an hourglass with a single circle that fills the square.
      -- Each successive rectangle is immediately to the right of the
           previous rectangle, and each contains an hourglass with
           the hourglass'  n   being one greater than the  n  used
           for the previous rectangle.
      -- The colors for the hourglass figures use the given sequence of
           colors, "wrapping" if m exceeds the length of the sequence.

    Preconditions:
      :type window: rg.RoseWindow
      :type square: rg.Square
      :type m: int
      :type colors: (list | tuple) of str
    where m is positive and colors is a sequence of strings,
    each of which denotes a color that rosegraphics understands.
    """
    # ------------------------------------------------------------------
    # DONE: 3. Implement and test this function.
    #       We provided some tests for you (above).
    # ------------------------------------------------------------------
    ####################################################################
    # IMPORTANT:
    #   1. Partial credit if you draw JUST the rectangles.
    #   2. No additional credit unless you CALL the  hourglass  function
    #        in the PREVIOUS problem appropriately
    #        to draw the hourglass figures.
    ####################################################################
    # ------------------------------------------------------------------
    # DIFFICULTY AND TIME RATINGS (see top of this file for explanation)
    #    DIFFICULTY:      7  (assuming that you already have
    #                         a correct "hourglass" function above)
    #    TIME ESTIMATE:  20 minutes (warning: this problem is challenging)
    # ------------------------------------------------------------------
    radius = square.length_of_each_side / 2
    wrap = 0
    center = square.center
    upper_left = rg.Point(center.x - radius, center.y - radius)
    lower_right = rg.Point(center.x + radius, center.y + radius)
    for k in range(m):
        if k < len(colors):
            hourglass(window, k + 1, center, radius, colors[k])
        else:
            if wrap < len(colors):
                hourglass(window, k + 1, center, radius, colors[wrap])
                wrap = wrap + 1
            else:
                wrap = 0
                hourglass(window, k + 1, center, radius, colors[wrap])
                wrap = wrap + 1
        rectangle = rg.Rectangle(upper_left, lower_right)
        rectangle.attach_to(window)
        window.render()
        center.x = center.x + radius + ((k + 1) * 4 * radius * math.sin(0.524))
        upper_left.x = upper_left.x + (2 * radius * (k + 1))
        lower_right.x = lower_right.x + (2 * radius) + (
            (k + 1) * 4 * radius * math.sin(0.524))
        upper_left.y = upper_left.y - (2 * radius * math.cos(0.524))
        lower_right.y = lower_right.y + (2 * radius * math.cos(0.524))
def circle_and_rectangle():
    """
    -- Constructs an rg.RoseWindow.
    -- Constructs and draws a rg.Circle and rg.Rectangle
       on the window such that:
          -- They fit in the window and are easily visible.
          -- The rg.Circle is filled with 'blue'
    -- Prints (on the console, on SEPARATE lines) the following data
         associated with your rg.Circle:
          -- Its outline thickness.
          -- Its fill color.
          -- Its center.
          -- Its center's x coordinate.
          -- Its center's y coordinate.
    -- Prints (on the console, on SEPARATE lines) the same data
         but for your rg.Rectangle.
    -- Waits for the user to press the mouse, then closes the window.

    Here is an example of the output on the console,
    for one particular circle and rectangle:
           1
           blue
           Point(180.0, 115.0)
           180
           115
           1
           None
           Point(75.0, 150.0)
           75.0
           150.0
    """
    window = rg.RoseWindow(800, 600)
    x1 = 200
    y1 = 200
    centerpoint1 = rg.Point(x1, y1)
    radius = 100
    x2 = 300
    y2 = 300
    x3 = 500
    y3 = 400
    cornerpoint1 = rg.Point(x2, y2)
    cornerpoint2 = rg.Point(x3, y3)
    centerpoint2 = rg.Point(x3 - x2, y3 - y2)
    circle = rg.Circle(centerpoint1, radius)
    circle.fill_color = 'blue'
    rectangle = rg.Rectangle(cornerpoint1, cornerpoint2)
    circle.attach_to(window)
    rectangle.attach_to(window)

    print(circle.outline_thickness)
    print(circle.fill_color)
    print(centerpoint1)
    print(x1)
    print(y1)

    print(rectangle.outline_thickness)
    print(centerpoint2)
    print(x3 - x2)
    print(y3 - y2)

    window.render()
    window.close_on_mouse_click()
def run_test_draw_circles_from_rectangle():
    """ Tests the   draw_circles_from_rectangle  function. """
    print()
    print('--------------------------------------------------')
    print('Testing the  draw_circles_from_rectangle  function:')
    print('  See the graphics windows that pop up.')
    print('--------------------------------------------------')

    # ------------------------------------------------------------------
    # done: 3. Implement this TEST function.
    #   It TESTS the  draw_circles_from_rectangle  function
    #   defined below.  Include at least **   3   ** tests, of which
    #      ***  at least TWO tests are on ONE window and
    #      ***  at least ONE test is on a DIFFERENT window.
    #
    ####################################################################
    # HINT: C onsider using the same test cases as suggested by the
    #   pictures in  draw_circles_from_rectangle.pdf   in this project.
    #   Follow the same form as the example in a previous problem.
    ####################################################################
    # ------------------------------------------------------------------

    # ------------------------------------------------------------------
    # TWO tests on ONE window.
    # ------------------------------------------------------------------
    window1 = rg.RoseWindow(720, 500)

    # Test 1:
    point1 = rg.Point(400, 250)
    point2 = rg.Point(440, 325)
    rectangle = rg.Rectangle(point1, point2)
    rectangle.fill_color = 'green'
    rectangle.outline_color = 'black'
    rectangle.outline_thickness = 5
    draw_circles_from_rectangle(4, 5, rectangle, window1)

    # Test 2:
    point1 = rg.Point(500, 450)
    point2 = rg.Point(600, 400)
    rectangle = rg.Rectangle(point1, point2)
    rectangle.fill_color = 'blue'
    rectangle.outline_color = 'red'
    rectangle.outline_thickness = 3
    draw_circles_from_rectangle(8, 3, rectangle, window1)
    window1.close_on_mouse_click()

    # ------------------------------------------------------------------
    # A third test on ANOTHER window.
    # ------------------------------------------------------------------
    window2 = rg.RoseWindow(620, 380)

    # Test 3:
    point1 = rg.Point(350, 280)
    point2 = rg.Point(375, 330)
    rectangle = rg.Rectangle(point1, point2)
    rectangle.fill_color = 'yellow'
    rectangle.outline_color = 'red'
    rectangle.outline_thickness = 5
    draw_circles_from_rectangle(6, 10, rectangle, window2)

    window2.close_on_mouse_click()
Example #18
0
        # Upper Row
        go_along_row(ring_size, row_start_x, row_start_y, width,
                     colors[k % len(colors)], window)

        # Left column
        go_along_column(ring_size, row_start_x, row_start_y, width,
                        colors[k % len(colors)], window)

        # Lower Row
        lower_x = row_start_x
        lower_y = row_start_y + width * (ring_size - 1)
        go_along_row(ring_size, lower_x, lower_y, width,
                     colors[k % len(colors)], window)

        # Right column
        upper_right_x = row_start_x + width * (ring_size - 1)
        upper_right_y = row_start_y
        go_along_column(ring_size, upper_right_x, upper_right_y, width,
                        colors[k % len(colors)], window)

        # after we drew the row...
        row_start_x = row_start_x - width
        row_start_y = row_start_y - width


window1 = rg.RoseWindow(1600, 900, "squares 1")
squares(rg.Rectangle(rg.Point(450, 450), rg.Point(475, 475)), 15,
        ['cyan', 'pink', 'blue'], window1)
window1.close_on_mouse_click()
def draw_lines_from_rectangles(rectangle1, rectangle2, n, window):
    """
    What comes in:  Four arguments:
      -- Two rg.Rectangles.
      -- A positive integer n.
      -- An rg.RoseWindow.
    What goes out:  Nothing (i.e., None).
    Side effects:
      See   draw_lines_from_rectangles.pdf   in this project
      for pictures that may help you better understand
      the following specification:

      First draws the given rg.Rectangles on the given rg.RoseWindow.
      Then draws  n  rg.Lines on the given rg.RoseWindow, such that:
        -- The 1st rg.Line goes from the center of one of the
             1st rg.Rectangle to the center of the 2nd rg.Rectangle.
        -- The 2nd rg.Line goes from the lower-left corner of the
              1st rg.Rectangle and is parallel to the 1st rg.Line,
              with the same length and direction as the 1st rg.Line.
        -- Subsequent rg.Lines are shifted from the previous rg.Line in
              the same way that the 2nd rg.Line is shifted from the 1st.
        -- Each of the rg.Lines has thickness 5.
        -- The colors of the rg.Lines alternate, as follows:
             - The 1st, 3rd, 5th, ... rg.Line has color R1_color
             - The 2nd, 4th, 6th, ... rg.Line has color R2_color
            where
             - R1_color is the outline color of the 1st rg.Rectangle
             - R2_color is the outline color of the 2nd rg.Rectangle
      Must  ** render **     but   ** NOT close **   the window.

    Type hints:
      :type rectangle1: rg.Rectangle
      :type rectangle2: rg.Rectangle
      :type n: int
      :type window: rg.RoseWindow
      """
    # ------------------------------------------------------------------
    # TODO: 5. Implement and test this function.
    #          Tests have been written for you (above).
    #
    # C ONSIDER using the ACCUMULATOR IN GRAPHICS pattern,
    #             as in   draw_row_of_circles   in m1e,
    #             instead of directly using the loop variable.
    #
    ####################################################################
    # HINT: To figure out the code that computes the necessary
    #       endpoints for each line,
    #          ** FIRST DO A CONCRETE EXAMPLE BY HAND! **
    ####################################################################
    # ------------------------------------------------------------------

    # Draw First Rectangle
    point1 = rectangle1._upper_left_corner
    point2 = rectangle1._lower_right_corner
    outline1 = rectangle1.outline_color
    thick = rectangle1.outline_thickness
    rectangle = rg.Rectangle(point1, point2)
    rectangle.outline_color = outline1
    rectangle.outline_thickness = thick
    rectangle.attach_to(window)

    centerx = (point1.x + point2.x) / 2
    centery = (point1.y + point2.y) / 2
    center1 = rg.Point(centerx, centery)

    # Draw Second Rectangle
    point1 = rectangle2._upper_left_corner
    point2 = rectangle2._lower_right_corner
    outline2 = rectangle2.outline_color
    thick = rectangle2.outline_thickness
    rectangle = rg.Rectangle(point1, point2)
    rectangle.outline_color = outline2
    rectangle.outline_thickness = thick
    rectangle.attach_to(window)

    centerx = (point1.x + point2.x) / 2
    centery = (point1.y + point2.y) / 2
    center2 = rg.Point(centerx, centery)

    # Draw  Lines
    count = 1
    point1 = center1
    point2 = center2
    for _ in range(n):
        line = rg.Line(point1, point2)
        line.thickness = 3

        if count == 1:
            line.color = outline1
            line.attach_to(window)
            count = count - 1
        else:
            line.color = outline2
            line.attach_to(window)
            count = count + 1

        xdis = (rectangle1._lower_right_corner.x - \
               rectangle1._upper_left_corner.x) / 2
        ydis = (rectangle1._lower_right_corner.y - \
                rectangle1._upper_left_corner.y) / 2
        point1 = rg.Point(point1.x - xdis, point1.y + ydis)
        point2 = rg.Point(point2.x - xdis, point2.y + ydis)

    # ------------------------------------------------------------------
    # render: Draw ALL the objects attached to this window.
    # ------------------------------------------------------------------
    window.render()
Example #20
0
def circle_and_rectangle():
    """
    -- Constructs an rg.RoseWindow.
    -- Constructs and draws a rg.Circle and rg.Rectangle
       on the window such that:
          -- They fit in the window and are easily visible.
          -- The rg.Circle is filled with 'blue'
    -- Prints (on the console, on SEPARATE lines) the following data
         associated with your rg.Circle:
          -- Its outline thickness.
          -- Its fill color.
          -- Its center.
          -- Its center's x coordinate.
          -- Its center's y coordinate.
    -- Prints (on the console, on SEPARATE lines) the same data
         but for your rg.Rectangle.
    -- Waits for the user to press the mouse, then closes the window.

    Here is an example of the output on the console,
    for one particular circle and rectangle:
           1
           blue
           Point(180.0, 115.0)
           180
           115
           1
           None
           Point(75.0, 150.0)
           75.0
           150.0
    """
    # -------------------------------------------------------------------------
    # DONE: 3. Implement this function, per its green doc-string above.
    #   -- ANY objects that meet the criteria are fine.
    # Put a statement in   main   to test this function
    #    (by calling this function).
    #
    # IMPORTANT: Use the DOT TRICK to guess the names of the relevant
    #       instance variables for outline thickness, etc.
    # -------------------------------------------------------------------------
    window = rg.RoseWindow()
    x = 200
    y = 150
    color = 'blue'
    ccenter = rg.Point(x, y)
    point = rg.Point(x, y)
    radius = 50
    circle = rg.Circle(point, radius)
    circle.outline_thickness = 1
    circle.fill_color = color
    circle.attach_to(window)
    point1 = rg.Point(50, 50)
    point2 = rg.Point(100, 100)
    z = 75
    w = 75
    rcenter = rg.Point(z, w)
    rectangle = rg.Rectangle(point1, point2)
    rectangle.outline_thickness = 1
    rectangle.attach_to(window)
    window.render()
    window.close_on_mouse_click()
    print('1')
    print(color)
    print(ccenter)
    print(x)
    print(y)
    print('1')
    print('None')
    print(rcenter)
    print(z)
    print(w)
Example #21
0
def circle_and_rectangle():
    """
    -- Constructs an rg.RoseWindow.
    -- Constructs and draws a rg.Circle and rg.Rectangle
       on the window such that:
          -- They fit in the window and are easily visible.
          -- The rg.Circle is filled with 'blue'
    -- Prints (on the console, on SEPARATE lines) the following data
         associated with your rg.Circle:
          -- Its outline thickness.
          -- Its fill color.
          -- Its center.
          -- Its center's x coordinate.
          -- Its center's y coordinate.
    -- Prints (on the console, on SEPARATE lines) the same data
         but for your rg.Rectangle.
    -- Waits for the user to press the mouse, then closes the window.

    Here is an example of the output on the console,
    for one particular circle and rectangle:
           1
           blue
           Point(180.0, 115.0)
           180
           115
           1
           None
           Point(75.0, 150.0)
           75.0
           150.0
    """
    # -------------------------------------------------------------------------
    # DONE: 3. Implement this function, per its green doc-string above.
    #   -- ANY objects that meet the criteria are fine.
    # Put a statement in   main   to test this function
    #    (by calling this function).
    #
    # IMPORTANT: Use the DOT TRICK to guess the names of the relevant
    #       instance variables for outline thickness, etc.
    # -------------------------------------------------------------------------
    window = rg.RoseWindow()
    cthick = 2
    cx = 100
    cy = 100
    ccolor = 'blue'
    ccenter = rg.Point(cx, cy)
    rthick = 4
    rx1 = 150
    ry1 = 150
    rx2 = 300
    ry2 = 250
    rcenterx = math.fabs(rx1 + rx2) / 2
    rcentery = math.fabs(ry1 + ry2) / 2
    rcenter = rg.Point(rcenterx, rcentery)
    rcolor = 'red'

    print(
        "--------------------------------------------------------------------------"
    )
    print("Circle Variables")
    print(
        "--------------------------------------------------------------------------"
    )
    print("Outline Thickness:", cthick)
    print("Fill Color:", ccolor)
    print("Center:", ccenter)
    print("Center's x Coordinate:", cx)
    print("Center's y Coordinate:", cy)
    print(
        "--------------------------------------------------------------------------"
    )
    print("Rectangle Variables")
    print(
        "--------------------------------------------------------------------------"
    )
    print("Outline Thickness:", rthick)
    print("Fill Color:", rcolor)
    print("Center:", rcenter)
    print("Center's x Coordinate:", rcenterx)
    print("Center's y Coordinate:", rcentery)

    circle = rg.Circle(ccenter, 50)
    circle.fill_color = ccolor
    circle.outline_thickness = cthick
    rectangle = rg.Rectangle(rg.Point(rx1, ry1), rg.Point(rx2, ry2))
    rectangle.outline_thickness = rthick
    rectangle.fill_color = rcolor

    circle.attach_to(window)
    rectangle.attach_to(window)
    window.render()
    window.close_on_mouse_click()
Example #22
0
# Author: Jaclyn Setina

import rosegraphics as rg

window = rg.RoseWindow(700, 400)

center = rg.Point(100, 50)
circle = rg.Circle(center, 40)
circle.fill_color = 'red'
circle.attach_to(window)

corner_1 = rg.Point(100, 150)
corner_2 = rg.Point(300, 250)
rectangle = rg.Rectangle(corner_1, corner_2)
rectangle.outline_color = 'blue'
rectangle.attach_to(window)

window.render()
window.continue_on_mouse_click()

start_point = rg.Point(300, 150)
end_point = rg.Point(100, 250)
line = rg.Line(start_point, end_point)
line.arrow = 'last'
line.attach_to(window)

window.render()
window.continue_on_mouse_click()

circle.fill_color = 'blue'
circle.attach_to(window)
Example #23
0
def problem2b(rect, n, delta, win):
    """
    See   problem2b_picture.pdf   in this project for pictures
    that may help you better understand the following specification:
    
    What comes in:
      -- An rg.Rectangle.
      -- A positive integer n.
      -- A positive integer delta.
      -- An rg.RoseWindow.
    What goes out:  Nothing (i.e., None).
    Side effects:
      Draws n rg.Rectangles on the given rg.RoseWindow such that:
        -- The first rg.Rectangle is the given one.
        -- Subsequent rg.Rectangles have the same center
            as the given rg.Rectangle, but their width
            and height are each   (2 * delta)   greater than
            the width and height of the previous rg.Rectangle.
            That is, the distance from each line of each rg.Rectangle
            to the corresponding line of the rg.Rectangle next to it
            is delta.  (See problem2b_picture.)
      Must render but   ** NOT close **   the window.

    Type hints:
      :type rect:   rg.Rectangle
      :type n:      int
      :type delta:  int
      :type win:    rg.RoseWindow
    """
    # -------------------------------------------------------------------------
    # D: 3. Implement and test this function.
    #          Tests have been written for you (above).
    # -------------------------------------------------------------------------
    # -------------------------------------------------------------------------
    # DIFFICULTY AND TIME RATINGS (see top of this file for explanation)
    #    DIFFICULTY:      7
    #    TIME ESTIMATE:   15 to 25 minutes.
    # -------------------------------------------------------------------------
    rect.attach_to(win)

    center = rect.get_center()
    height = rect.get_height()
    width = rect.get_width()
    cor1x = (center.x - (width / 2))
    cor1y = (center.y - (height / 2))

    cor2x = (center.x + (width / 2))
    cor2y = (center.y + (height / 2))

    for k in range(n - 1):
        cor1x = cor1x - delta
        cor1y = cor1y - delta
        cor1 = rg.Point(cor1x, cor1y)

        cor2x = cor2x + delta
        cor2y = cor2y + delta
        cor2 = rg.Point(cor2x, cor2y)

        rectangle = rg.Rectangle(cor1, cor2)
        rectangle.attach_to(win)

    win.render()
Example #24
0
def circle_and_rectangle(center_x, center_y, radius, rectangle_corner1_x,
                         rectangle_corner1_y, rectangle_corner2_x,
                         rectangle_corner2_y):
    """
    -- Constructs an rg.RoseWindow.
    -- Constructs and draws a rg.Circle and rg.Rectangle
       on the window such that:
          -- They fit in the window and are easily visible.
          -- The rg.Circle is filled with 'blue'
    -- Prints (on the console, on SEPARATE lines) the following data
         associated with your rg.Circle:
          -- Its outline thickness.
          -- Its fill color.
          -- Its center.
          -- Its center's x coordinate.
          -- Its center's y coordinate.
    -- Prints (on the console, on SEPARATE lines) the same data
         but for your rg.Rectangle.
    -- Waits for the user to press the mouse, then closes the window.

    Here is an example of the output on the console,
    for one particular circle and rectangle:
           1
           blue
           Point(180.0, 115.0)
           180
           115
           1
           None
           Point(75.0, 150.0)
           75.0
           150.0
    """
    # -------------------------------------------------------------------------
    # DONE: 3. Implement this function, per its green doc-string above.
    #   -- ANY objects that meet the criteria are fine.
    # Put a statement in   main   to test this function
    #    (by calling this function).
    #
    # IMPORTANT: Use the DOT TRICK to guess the names of the relevant
    #       instance variables for outline thickness, etc.
    # -------------------------------------------------------------------------
    print('CIRCLE Parts')

    circle = rg.Circle(rg.Point(center_x, center_y), radius)
    circle.fill_color = ('blue')
    circle.attach_to(window1)
    print(circle.center)
    print(center_x)
    print(center_y)
    print('blue')
    circle.outline_thickness = 1
    print(circle.outline_thickness)

    print('RECTANGLE Parts')
    rectangle_corner1 = rg.Point(rectangle_corner1_x, rectangle_corner1_y)
    rectangle_corner2 = rg.Point(rectangle_corner2_x, rectangle_corner2_y)

    rectangle = rg.Rectangle(rectangle_corner1, rectangle_corner2)
    rectangle.attach_to(window1)
    print(rectangle.outline_thickness)
    print(rectangle.fill_color)
    print(rectangle.get_center())
    print(rectangle.get_center().x)
    print(rectangle.get_center().y)

    window1.render()
    return
def shapes():
    print("rg.Point.x=5 should give an error")
    try:
        rg.Point.x = 5
    except:
        print("Got the error!")
        traceback.print_exc()
        # raise

    # Demonstrates/tests the __repr__ methods:
    point1 = rg.Point(100, 200)
    point2 = rg.Point(300, 400)
    circle = rg.Circle(point1, 50)
    rectangle = rg.Rectangle(point1, point2)
    square = rg.Square(point1, 40)
    line = rg.Line(point1, point2)
    for shape in (point1, point2, circle, rectangle, square, line):
        print(shape)

    for shape in (circle, rectangle, square):
        shape.fill_color = 'red'
        shape.outline_color = 'blue'
        shape.outline_thickness = 5
    line.color = 'green'
    line.thickness = 10

    for shape in (point1, point2, circle, rectangle, square, line):
        print(shape)

    w = rg.RoseWindow(500, 300, 'hello')
    w.close_on_mouse_click()
    window1 = rg.RoseWindow(title='An empty window', make_initial_canvas=False)

    window1.close_on_mouse_click()

    window2 = rg.RoseWindow(
        500,
        300,
        'Blue window with yellow canvas',
        # window_color='blue', # Mark: I assume most students
        # won't use this.
        canvas_color='yellow')

    center = rg.Point(300, 100)
    circle = rg.Circle(center, 40)
    circle.attach_to(window2.initial_canvas)
    circle.fill_color = 'red'
    window2.render(1)
    circle.fill_color = ''
    print("Emptied the fill color.")
    print(window2.width, window2.height)
    window2.render()
    window2.get_next_mouse_click()

    center.move_by(-200, -50)
    circle = rg.Circle(center, 70)
    circle.attach_to(window2.initial_canvas)
    circle.fill_color = None

    print(window2.width, window2.height)
    window2.render()
    window2.close_on_mouse_click()

    window3 = rg.RoseWindow()

    p1 = rg.Point(100, 50)
    p2 = rg.Point(200, 90)

    rect = rg.Rectangle(p1, p2)
    # rect.attach_to(window2.initial_canvas) # DCM: Two windows open,
    # confusing.
    rect.attach_to(window3.initial_canvas)

    window3.render(1)

    rect.fill_color = 'red'
    center.attach_to(window3.initial_canvas)
    window3.render(1)

    center.move_by(50, 0)
    window3.render()

    window3.close_on_mouse_click()
Example #26
0
def problem2a(circle, rectangle, window):
    """
    See   problem2a_picture.pdf   in this project for pictures
    that may help you better understand the following specification:
    
    What comes in:
      -- An rg.Circle.
      -- An rg.Rectangle.
      -- An rg.RoseWindow.
    What goes out:  Nothing (i.e., None).
    Side effects:
      -- Draws the given rg.Circle and rg.Rectangle
           on the given rg.RoseWindow,
           then waits for the user to click the window.
      -- Then draws an rg.Line from the upper-right corner
           of the rg.Rectangle to its lower-left corner,
           with the line drawn as an arrow,
           then waits for the user to click the window.
      -- Changes the fill color of the given rg.Circle to the
           outline color of the given rg.Rectangle,
           then renders the window again
           (with no waiting for a click from the user this time).
      Must  ** NOT close **   the window.

    Type hints:
      :type circle:    rg.Circle
      :type rectangle: rg.Rectangle
      :type window:    rg.RoseWindow
    """
    # -------------------------------------------------------------------------
    # DONE: 2. Implement and test this function.
    #          Tests have been written for you (above).
    # -------------------------------------------------------------------------
    # -------------------------------------------------------------------------
    # DIFFICULTY AND TIME RATINGS (see top of this file for explanation)
    #    DIFFICULTY:      6
    #    TIME ESTIMATE:   10 to 15 minutes.
    # -------------------------------------------------------------------------
    r = circle.radius
    x1 = circle.center.x
    y1 = circle.center.y
    start_x = rectangle.get_upper_left_corner().x
    start_y = rectangle.get_upper_left_corner().y
    end_y = rectangle.get_lower_right_corner().y
    end_x = rectangle.get_lower_right_corner().x
    c1_color = rectangle.outline_color

    circle = rg.Circle(rg.Point(x1, y1), r)
    rectangle = rg.Rectangle(rg.Point(start_x, start_y),
                             rg.Point(end_x, end_y))
    circle.attach_to(window)
    rectangle.attach_to(window)
    window.render()
    window.continue_on_mouse_click()

    line = rg.Line(rg.Point(start_x, start_y), rg.Point(end_x, end_y))
    line.attach_to(window)
    window.render()
    window.continue_on_mouse_click()

    circle.fill_color = c1_color
    window.render()
Example #27
0
def circle_and_rectangle():
    """
    -- Constructs an rg.RoseWindow.
    -- Constructs and draws a rg.Circle and rg.Rectangle
       on the window such that:
          -- They fit in the window and are easily visible.
          -- The rg.Circle is filled with 'blue'
    -- Prints (on the console, on SEPARATE lines) the following data
         associated with your rg.Circle:
          -- Its outline thickness.
          -- Its fill color.
          -- Its center.
          -- Its center's x coordinate.
          -- Its center's y coordinate.
    -- Prints (on the console, on SEPARATE lines) the same data
         but for your rg.Rectangle.
    -- Waits for the user to press the mouse, then closes the window.

    Here is an example of the output on the console,
    for one particular circle and rectangle:
           1
           blue
           Point(180.0, 115.0)
           180
           115
           1
           None
           Point(75.0, 150.0)
           75.0
           150.0
    """
    # ------------------------------------------------------------------
    # DONE: 3. Implement this function, per its doc-string above.
    #   -- ANY objects that meet the criteria are fine.
    # Put a statement in   main   to test this function
    #    (by calling this function).
    #
    # IMPORTANT: Use the DOT TRICK to guess the names of the relevant
    #       instance variables for outline thickness, etc.
    # ------------------------------------------------------------------

    window = rg.RoseWindow(400, 400)

    circle = rg.Circle(rg.Point(120, 120), 100)
    circle.fill_color = 'blue'
    circle.attach_to(window)
    print(circle.outline_thickness)
    print(circle.fill_color)
    print(circle.center)
    print(circle.center.x)
    print(circle.center.y)

    rectangle = rg.Rectangle(rg.Point(190, 200), rg.Point(390, 300))
    rectangle.attach_to(window)
    rectangle.fill_color = 'SteelBlue'
    print(rectangle.outline_thickness)
    print(rectangle.fill_color)
    print(rectangle.corner_1)
    print(rectangle.corner_1.x)
    print(rectangle.corner_1.y)
    print(rectangle.corner_2)
    print(rectangle.corner_2.x)
    print(rectangle.corner_2.y)

    window.render()
    window.close_on_mouse_click()
Example #28
0
def problem2b(rect, n, delta, win):
    """
    See   problem2b_picture.pdf   in this project for pictures
    that may help you better understand the following specification:
    
    What comes in:
      -- An rg.Rectangle.
      -- A positive integer n.
      -- A positive integer delta.
      -- An rg.RoseWindow.
    What goes out:  Nothing (i.e., None).
    Side effects:
      Draws n rg.Rectangles on the given rg.RoseWindow such that:
        -- The first rg.Rectangle is the given one.
        -- Subsequent rg.Rectangles have the same center
            as the given rg.Rectangle, but their width
            and height are each   (2 * delta)   greater than
            the width and height of the previous rg.Rectangle.
            That is, the distance from each line of each rg.Rectangle
            to the corresponding line of the rg.Rectangle next to it
            is delta.  (See problem2b_picture.)
      Must render but   ** NOT close **   the window.

    Type hints:
      :type rect:   rg.Rectangle
      :type n:      int
      :type delta:  int
      :type win:    rg.RoseWindow
    """
    movex = delta
    movey = delta

    x1 = rect.corner_1.x
    y1 = rect.corner_1.y
    x2 = rect.corner_2.x
    y2 = rect.corner_2.y

    if x1 <= x2:
        xtopright = x2
        xbottomleft = x1

    if x2 <= x1:
        xbottomleft = x2
        xtopright = x1
    if y1 <= y2:
        ytopright = y1
        ybottomleft = y2

    if y2 <= y1:
        ybottomleft = y1
        ytopright = y2

    for _ in range(n):
        xtopright += movex
        ytopright -= movey
        xbottomleft -= movex
        ybottomleft += movey

        topright = rg.Point(xtopright, ytopright)
        bottomleft = rg.Point(xbottomleft, ybottomleft)

        rectangle = rg.Rectangle(topright, bottomleft)
        rectangle.attach_to(win)

    win.render()
def run_test_swap_colors():
    """ Tests the   swap_colors   function. """
    print()
    print('--------------------------------------------------')
    print('Testing the   swap_colors   function:')
    print('--------------------------------------------------')

    # ------------------------------------------------------------------
    # Test 1:  Note that it tests both:
    #            -- what was SUPPOSED to be mutated (fill_color) and
    #            -- what was NOT supposed to be mutated (all else).
    #   The code passes this test if the expected value printed
    #   is the same as the actual value printed.
    # ------------------------------------------------------------------
    circle = rg.Circle(rg.Point(100, 150), 50)
    circle.fill_color = 'blue'

    rectangle = rg.Rectangle(rg.Point(200, 30), rg.Point(350, 150))
    rectangle.fill_color = 'green'

    expected_c = 'Circle: center=(100, 150), radius=50,'
    expected_c += ' fill_color=green, outline_color=black,'
    expected_c += ' outline_thickness=1.'

    expected_r = 'Rectangle: corner_1=(200, 30), corner_2=(350, 150),'
    expected_r += ' fill_color=blue, outline_color=black,'
    expected_r += ' outline_thickness=1.'

    swap_colors(circle, rectangle)

    print()
    print('Expected circle after the function call:')
    print(expected_c)
    print('Actual circle after the function call:')
    print(circle)

    print()
    print('Expected rectangle after the function call:')
    print(expected_r)
    print('Actual rectangle after the function call:')
    print(rectangle)

    # ------------------------------------------------------------------
    # Test 2:  This is a VISUAL test.
    #   The code passes this test if the objects are drawn per the test.
    #
    #   Here, that means that when the window pauses and asks the user
    #   to press any key to continue:
    #     -- the circle is black_filled and
    #     -- the rectangle is red_filled
    #   and then when the user presses a key and the window pauses
    #   again, now the reverse is true:
    #     -- the circle is red_filled and
    #     -- the rectangle is black_filled.
    # ------------------------------------------------------------------
    title = 'The code passes the test if the fill colors of the circle'
    title += ' and rectangle get SWAPPED.'
    window = rg.RoseWindow(700, 400, title)

    circle = rg.Circle(rg.Point(100, 50), 40)
    circle.fill_color = 'black'
    circle.attach_to(window)

    rectangle = rg.Rectangle(rg.Point(200, 280), rg.Point(350, 350))
    rectangle.fill_color = 'red'
    rectangle.attach_to(window)

    msg1 = 'At this point, the circle should be filled with BLACK\n'
    msg1 += 'and the rectangle should be filled with RED'
    message = rg.Text(rg.Point(400, 100), msg1)
    message.attach_to(window)

    # At this point, the CIRCLE should be filled with BLACK
    #                   and the RECTANGLE filled with RED.
    window.render()
    window.continue_on_mouse_click()

    swap_colors(circle, rectangle)

    # At this point, the CIRCLE should be filled with RED
    #                   and the RECTANGLE filled with BLACK.
    msg2 = 'Now, the circle should be filled with RED\n'
    msg2 += 'and the rectangle should be filled with BLACK.\n'
    msg2 += 'If so, and if nothing else changed,\n'
    msg2 += 'the code passed this test.'
    message.text = msg2

    window.render()
    window.close_on_mouse_click()
Example #30
0
def run_test_fill_from_colors():
    """ Tests the   fill_from_colors   function. """
    print('--------------------------------------------------')
    print('Testing the   fill_from_colors   function:')
    print('See the two graphics windows that pop up.')
    print('--------------------------------------------------')

    # ------------------------------------------------------------------
    # Test 1: Flashes red, white, blue -- 5 times.
    # ------------------------------------------------------------------
    title = 'Red, white and blue, repeated 5 times!'
    window = rg.RoseWindow(400, 180, title, canvas_color='dark gray')

    circle = rg.Circle(rg.Point(150, 100), 40)
    circle.attach_to(window.initial_canvas)

    number_of_cycles = 5
    window.continue_on_mouse_click('Click anywhere in here to start')

    for _ in range(number_of_cycles):
        fill_from_colors(window, circle, ['red', 'white', 'blue'])

    window.close_on_mouse_click()

    # ------------------------------------------------------------------
    # Test 2: Flashes through a bunch of colors, looping through the
    # list forwards in a rectangle, then backwards in an ellipse.
    # ------------------------------------------------------------------
    colors = [
        'red', 'white', 'blue', 'chartreuse', 'chocolate', 'DodgerBlue',
        'LightPink', 'maroon', 'yellow', 'green', 'SteelBlue', 'black'
    ]

    title = 'Loop through 12 colors, forwards then backwards!'
    window = rg.RoseWindow(450, 250, title, canvas_color='yellow')

    rect_width = 100
    rect_height = 40
    rect_center = rg.Point(125, 100)
    rectangle = rg.Rectangle(
        rg.Point(rect_center.x - (rect_width / 2),
                 rect_center.y - (rect_height / 2)),
        rg.Point(rect_center.x + (rect_width / 2),
                 rect_center.y + (rect_height / 2)))

    oval_width = 70
    oval_height = 160
    oval_center = rg.Point(300, 100)
    ellipse = rg.Ellipse(
        rg.Point(oval_center.x - (oval_width / 2),
                 oval_center.y - (oval_height / 2)),
        rg.Point(oval_center.x + (oval_width / 2),
                 oval_center.y + (oval_height / 2)))

    rectangle.attach_to(window)
    ellipse.attach_to(window)
    window.render()
    window.continue_on_mouse_click('Click anywhere in here to start')

    # This function call iterates through the colors,
    # filling the rectangle with those colors:
    fill_from_colors(window, rectangle, colors)

    # The  reverse  method reverses its list IN PLACE
    # (i.e., it "mutates" its list -- more on that in future sessions).
    colors.reverse()

    window.continue_on_mouse_click()

    # This function call iterates through the colors,
    # filling the ellipse (oval) with those colors:
    fill_from_colors(window, ellipse, colors)

    window.close_on_mouse_click()