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 """ width = 700 height = 600 window = rg.RoseWindow(width, height) x = 200 y = 100 center_point = rg.Point(x, y) radius = 55 circle = rg.Circle(center_point, radius) circle.fill_color = 'blue' circle.outline_thickness = 3 circle.attach_to(window) print(circle.outline_thickness) print(circle.fill_color) print('Point', center_point) print(center_point.x) print(center_point.y) point1 = rg.Point(300, 150) point2 = rg.Point(400, 50) rectangle = rg.Rectangle(point1, point2) rectangle.outline_thickness = 4 rectangle.attach_to(window) print(rectangle.outline_thickness) print(rectangle.fill_color) print(rectangle.get_center()) print(rectangle.get_center().x) print(rectangle.get_center().y) window.render() window.close_on_mouse_click()
def run_test_rectangles_from_circles(): """ Tests the rectangles_from_circles function. """ print() print('-----------------------------------------------------------') print('Testing the rectangles_from_circles function:') print('-----------------------------------------------------------') print('See the graphics window that pops up.') print('It should show circles, then the circles circumscribed,') print('then more circles, then the new circles circumscribed too.') print() print('See rectangles_from_circles.pdf in this project') print('for pictures of the anticipated results.') # ------------------------------------------------------------------------- # Test 1 is ALREADY DONE (here). # ------------------------------------------------------------------------- window = rg.RoseWindow(650, 350, 'rectangles_from_circles, two tests') circles = [ rg.Circle(rg.Point(50, 80), 40), rg.Circle(rg.Point(150, 50), 30), rg.Circle(rg.Point(300, 100), 50), rg.Circle(rg.Point(220, 70), 60) ] circles[0].fill_color = 'red' circles[1].fill_color = 'white' circles[2].fill_color = 'blue' circles[3].fill_color = 'green' # ------------------------------------------------------------------------- # This test calls the draw_shapes function that YOU write, # above. So if your draw_shapes breaks, so will this test. # ------------------------------------------------------------------------- draw_shapes(circles, window) message = 'The circles to be circumscribed are shown above.' message = message + ' Click to continue.' window.continue_on_mouse_click(message) rectangles = rectangles_from_circles(circles) if rectangles is None: print() print('Either you have not yet gotten') print(' to the rectangles_from_circles problem (OK, no problem)') print(' or you have forgotten to return a result from that function.') window.close_on_mouse_click() return draw_shapes(rectangles, window) message = 'Now you should see the circumscribing rectangles too.' message = message + ' Click to continue.' window.continue_on_mouse_click(message) # ------------------------------------------------------------------------- # Test 2 is ALREADY DONE (here). # It runs in the same window as Test 1. # ------------------------------------------------------------------------- circles = [] center = rg.Point(50, 150) radius = 35 for _ in range(10): circle = rg.Circle(center, radius) circle.fill_color = 'magenta' circles = circles + [circle] center.x = center.x + 2 * radius center.y = center.y + 15 radius = radius - 3 draw_shapes(circles, window) message = 'More circles to be circumscribed are shown above.' message = message + ' Click to continue.' window.continue_on_mouse_click(message) rectangles = rectangles_from_circles(circles) draw_shapes(rectangles, window) message = 'Now you should see the circumscribing rectangles too.' message = message + ' Click to exit.' window.continue_on_mouse_click(message, close_it=True)
# draws all the objects attached to the window # # g. When is a RoseWindow close_on_mouse_click method call necessary? Why? # It is necessary a the end of the program, and it is necessary to keep all windows open until the end # # ASK QUESTIONS ** NOW ** if you do not understand how the # RoseGraphics graphics system works. # # When you are confident that you have written correct answers # to the above questions (ASK QUESTIONS AS NEEDED!), # change the above TO DO to DONE. # ######################################################################## import rosegraphics as rg window = rg.RoseWindow() window2 = rg.RoseWindow(50, 100) def main(): window = rg.RoseWindow() """ Uses ROSEGRAPHICS to demonstrate: -- CONSTRUCTING objects, -- applying METHODS to them, and -- accessing their DATA via INSTANCE VARIABLES """ example1() example2() example3()
def run_test_problem3a(): """ Tests the problem3a function. """ # ------------------------------------------------------------------ # TODO: 2. Implement this TEST function. # It TESTS the problem1a function defined below. # Include at least ** 5 ** tests (we wrote four for you). # ------------------------------------------------------------------ # ------------------------------------------------------------------ # DIFFICULTY AND TIME RATINGS (see top of this file for explanation) # DIFFICULTY: 4 # TIME ESTIMATE: 10 to 15 minutes. # ------------------------------------------------------------------ # Window 1: title = 'Problem 3a. Test 1: Start at (30, 30), 6 lines' window1 = rg.RoseWindow(350, 200, title) # Test 1 (it is on window 1): point = rg.Point(30, 30) expected = 36 answer = problem3a(window1, point, 6) print() print('Test 1 expected:', expected) print(' actual: ', answer) window1.close_on_mouse_click() # Window 2: title = 'Problem 3a. Test 2: Start at (80, 10), 9 lines.' title += ' Test 3: Start at (30, 50), 3 lines.' window2 = rg.RoseWindow(550, 200, title) # Test 2 (it is on window 2): point = rg.Point(80, 10) expected = 75 answer = problem3a(window2, point, 9) print() print('Test 2 expected:', expected) print(' actual: ', answer) # Test 3 (it is also on window 2): point = rg.Point(30, 50) expected = 9 answer = problem3a(window2, point, 3) print() print('Test 3 expected:', expected) print(' actual: ', answer) window2.close_on_mouse_click() # Window 3: title = 'Problem 3a. Test 4: Start at (30, 30), 20 lines' window3 = rg.RoseWindow(450, 300, title) # Test 4 (it is on window 3): point = rg.Point(30, 30) expected = 218 answer = problem3a(window3, point, 20) print() print('Test 4 expected:', expected) print(' actual: ', answer) window3.close_on_mouse_click()
def run_test_RETURN_circle(): """ Tests the RETURN_circle function. """ print() print('-----------------------------------------') print('Testing RETURN_circle:') print('-----------------------------------------') print() print('See the graphics window for this test.') print('If an error msg appears at any point,') print('then you have FAILED this test.') # ------------------------------------------------------------------ # Tests 1 and 2 (on one window): # ------------------------------------------------------------------ window = rg.RoseWindow(500, 400, 'Testing RETURN_circle') text = rg.Text(rg.Point(250, 125), '') text.attach_to(window.initial_canvas) circle = rg.Circle(rg.Point(200, 300), 50) circle.fill_color = 'blue' circle.attach_to(window.initial_canvas) msg = 'Note: If you see an error message at ANY point,\n' msg = msg + 'then you have failed this test.\n\n' msg = msg + 'I have drawn the original, blue circle.\n\n' msg = msg + 'So you should see a BLUE circle below\n' msg = msg + '(and nothing else).\n\n' msg = msg + 'Click the mouse to continue this test.\n' text.text = msg window.render(0.5) window.get_next_mouse_click() new_circle = RETURN_circle(circle, 'red') if new_circle: new_circle.attach_to(window.initial_canvas) msg = 'I have now called your RETURN_circle function\n' msg = msg + 'and drawn the CLONED, RED circle that your\n' msg = msg + 'RETURN_circle function should have returned.\n\n' msg = msg + 'So you should see a RED circle below\n' msg = msg + '(and nothing else, since the blue circle\n' msg = msg + 'is beneath the red circle).\n\n' msg = msg + 'Click the mouse to continue this test.' text.text = msg window.render(0.5) window.get_next_mouse_click() if new_circle: new_circle.detach_from(window.initial_canvas) msg = 'I have now UN-drawn the CLONED, RED circle.\n\n' msg = msg + 'So you should see the original, BLUE circle below\n' msg = msg + '(and nothing else).\n\n' msg = msg + 'Click the mouse to continue this test.' text.text = msg window.render(0.5) window.get_next_mouse_click() circle.detach_from(window.initial_canvas) msg = 'I have now UN-drawn the ORIGINAL, blue circle.\n\n' msg = msg + 'So you should see NO circles below.\n\n' msg = msg + 'Click the mouse to continue this test.' text.text = msg window.render(0.5) window.get_next_mouse_click() if new_circle: new_circle.attach_to(window.initial_canvas) msg = 'Now I have RE-drawn the CLONED, RED circle\n' msg = msg + 'that your RETURN_circle function\n' msg = msg + 'should have returned.\n\n' msg = msg + 'So you should see a RED circle below\n' msg = msg + '(and nothing else).\n\n' msg = msg + 'Click the mouse to continue this test.' text.text = msg window.render(0.5) window.get_next_mouse_click() # ------------------------------------------------------------------ # Note: The following statements make the variable new_circle # refer to a new, green circle. So we can no longer refer # to the RED circle (to which the variable new_circle once # referred). But the red circle still exists! # ------------------------------------------------------------------ new_circle = RETURN_circle(circle, 'green') if new_circle: new_circle.attach_to(window.initial_canvas) msg = 'Now I have ALSO drawn the CLONED, GREEN circle\n' msg = msg + 'that your RETURN_circle function\n' msg = msg + 'should have returned.\n\n' msg = msg + 'So you should see a GREEN circle below\n' msg = msg + '(and nothing else, since the red circle\n' msg = msg + 'is beneath the green circle).\n\n' msg = msg + 'Click the mouse to continue this test.' text.text = msg window.render(0.5) window.get_next_mouse_click() if new_circle: new_circle.detach_from(window.initial_canvas) msg = 'I have now UN-drawn the CLONED, GREEN circle.\n\n' msg = msg + 'So you should see the cloned, RED circle below\n' msg = msg + '(and nothing else).\n\n' msg = msg + 'Click the mouse to continue this test.' text.text = msg window.render(0.5) window.get_next_mouse_click() new_circle2 = RETURN_circle(rg.Circle(rg.Point(250, 350), 25), 'white') if new_circle: new_circle.attach_to(window.initial_canvas) if new_circle2: new_circle2.attach_to(window.initial_canvas) msg = 'I have now RE-drawn the CLONED, GREEN circle.\n' msg = msg + 'Additionally, I called your RETURN_circle function\n' msg = msg + 'again, asking it to return a smaller WHITE circle\n' msg = msg + 'that is below and to the right of the other circles.\n\n' msg = msg + 'So you should see a GREEN circle below\n' msg = msg + 'and a smaller WHITE circle below and to its right\n' msg = msg + '(but NOT the red circle, since it\n' msg = msg + 'is beneath the green circle).\n\n' msg = msg + 'Click the mouse to conclude this test.' text.text = msg window.render(0.5) window.get_next_mouse_click() window.close()
def run_test_problem3a(): """ Tests the problem3a function. """ # ------------------------------------------------------------------ # DONE: 2. Implement this TEST function. # It TESTS the problem1a function defined below. # Include at least ** 5 ** tests (we wrote four for you). # ------------------------------------------------------------------ # ------------------------------------------------------------------ # DIFFICULTY AND TIME RATINGS (see top of this file for explanation) # DIFFICULTY: 4 # TIME ESTIMATE: 10 to 15 minutes. # ------------------------------------------------------------------ # Window 1: title = 'Problem 3a. Test 1: Start at (30, 30), 6 lines' window1 = rg.RoseWindow(350, 200, title) # Test 1 (it is on window 1): point = rg.Point(30, 30) expected = 36 answer = problem3a(window1, point, 6) print() print('Test 1 expected:', expected) print(' actual: ', answer) window1.close_on_mouse_click() # Window 2: title = 'Problem 3a. Test 2: Start at (80, 10), 9 lines.' title += ' Test 3: Start at (30, 50), 3 lines.' window2 = rg.RoseWindow(550, 200, title) # Test 2 (it is on window 2): point = rg.Point(80, 10) expected = 75 answer = problem3a(window2, point, 9) print() print('Test 2 expected:', expected) print(' actual: ', answer) # Test 3 (it is also on window 2): point = rg.Point(30, 50) expected = 9 answer = problem3a(window2, point, 3) print() print('Test 3 expected:', expected) print(' actual: ', answer) window2.close_on_mouse_click() # Window 3: title = 'Problem 3a. Test 4: Start at (30, 30), 20 lines' window3 = rg.RoseWindow(450, 300, title) # Test 4 (it is on window 3): point = rg.Point(30, 30) expected = 218 answer = problem3a(window3, point, 20) print() print('Test 4 expected:', expected) print(' actual: ', answer) window3.close_on_mouse_click() # ------------------------------------------------------------------ # TO DO: 2 (continued). # Below this comment (or integrated with one of the above tests, # your choice), add 1 more test case of your own choosing. # ------------------------------------------------------------------ # Test 5 (is on window 4) title = 'Problem 3a. Test 5: starts at (40,40), 15 lines' window4 = rg.RoseWindow(450, 300, title) point = rg.Point(40, 40) expected = 153 answer = problem3a(window4, point, 15) print() print('Test 5 expected', expected) print(' actual: ', answer) window4.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(500, 500) x1 = 100 x2 = 200 x3 = 300 y1 = 100 y2 = 200 y3 = 300 point1 = rg.Point(x1, y1) point2 = rg.Point(x2, y2) point3 = rg.Point(x3, y3) rectangle = rg.Rectangle(point1, point3) circle = rg.Circle(point2, 100) circle.fill_color = 'blue' print("Circles:") print(circle.outline_thickness) print(circle.fill_color) print(point2) print(x2) print(y2) print(rectangle.outline_thickness) print(rectangle.fill_color) print(point2) print(x2) print(y2) circle.attach_to(window) rectangle.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 """ window = rg.RoseWindow(1500, 1500) center_x = 500 center_y = 500 rectangle_1_x = 0 rectangle_1_y = 0 rectangle_2_x = 250 rectangle_2_y = 360 center = rg.Point(center_x, center_y) point_1 = rg.Point(0, 0) point_2 = rg.Point(250, 360) rectangle = rg.Rectangle(point_1, point_2) circle = rg.Circle(center, 250) circle.fill_color = 'blue' thicc = 3 thicc_2 = 1 circle.pen = rg.Pen('black', thicc) rectangle.pen = rg.Pen('black', thicc_2) circle.attach_to(window) rectangle.attach_to(window) window.render() print(thicc) print('blue') print(center) print(center_x) print(center_y) print(thicc_2) print('None') rectangle_center_x = (rectangle_1_x + rectangle_2_x) / 2 rectangle_center_y = (rectangle_1_y + rectangle_2_y) / 2 rectangle_center = rg.Point(rectangle_center_x, rectangle_center_y) print(rectangle_center) print(rectangle_center_x) print(rectangle_center_y) window.close_on_mouse_click()
def start_drawing(title=None): global WINDOW if WINDOW: WINDOW.close() WINDOW = rg.RoseWindow(WIDTH, HEIGHT, title)
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(700, 700, 'yuan&juxing') center = rg.Point(300, 63) xcenter = center.x ycenter = center.y radius = 50 circle = rg.Circle(center, radius) circle.fill_color = 'blue' circle.attach_to(window2) point1 = rg.Point(400, 300) point2 = rg.Point(200, 600) rectangle = rg.Rectangle(point1, point2) rectangle.fill_color = 'PeachPuff1' rectangle.attach_to(window2) centerofrec = rg.Point(((point1.x + point2.x) / 2), ((point1.y + point2.y) / 2)) centerofrecx = centerofrec.x centerofrecy = centerofrec.y window2.render() window2.close_on_mouse_click() print(circle.outline_thickness) print(circle.fill_color) print(center) print(xcenter) print(ycenter) print(rectangle.outline_thickness) print(rectangle.fill_color) print(centerofrec) print(centerofrecx) print(centerofrecy)
def run_test_problem1(): """ Tests the problem1 function. """ print() print('--------------------------------------------------') print('Testing the problem1 function:') print(' See the graphics windows that pop up.') print('--------------------------------------------------') # TWO tests on ONE window. title = 'Tests 1 & 2 of problem 1: ' title += 'cyan/green fills, then black/magenta' window = rg.RoseWindow(450, 350, title) # Test 1: title = 'Tests 1 & 2 of problem 1: ' title += 'cyan/green fills, then black/magenta' window = rg.RoseWindow(450, 350, title) # Test 1: circle = rg.Circle(rg.Point(150, 50), 30) circle.fill_color = 'cyan' circle.outline_color = 'blue' circle.outline_thickness = 3 rect = rg.Rectangle(rg.Point(325, 50), rg.Point(365, 150)) rect.fill_color = 'green' rect.outline_color = 'black' rect.outline_thickness = 5 problem1(circle, rect, 'red', 50, window) window.continue_on_mouse_click() # Test 2: circle = rg.Circle(rg.Point(275, 200), 40) circle.fill_color = 'black' circle.outline_color = 'red' circle.outline_thickness = 15 rect = rg.Rectangle(rg.Point(40, 340), rg.Point(10, 300)) rect.fill_color = 'magenta' rect.outline_color = 'green' rect.outline_thickness = 4 problem1(circle, rect, 'blue', 140, window) window.close_on_mouse_click() # A third test on ANOTHER window. title = 'Test 3 of problem 1: purple/yellow fills' window = rg.RoseWindow(300, 200, title) # Test 3: circle = rg.Circle(rg.Point(50, 150), 20) circle.fill_color = 'purple' circle.outline_color = 'blue' circle.outline_thickness = 5 rect = rg.Rectangle(rg.Point(100, 20), rg.Point(140, 70)) rect.fill_color = 'yellow' rect.outline_color = 'grey' rect.outline_thickness = 10 problem1(circle, rect, 'green', 170, window) window.close_on_mouse_click()
def run_test_fancy_polygon(): """ Tests the fancy_polygon function. """ # ------------------------------------------------------------------ # TODO: 9. Implement this TEST function. # It TESTS the fancy_polygon function defined below. # Include at least ** 1 ** ADDITIONAL test (that YOU write). # # As usual, include both EXPECTED and ACTUAL results in your test # and compute the latter BY HAND (not by running your program). # ------------------------------------------------------------------ print() print('--------------------------------------------------') print('Testing the fancy_polygon function:') print('See the windows that pop up.') print('--------------------------------------------------') # Tests 1 and 2 (on the same window): title = 'FANCY POLYGON tests 1 and 2: 7 blue lines, hops = 2; 5 red lines, hops = 3.' window = rg.RoseWindow(520, 400, title) circle = rg.Circle(rg.Point(100, 100), 80) fancy_polygon(window, circle, 7, 2, 'blue', 3) window.continue_on_mouse_click() circle = rg.Circle(rg.Point(330, 200), 150) fancy_polygon(window, circle, 5, 3, 'red', 3) window.close_on_mouse_click() # Test 3 (on another window): title = 'FANCY POLYGON test 3: 20 lime green lines on blue circle, hops = 7.' window = rg.RoseWindow(480, 350, title) circle = rg.Circle(rg.Point(240, 165), 150) circle.fill_color = 'blue' fancy_polygon(window, circle, 20, 7, 'lime green', 5) window.close_on_mouse_click() # ------------------------------------------------------------------ # Test 4: (YOU write THIS test). # If you wish, try even more tests to get some really cool # pictures. Some that I especially like are: # -- 20 segments, hops of length 5 # -- 51 segments, hops of length 25 # -- 300 segments, hops of length 61 # For all these, filling the circles with one color and using # a contrasting color for the lines makes them especially pretty. # ------------------------------------------------------------------ # Test 4 (on another window): title = 'FANCY POLYGON test 4: 20 blue lines' + \ ' on a lime green circle, hops = 5.' window = rg.RoseWindow(480, 350, title) circle = rg.Circle(rg.Point(240, 165), 150) circle.fill_color = 'lime green' fancy_polygon(window, circle, 20, 5, 'blue', 5) window.continue_on_mouse_click() title = 'FANCY POLYGON test 4: 51 red lines' + \ ' on white circle, hops = 25.' circle = rg.Circle(rg.Point(240, 165), 150) circle.fill_color = 'white' fancy_polygon(window, circle, 51, 25, 'red', 1) window.continue_on_mouse_click() title = 'FANCY POLYGON test 4: 300 black lines' + \ ' on yellow circle, hops = 61.' circle = rg.Circle(rg.Point(240, 165), 150) circle.fill_color = 'yellow' fancy_polygon(window, circle, 300, 61, 'black', 1) 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 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(800, 600, 'Circle_and_Rectangle') circle = rg.Circle(rg.Point(400, 400), 100) circle.fill_color = 'blue' circle.attach_to(window) corner_1 = rg.Point(200, 200) corner_2 = rg.Point(500, 400) rectangle = rg.Rectangle(corner_1, corner_2) center_of_rectangle = rectangle.get_center() center_of_rectangle_x = center_of_rectangle.x center_of_rectangle_y = center_of_rectangle.y rectangle.attach_to(window) print('THIS IS THE CIRCLE\'S DATA') # \' is the string literal for ' since Python already uses '' to segreate strings. Credit: Python Software # Foundation webpage on Python 3. print(circle.outline_thickness, '\n', circle.fill_color, '\n', circle.center, '\n', circle.center) print('THIS IS THE RECTANGLE\'S DATA') print(rectangle.outline_thickness, rectangle.fill_color, center_of_rectangle, center_of_rectangle_x, center_of_rectangle_y, sep='\n') 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 """ window = rg.RoseWindow(700, 500) center = rg.Point(150, 400) radius = 100 circle = rg.Circle(center, radius) circle.fill_color = 'blue' circle.attach_to(window) circle_outline = circle.outline_thickness circle_fill = circle.fill_color circle_center = circle.center center_x = center.x center_y = center.y print("CIRCLE") print("circle outline thickness:", circle_outline) print("fill color:", circle_fill) print("center:", circle_center) print("center x-coordinate:", center_x) print("center y-coordinate:", center_y) print() #start rectangle r1 = rg.Point(422, 50) r2 = rg.Point(690, 172) rectangle = rg.Rectangle(r1, r2) rectangle.attach_to(window) rect_outline = rectangle.outline_thickness rect_fill = rectangle.fill_color rect_cent_x = (r1.x + r2.x) / 2 rect_cent_y = (r1.y + r2.y) / 2 print("RECTANGLE") print("rectangle outline thickness:", rect_outline) print("fill color:", rect_fill) print("center:", rect_cent_x, ",", rect_cent_y) print("center x-coordinate:", rect_cent_x) print("center y-coordinate:", rect_cent_y) print() 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 # -- 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(600, 600) circ = rg.Circle(rg.Point(450, 300), 80) circ.fill_color = 'blue' circ.attach_to(window) window.render() # Printing Values # thickness print(circ.outline_thickness) # fill print(circ.fill_color) # center print(circ.center) # x- coor print(circ.center.x) # y- coor print(circ.center.y) pointone = rg.Point(500, 400) pointtwo = rg.Point(200, 200) rect = rg.Rectangle(pointone, pointtwo) rect.attach_to(window) window.render() # Printing Values # thickness print(rect.outline_thickness) # fill print(rect.fill_color) # center # xcor = rg.Rectangle.corner_1.x-rg.Rectangle.corner_2.x # ycor = rg.Rectangle.corner_1.y - rg.Rectangle.corner_2.y # get_center() is easier than calculating center = rect.get_center() print(center) # x- coor print(center.x) # y- coor print(center.y) 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 """ window = rg.RoseWindow() center = rg.Point(100, 100) radius = 30 circle = rg.Circle(center, radius) circle.fill_color = 'blue' circle.attach_to(window) corner1 = rg.Point(200, 200) corner2 = rg.Point(350, 100) rectangle = rg.Rectangle(corner1, corner2) rectangle.attach_to(window) window.render() print('Circle Outline Thickness:', circle.outline_thickness) print('Circle Fill Color:', circle.fill_color) print('Circle Center', center) print('Circle x-coordinate:', center.x) print('Circle y-coordinate', center.y) print('Rectangle Outline Thickness:', rectangle.outline_thickness) print('Rectangle Fill Color:', rectangle.fill_color) print('Rectangle Center:', rectangle.get_center()) print('Rectangle x-coordinate:', rectangle.get_center().x) print('Rectangle y-coordinate:', rectangle.get_center().y) window.close_on_mouse_click()
def problem3b(m, point1): """ See problem3b_picture.pdf in this project for pictures that may help you better understand the following specification: What comes in: -- A positive integer m. -- An rg.Point. What goes out: -- Returns the sum of the thicknesses of ALL of the lines drawn (over all m sets of lines). Side effects: -- Constructs and displays an rg.RoseWindow that is 400 wide by 650 tall. -- Draws, on the rg.RoseWindow, m SETS of lines, where: -- Each SET of lines is drawn *** by a call to ** problem3a **. *** -- The first set has 3 lines that start at point1 (the given point). -- The second set has 5 lines that start 60 pixels directly below point1. -- The third set has 7 lines that start 120 pixels directly below point1. -- The fourth set has 9 lines that start 180 pixels directly below point1. -- etc until m SETS of lines are drawn (where m is given). -- Each set of lines should have widths (thicknesses) per problem3a. -- Waits for the user to click the mouse (and displays an appropriate message prompting the user to do so), then closes the window. Type hints: :type m: int :type point1: rg.Point """ # ------------------------------------------------------------------ # DONE: 4. Implement and test this function. # Tests have been written for you (above). # #################################################################### # IMPORTANT: # ** For full credit you must appropriately use (call) # ** the problem3a function that you implemented above. #################################################################### # ------------------------------------------------------------------ # DIFFICULTY AND TIME RATINGS (see top of this file for explanation) # DIFFICULTY: 8 or 9 # TIME ESTIMATE: 20 to 30 minutes. # ------------------------------------------------------------------ window = rg.RoseWindow(400, 650) sum_of_thickness = 0 for k in range(3, 3 + (m * 2), 2): problem3a(window, rg.Point(point1.x, point1.y + ((k - 1) * 30)), k) sum_of_thickness = sum_of_thickness + problem3a( window, rg.Point(point1.x, point1.y + ((k - 1) * 30)), k) window.render window.close_on_mouse_click() return sum_of_thickness
def start_drawing(title=None): global WINDOW, DO_DRAW WINDOW = rg.RoseWindow(WIDTH, HEIGHT, title) DO_DRAW = True
def run_test_all(): """ Tests ALL the functions in this module. """ # Test broken_1: window = rg.RoseWindow(title='Testing BROKEN_1') circle1 = rg.Circle(rg.Point(50, 50), 15) circle1.fill_color = 'blue' broken_1(circle1, window) # Test 1 of broken_1 circle2 = rg.Circle(rg.Point(70, 150), 30) circle2.fill_color = 'red' broken_1(circle2, window) # Test 2 of broken_1 window.close_on_mouse_click() # Test broken_2: window = rg.RoseWindow(title='Testing BROKEN_2') broken_2(50, 75, window) # Test 1 of broken_2 broken_2(100, 150, window) # Test 2 of broken_2 window.close_on_mouse_click() # Test broken_3: window = rg.RoseWindow(title='Testing BROKEN_3') broken_3(5, rg.Point(100, 50), 80, 20, window) # Test 1 of broken_3 broken_3(3, rg.Point(50, 150), 40, 50, window) # Test 2 of broken_3 window.close_on_mouse_click() # Test broken_4: window = rg.RoseWindow(title='Testing BROKEN_4') broken_4(50, 75, 40, window) # Test 1 of broken_4 broken_4(100, 150, 75, window) # Test 2 of broken_4 window.close_on_mouse_click() # Test broken_5: window = rg.RoseWindow(title='Testing BROKEN_5') circle = rg.Circle(rg.Point(100, 50), 30) circle.fill_color = 'pink' broken_5(circle, window) # Test 1 of broken_5 circle = rg.Circle(rg.Point(250, 100), 80) circle.fill_color = 'red' broken_5(circle, window) # Test 2 of broken_5 window.close_on_mouse_click() # Test broken_6: expected = 1.8333333 actual = broken_6(3) # Test 1 of broken_6 print("Testing BROKEN_6:\n") print('Expected for BROKEN_6, Test 1:', expected, '(approximately)') print(' Actual for BROKEN_6, Test 1:', actual) expected = 5.1873775 actual = broken_6(100) # Test 2 of broken_6 print() print('Expected for BROKEN_6, Test 2:', expected, '(approximately)') print(' Actual for BROKEN_6, Test 2:', actual) print() # Test broken_7: window = rg.RoseWindow(title='Testing BROKEN_7') broken_7(5, rg.Point(100, 50), 80, 20, window) # Test 1 of broken_7 broken_7(3, rg.Point(50, 150), 40, 50, window) # Test 2 of broken_7 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: Consider 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. ########################################################################### # ------------------------------------------------------------------------- """ 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('--------------------------------------------------') # ------------------------------------------------------------------------- # TWO tests on ONE window. # ------------------------------------------------------------------------- title = 'Tests 1 and 2 of draw_circles_from_rectangle: ' title = title + '4 green large circles and 5 black small circles,' title = title + '8 small blue squares plus 3 large red circles' window1 = rg.RoseWindow(720, 500, title) # Test 1: rectangle1 = rg.Rectangle(rg.Point(400, 250), rg.Point(440, 325)) rectangle1.fill_color = 'green' rectangle1.outline_thickness = 5 rectangle1.outline_color = 'black' draw_circles_from_rectangle(4, 8, rectangle1, window1) # Test 2: rectangle2 = rg.Rectangle(rg.Point(600, 400), rg.Point(500, 450)) rectangle2.fill_color = 'blue' rectangle2.outline_thickness = 3 rectangle2.outline_color = 'red' draw_circles_from_rectangle(8, 3, rectangle2, window1) window1.continue_on_mouse_click() # ------------------------------------------------------------------------- # A third test on ANOTHER window. # ------------------------------------------------------------------------- title = 'Test 3 of draw_circles_from_rectangle: ' title += ' 6 yellow circels plus 10 red circles' window2 = rg.RoseWindow(620, 380, title) # Test 3: rectangle3 = rg.Rectangle(rg.Point(375, 330), rg.Point(350, 280)) rectangle3.fill_color = 'yellow' rectangle3.outline_thickness = 5 rectangle3.outline_color = 'brown' draw_circles_from_rectangle(6, 10, rectangle3, window2) window2.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) note = rg.Circle(rg.Point(100, 100), 80) note.fill_color = "blue" note.attach_to(window) mono = rg.Rectangle(rg.Point(250, 100), rg.Point(350, 300)) mono.attach_to(window) print(note.outline_thickness) print(note.fill_color) print(note.center) print(note.center.x) print(note.center.y) print(mono.outline_thickness) print(mono.fill_color) print(mono.get_center()) print(mono.get_center().x) print(mono.get_center().y) window.render() window.close_on_mouse_click()
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()
def circle_and_rectangle(x1, y1, a1, b1, a2, b2, radius): """ -- 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() color = 'blue' centerc = rg.Point(x1, y1) circle = rg.Circle(centerc,radius) circle.fill_color = color p1 = rg.Point(a1, b1) p2 = rg.Point(a2, b2) rtg = rg.Rectangle(p1, p2) circle.attach_to(window) rtg.attach_to(window) print(circle.outline_thickness) print(circle.fill_color) print(circle.center) print(centerc.x) print(centerc.y) print(rtg.outline_thickness) print(rtg.fill_color) print(rtg.corner_2) print(p2.x) print(p2.y) window.render() window.close_on_mouse_click() circle = rg.Circle
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 """ window1 = rg.RoseWindow() point3 = rg.Point(100, 100) radius3 = 50 circle3 = rg.Circle(point3, radius3) circle3.fill_color = 'blue' circle3.attach_to(window1) point4 = rg.Point(200, 200) point5 = rg.Point(250, 250) rectangle = rg.Rectangle(point4, point5) rectangle.attach_to(window1) window1.render() print(circle3.outline_thickness) print(circle3.fill_color) print(circle3.center) print(point3.x) print(point3.y) print(rectangle.outline_thickness) print(rectangle.fill_color) rect_center = rectangle.get_center() print(rect_center) print(rect_center.x) print(rect_center.y) window1.close_on_mouse_click()
def run_test_MUTATE_circle(): """ Tests the MUTATE_circle function. """ print() print('-----------------------------------------') print('Testing MUTATE_circle:') print('-----------------------------------------') print() print('See the graphics window for this test.') print('If an error msg appears at any point,') print('you have failed this test.') # Tests 1 and 2 (on one window): window = rg.RoseWindow(500, 450, 'Testing MUTATE_circle') text = rg.Text(rg.Point(250, 125), '') text.attach_to(window.initial_canvas) circle = rg.Circle(rg.Point(250, 300), 50) circle.fill_color = 'yellow' circle.attach_to(window.initial_canvas) msg = 'Note: If you see an error message at ANY point,\n' msg = msg + 'then you have failed this test.\n\n' msg = msg + 'I have drawn the original, yellow circle.\n\n' msg = msg + 'So you should see a YELLOW circle below\n' msg = msg + '(and nothing else).\n\n' msg = msg + 'Click the mouse to continue this test.\n' text.text = msg window.render(0.5) window.get_next_mouse_click() result = MUTATE_circle(circle, 'pink', 200) msg = 'I have called your MUTATE_circle function.\n' msg = msg + 'It should have MUTATED the (sole) circle to be pink\n' msg = msg + 'and 200 units to the right of where it was.\n\n' msg = msg + 'So you should see (only) a PINK circle below,\n' msg = msg + 'almost touching the right side of the window.\n\n' msg = msg + 'Click the mouse to continue this test.' if result is not None: msg = msg + '\n\nERROR: You returned a value! Wrong!!!' text.text = msg window.render(0.5) window.get_next_mouse_click() circle.detach_from(window.initial_canvas) msg = 'I have now UN-drawn the (sole) circle.\n\n' msg = msg + 'So you should see NO circles below.\n\n' msg = msg + 'Click the mouse to continue this test.' text.text = msg window.render(0.5) window.get_next_mouse_click() circle.attach_to(window.initial_canvas) msg = 'Now I have RE-drawn the (now pink) circle.\n\n' msg = msg + 'So you should see (only) a PINK circle below,\n' msg = msg + 'just touching the right side of the window.\n' msg = msg + 'Click the mouse to continue this test.' text.text = msg window.render(0.5) window.get_next_mouse_click() result = MUTATE_circle(circle, 'black', -400) msg = 'I have called your MUTATE_circle function again.\n' msg = msg + 'It should have MUTATED the (sole) circle to be black\n' msg = msg + 'and 400 units to the left of where it was.\n\n' msg = msg + 'So you should see (only) a BLACK circle below,\n' msg = msg + 'just touching the left side of the window.\n\n' msg = msg + 'Click the mouse to continue this test.' if result is not None: msg = msg + '\n\nERROR: You returned a value! Wrong!!!' text.text = msg window.render(0.5) window.get_next_mouse_click() another_circle = rg.Circle(circle.center, 25) another_circle.fill_color = 'red' another_circle.attach_to(window.initial_canvas) msg = 'I have constructed and drawn another circle.\n' msg = msg + 'It is red and smaller than the black circle,\n' msg = msg + 'and currently centered on the black circle.\n\n' msg = msg + 'So you should now see a BLACK circle below,\n' msg = msg + 'just touching the left side of the window,\n' msg = msg + 'with a smaller red circle on top of the black circle.\n\n' msg = msg + 'Click the mouse to continue this test.' text.text = msg window.render(0.5) window.get_next_mouse_click() result = MUTATE_circle(another_circle, 'white', 65) msg = 'I have called your MUTATE_circle function again.\n' msg = msg + 'It should have MUTATED the small, red circle to be white\n' msg = msg + 'and 65 units to the left of where it was.\n\n' msg = msg + 'So you should see a BLACK circle below,\n' msg = msg + 'just touching the left side of the window,\n' msg = msg + 'with a WHITE circle slightly overlapping the black one.\n\n' msg = msg + 'Click the mouse to conclude this test.' if result is not None: msg = msg + '\n\nERROR: You returned a value! Wrong!!!' text.text = msg window.render(0.5) window.get_next_mouse_click() window.close()
# GETTING HELP AS NEED! (Ask questions!!!) # # a. For the RoseGraphics coordinate system: # # -- Where is the (0, 0) point on the screen? # (0,0) is in the upper left hand corner. # # -- In what direction on the screen does the positive X-axis point? # The positive x-axis points to the right. # # -- In what direction on the screen does the positive Y-axis point? # The positive y-axis points down. # # b. Write a line of code that constructs a basic RoseWindow object: import rosegraphics as rg window1 = rg.RoseWindow(300, 300) # # c. What is the default height of a RoseWindow? # (Type the code shown in your answer above within main to see if PyCharm will help you figure out the answer. # Hint: After you type the ( in the line of code, if you wait a moment PyCharm will add the ) and has a popup.) # width = 400, height = 300 # # d. Write a line of code that construct a RoseWindow object whose height is 100 with any width you choose. # (again try to use PyCharm's hints to help you figure it out) window2 = rg.RoseWindow(300, 100, 'window 2') # # e. Use the DOT trick to answer the following: # # -- Write the names of two types of graphics objects that you can construct OTHER than Circle and Point: # Rectangular shape, ellipse #
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()
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(600, 400) point1 = rg.Point(100, 100) point2 = rg.Point(550, 50) point3 = rg.Point(300, 300) circle = rg.Circle(point1, 50) rectangle = rg.Rectangle(point2, point3) circle.fill_color('blue') rectangle.outline_color('red') print('Rectangle point 1 is ', rectangle.p1) print('Rectangle point 2 is ', rectangle.p2) radius = circle.getRadius() print('Circle radius is ', radius) print('Circle radius is ', circle.radius) circle.attach_to(window) rectangle.attach_to(window) window.close_on_mouse_click()
def example1(): """ Displays an empty window. """ window = rg.RoseWindow(500, 300, 'Example 1: An empty window') window.close_on_mouse_click()
def 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: Consider 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. # ------------------------------------------------------------------ title = 'Tests 1 and 2 of draw_circles_from_rectangle: ' title = title + ' 8 blue in row, 3 in column; then 4 green in row, 5 in column' window1 = rg.RoseWindow(720, 500, title) # Test 1: rectangle = rg.Rectangle(rg.Point(500, 400), rg.Point(600, 450)) rectangle.fill_color = 'blue' rectangle.outline_color = 'red' rectangle.outline_thickness = 3 draw_circles_from_rectangle(8, 3, rectangle, window1) # Test 2: rectangle = rg.Rectangle(rg.Point(400, 250), rg.Point(440, 325)) rectangle.fill_color = 'green' rectangle.outline_color = 'black' rectangle.outline_thickness = 5 draw_circles_from_rectangle(4, 5, rectangle, window1) window1.close_on_mouse_click() # ------------------------------------------------------------------ # A third test on ANOTHER window. # ------------------------------------------------------------------ title = 'Test 3 of draw_circles_from_rectangle: ' title += ' 6 yellow-filled row, 10 brown-outlined column' window2 = rg.RoseWindow(620, 380, title) # Test 3: rectangle = rg.Rectangle(rg.Point(350, 280), rg.Point(375, 330)) rectangle.fill_color = 'yellow' rectangle.outline_color = 'brown' rectangle.outline_thickness = 5 draw_circles_from_rectangle(6, 10, rectangle, window2) window2.close_on_mouse_click()