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). # # CONSIDER 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! ** ########################################################################### # ------------------------------------------------------------------------- circle.attach_to(window) square_x = circle.center.x square_y = circle.center.y for _ in range(n): square = rg.Square(rg.Point(square_x, square_y), circle.radius * 2) square.attach_to(window) square_x = square_x + circle.radius square_y = square_y + circle.radius window.render()
def draw_squares_from_circle(n, circle, window): drawncircle = circle windowsfc = window centersquare = drawncircle.center csx = centersquare.x csy = centersquare.y sidesquare = drawncircle.radius * 2 #print(centersquare) drawncircle.attach_to(windowsfc) for k in range(n): square = rg.Square( rg.Point(csx + k * sidesquare / 2, csy + k * sidesquare / 2), sidesquare) square.attach_to(windowsfc) windowsfc.render() """
def broken_5(circle, window): """ What comes in: an rg.Circle and an rg.RoseWindow. What goes out: Nothing (i.e., None). Side effects: Draws the given rg.Circle and an rg.Square that circumscribes it, both on the given rg.RoseWindow. Must ** render ** but ** NOT close ** the window. Type hints: :type circle: rg.Circle :type window: rg.RoseWindow """ circle.attach_to(window) square = rg.Square(circle.center, circle.radius * 2) square.outline_color = circle.fill_color square.attach_to(window) window.render()
class ShapesTest(unittest.TestCase): """ Runs tests for the Shapes specified in the class variable shapes below. """ shapes = (rg.Rectangle(rg.Point(50, 100), rg.Point(70, 140)), rg.Ellipse(rg.Point(50, 100), rg.Point(70, 140)), rg.Circle(rg.Point(50, 100), 30), rg.Square(rg.Point(50, 100), 30), rg.Point(50, 100), rg.Line(rg.Point(50, 100), rg.Point(70, 140))) speed = 10 def test(self): testcase = ShapeTest() testcase.speed = ShapesTest.speed for shape in ShapesTest.shapes: testcase.start_shape = shape testcase.test()
def draw_squares_from_circle(n, circle, window): x = circle.center.x y = circle.center.y center = rg.Point(x, y) circle = rg.Circle(center, circle.radius) circle.attach_to(window) circle.fill_color = 'pink' for k in range(n): square = rg.Square(center, 2 * circle.radius) square.outline_color = 'pink' square.attach_to(window) x = x + circle.radius y = y + circle.radius center = rg.Point(x, y) window.render() """ 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 """ """
def rectangles_from_circles(circles): """ See rectangles_from_circles.pdf in this project for pictures that may help you better understand the following specification: What comes in: -- a sequence of rg.Circle objects What goes out: Returns a list of rg.Rectangles, where each rg.Rectangle circumscribes its corresponding rg.Circle in the given list of rg.Circles. Side effects: None. Examples: See rectangles_from_circles.pdf in this project. Type hints: :type circles: list | tuple of rg.Circle :rtype: list of rg.Rectangles """ seq = [] for k in range(len(circles)): seq += [rg.Square(circles[k].center, circles[k].radius * 2)] return seq
def broken_5(circle, window): """ What comes in: an rg.Circle and an rg.RoseWindow. What goes out: Nothing (i.e., None). Side effects: Draws the given rg.Circle and an rg.Square that circumscribes it, both on the given rg.RoseWindow, with the rg.Square having the same OUTLINE color as the FILL color of the given rg.Circle. Must ** render ** but ** NOT close ** the window. Type hints: :type circle: rg.Circle :type window: rg.RoseWindow """ circle.attach_to(window) square = rg.Square(circle.center, 2 * circle.radius) square.outline_color = circle.__getattribute__('fill_color') square.attach_to(window) window.render()
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: """ circle.attach_to(window) window.render(0.01) rectcenter = circle.center for k in range(n): rect = rg.Square(rectcenter, circle.radius * 2) rect.attach_to(window) window.render(0.01) rectcenter = rg.Point(rectcenter.x + circle.radius, rectcenter.y + circle.radius) """
def rectangles_from_circles(circles): """ See rectangles_from_circles.pdf in this project for pictures that may help you better understand the following specification: What comes in: -- a sequence of rg.Circle objects What goes out: Returns a list of rg.Rectangles, where each rg.Rectangle circumscribes its corresponding rg.Circle in the given list of rg.Circles. Side effects: None. Examples: See rectangles_from_circles.pdf in this project. Type hints: :type circles: list | tuple of rg.Circle :rtype: list of rg.Rectangles """ # ------------------------------------------------------------------------- # TODO: 10. Implement and test this function. # The testing code is already written for you (above). # ########################################################################### # IMPORTANT: Examine the testing code above carefully. Be sure # that you understand WHY the tests are adequate tests! # # IMPORTANT: The specification does NOT say to draw anything # in this function, so DON'T draw anything in here! ########################################################################### # ------------------------------------------------------------------------- window = rg.RoseWindow() for k in range(len(circles)): circles[k].attach_to(window) square = rg.Square(circles[k].center, circles[k].radius * 2) square.attach_to(window) circles[k].attach_to(window) window.render()
def main(): """ Uses ROSEGRAPHICS to demonstrate: -- CONSTRUCTING objects, -- applying METHODS to them, and -- accessing their DATA via INSTANCE VARIABLES """ example1() example2() example3() window = rg.RoseWindow() point3 = rg.Point(200, 300) point3.attach_to(window) point4 = rg.Point(200,200) circle = rg.Circle(point4, 50) circle.attach_to(window) rg.Square(point3,10) rg.Ellipse(point3,point4) window.render() 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 """ circle.attach_to(window) window.render() cc = circle.clone() sq_center = cc.center sq_side_length = cc.radius * 2 for k in range(n): sq = rg.Square(sq_center, sq_side_length) sq.attach_to(window) window.render() sq_center.x += (sq_side_length / 2) sq_center.y += (sq_side_length / 2)
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. """ multiplier = 0 for k in range(m): new_square = rg.Square(square.center, square.length_of_each_side) new_square.attach_to(window) hourglass(window, k + 1, square.center, square.length_of_each_side / 2, colors[-k]) multiplier = multiplier + 2 square.center.x = square.center.x + square.length_of_each_side square.length_of_each_side = square.length_of_each_side + square.length_of_each_side * multiplier window.render()
def problem1(square, thickness, window): """ See problem1_picture.pdf in this project for pictures that may help you better understand the following specification: What comes in: -- An rg.Square. -- A positive integer -- An rg.RoseWindow. What goes out: Nothing (i.e., None). Side effects: -- Draws, on the given rg.RoseWindow: -- The given rg.Square. -- An rg.Circle that: -- is directly below and touching the given rg.Square, -- has diameter the same as the length of each side of the given rg.Square, -- has fill color the same as the fill color of the given rg.Square, and -- has the given thickness as its outline thickness. (SEE THE PICTURES.) -- An rg.Line that: -- has one endpoint is at the center of the above rg.Circle, -- has another endpoint that is at the midpoint of the left side of the given rg.Square, -- has color the same as the outline color of the given rg.Square, and -- has the given thickness. (SEE THE PICTURES.) Note: Attach the rg.Line AFTER attaching the rg.Square and rg.Circle. Must render but ** NOT close ** the window. Type hints: :type square: rg.Square :type thickness: int :type window: rg.RoseWindow """ # ------------------------------------------------------------------------- # DONE: 3. Implement and test this function. SEE THE PICTURES in the PDF! # Tests have been written for you (above). # ------------------------------------------------------------------------- start_x = square.center.x start_y = square.center.y length = square.length_of_each_side radius = length / 2 fill_color = square.fill_color circle_thick = thickness line_color = square.outline_color thickness = square.outline_thickness square = rg.Square(rg.Point(start_x, start_y), length) square.fill_color = fill_color square.outline_color = line_color square.outline_thickness = thickness circle = rg.Circle(rg.Point(start_x, (start_y + length)), radius) circle.fill_color = fill_color circle.outline_thickness = circle_thick square.attach_to(window) circle.attach_to(window) line = rg.Line(rg.Point((start_x - radius), start_y), rg.Point(start_x, (start_y + length))) line.thickness = circle_thick line.color = line_color line.attach_to(window) window.render()
def hourglass(window, n, point, radius, color): """ See hourglass_picture.pdf in this project for pictures that may help you better understand the following specification: Displays an "hourglass" shape of circles in the given window. -- Each circle has the given radius and given color. -- Each circle has a horizontal line drawn through it. -- The middlemost of the circles is centered at the given point. -- There is a single circle in that middlemost row. -- There are n rows (including the middlemost row) of circles going UP from the middlemost circle. -- There are n rows (including the middlemost row) of circles going DOWN from the middlemost circle. -- Each circle barely touches its neighbor circles. Preconditions: :type window: rg.RoseWindow :type n: int :type point: rg.Point :type radius: int :type color: str where n and radius are positive and color is a string that denotes a color that rosegraphics understands. """ # ------------------------------------------------------------------------- # DONE: 2. Implement and test this function. # We provided some tests for you (above). # ------------------------------------------------------------------------- ########################################################################### # BONUS: Avoid replicated code if you can. Hint: You are allowed # to define an additional function(s) if you wish. ########################################################################### # ------------------------------------------------------------------------- # DIFFICULTY AND TIME RATINGS (see top of this file for explanation) # DIFFICULTY: 8 # TIME ESTIMATE: 25 minutes (warning: this problem is challenging) # ------------------------------------------------------------------------- circle = rg.Circle(point, radius) circle.fill_color = color circle.attach_to(window) p1 = rg.Point(point.x - radius, point.y) p2 = rg.Point(point.x + radius, point.y) line = rg.Line(p1, p2) line.attach_to(window) rect = rg.Rectangle(rg.Point(0, 0), rg.Point(0, 0)) point1 = rg.Point(point.x, point.y) for i in range(n - 1): point1.x = point.x - (radius * (i + 1)) point1.y = point.y - ((radius * math.sqrt(3)) * (i + 1)) for j in range(i + 2): circle1 = rg.Circle(point1, radius) circle1.fill_color = color circle1.attach_to(window) p1 = rg.Point(point1.x - radius, point1.y) p2 = rg.Point(point1.x + radius, point1.y) line1 = rg.Line(p1, p2) line1.attach_to(window) point1.x = point1.x + radius * 2 point1.x = point.x - radius * n point1.y = point1.y - radius rect.corner_1 = point1 point2 = rg.Point(point.x, point.y) for k in range(n - 1): point2.x = point.x - (radius * (k + 1)) point2.y = point.y + ((radius * math.sqrt(3)) * (k + 1)) for l in range(k + 2): circle2 = rg.Circle(point2, radius) circle2.fill_color = color circle2.attach_to(window) p1 = rg.Point(point2.x - radius, point2.y) p2 = rg.Point(point2.x + radius, point2.y) line2 = rg.Line(p1, p2) line2.attach_to(window) point2.x = point2.x + radius * 2 point2.x = point2.x - radius point2.y = point2.y + radius rect.corner_2 = point2 window.render() if n == 1: return rg.Square(point, radius * 2) return rect
def problem1(square, thickness, window): """ See problem1_picture.pdf in this project for pictures that may help you better understand the following specification: What comes in: -- An rg.Square. -- A positive integer -- An rg.RoseWindow. What goes out: Nothing (i.e., None). Side effects: -- Draws, on the given rg.RoseWindow: -- The given rg.Square. -- An rg.Circle that: -- is directly below and touching the given rg.Square, -- has diameter the same as the length of each side of the given rg.Square, -- has fill color the same as the fill color of the given rg.Square, and -- has the given thickness as its outline thickness. (SEE THE PICTURES.) -- An rg.Line that: -- has one endpoint is at the center of the above rg.Circle, -- has another endpoint that is at the midpoint of the left side of the given rg.Square, -- has color the same as the outline color of the given rg.Square, and -- has the given thickness. (SEE THE PICTURES.) Note: Attach the rg.Line AFTER attaching the rg.Square and rg.Circle. Must render but ** NOT close ** the window. Type hints: :type square: rg.Square :type thickness: int :type window: rg.RoseWindow """ # ------------------------------------------------------------------------- # DONE: 3. Implement and test this function. SEE THE PICTURES in the PDF! # Tests have been written for you (above). # ------------------------------------------------------------------------- sponge = rg.Square(square.center, square.length_of_each_side) sponge.fill_color = square.fill_color sponge.outline_color = square.outline_color sponge.outline_thickness = square.outline_thickness sponge.attach_to(window) point = rg.Point(sponge.center.x, sponge.center.y + sponge.length_of_each_side) radius = sponge.length_of_each_side / 2 pat = rg.Circle(point, radius) pat.fill_color = square.fill_color pat.outline_thickness = thickness pat.attach_to(window) left = sponge.center.x - (sponge.length_of_each_side / 2) middle = sponge.center.y left_middle = rg.Point(left, middle) stick = rg.Line(pat.center, left_middle) stick.attach_to(window) stick.color = square.outline_color stick.thickness = thickness window.render()
def problem3(left_circle, right_circle, n, color1, color2, window): """ See problem3_pictures.pdf for pictures that may help you better understand the following specification: What comes in: -- Two rg.Circle objects, where they have the same radius and the second one is guaranteed to be to directly to the right of the first one. (Hence, the two rg.Circle's centers have the same y-coordinate. See the PDF.) -- A positive integer n. -- Two strings that are colors (e.g. 'red' or 'green' or ...) -- An rg.RoseWindow. What goes out: Nothing (i.e., None). Side effects: 1. Draws the two given rg.Circle objects on the given rg.RoseWindow. 2. Draws n rg.Square objects on the given rg.RoseWindow such that: -- The leftmost rg.Square is directly to the right of the first (i.e, leftmost) of the two given rg.Circles. -- There is an rg.Square directly to the right of the leftmost rg.Square, then another rg.Square directly to the right of it, and so forth, with the rightmost rg.Square being directly to the left of the second (i.e., rightmost) rg.Circle. -- All n rg.Squares are the same size. -- The rg.Squares alternate fill colors, with color1 being the fill color for the leftmost rg.Square. (See the PDF.) ** See problem3_pictures.pdf for examples. ** Must render but ** NOT close ** the window. Type hints: :type left_circle: rg.Circle :type right_circle: rg.Circle :type n: int :type color1: str :type color2: str :type window: rg.RoseWindow """ left_circle.attach_to(window) right_circle.attach_to(window) center1 = left_circle.center center2 = right_circle.center side_length = (center2.x - center1.x - 2 * left_circle.radius) / n x1 = center1.x + left_circle.radius + (side_length / 2) y1 = center1.y for k in range(n): value = k + 1 point1 = rg.Point(x1, y1) square1 = rg.Square(point1, side_length) square1.attach_to(window) if value % 2 == 1: square1.fill_color = color1 else: square1.fill_color = color2 x1 = x1 + side_length
x = 100 y = 100 radius = 20 center = rg.Point(x, y) circle = rg.Circle(center, radius) circle.fill_color='green' circle.attach_to(window) # Draw the squares x = 100 y = 100 length_side = 40 for k in range(7): center = rg.Point(x, y) square = rg.Square(center, length_side) square.attach_to(window) x = x + 20 y = y + 20 # Draw the hollow circle x = 350 y = 70 radius = 50 center = rg.Point(x, y) circle2 = rg.Circle(center, radius) circle2.attach_to(window) # Draw the squares x = 350
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(300, 300, 'Circle and Square Window') x1 = 100 y1 = 150 color_circle = 'blue' center_circle = rg.Point(x1, y1) circle = rg.Circle(center_circle, 60) circle.fill_color = color_circle cot = circle.outline_thickness = 1 circle.attach_to(window) x2 = 200 y2 = 100 center_square = rg.Point(x2, y2) square = rg.Square(center_square, 50) sot = square.outline_thickness = 1 square.attach_to(window) print(cot) print(color_circle) print('Point(', x1, ', ', y1, ')') print(x1) print(y1) print(sot) print('None') print('Point(', x2, ', ', y2, ')') print(x2) print(y2) window.render() window.close_on_mouse_click()