CPSC 231 - Introduction to Computer Science for Computer Science
Charting Expressions (Graphing Calculator) You will be creating a small graphical Python 3 program. This program will use the turtle library to draw based on information taken from the user. You should already have all the experience you need with this library from Assignment 1. The assignment requires an additional understanding of converting between Cartesian coordinate systems, using loops, and completing functions as outlined by someone else. You will be provided with a starter code that already implements the basic interaction with the user (something you learned in the previous assignment). This code consists of a completed main function, which sets up the window using a setup function. Neither of these functions should be changed. The main function makes use of several incomplete functions. Your assignment is to complete these functions so that the main function can use them to draw what the user requests. All your code should be written within the incomplete functions. You are allowed to create constants and new functions as long as they are not redundant to existing ones. There should be no global variables in your code (except if you are doing the bonus). There is a bonus that involves identify local minima/maxima and labelling them described at the end of the assignment. Your program will be drawing in an 800-pixel by 600-pixel window with (0,0) as the bottom left corner and (800,600) as the top right corner. The starter code has a setup() function, which setups up the window and returns the turtle drawing object pointer so you can use it. This pointer is passed into the existing starter code functions for you to use. The included starter code already prompts the user for the pixel coordinates for where the chart origin should be placed in the window. The code then prompts the user for a ratio of how many pixels are 1 step of the chart. For example, an origin of = (400,300) should be the centre of the screen. If the ratio of pixels per single step is 100 pixels per step. Then a position one step up from the origin point to (????, ????) = (0,1) in the charting coordinate system is (400 + 100 ? 0,300 + 100 ? 1) = (400,300 + 100) = (400,400). Below is an image of 3 curves drawn for a chart with an origin at 400,300 and a ratio of 30. The existing starter code uses the included (but incomplete) functions to draw the x and y axes in black, and then loops to get expressions from the user. The colour used to draw an expression is determined based on the number of previously drawn expressions. So, you must track this number using a counter variable. Your task is to complete the included functions, so they function in the way that the comments and assignment description require. You will likely find the visual examples of operation included in the assignment description helpful. I recommend approaching the functions in stages: Part 1: screenCoor: First, complete the coordinate conversion function screenCoor. This function uses the parameters that the main function obtains from the user, along with an (x,y) coordinate to perform the conversion. It returns this coordinate as a pixel screen coordinate following the math described above in the example. Use this function in your code any time you want to plot a chart location. You will lose marks if you duplicate the code functionality of this function somewhere else instead of calling it. Part 2: getColor: Second, complete the colour-determination function getColor. This function takes the counter tracked in the expression input loop and returns one of three different colours: red, green, or blue. You should find the remainder operator (also called modulus) helpful here because it allows you to change an infinitely increasing integer sequence into a range of integers. Part 3: drawXAxis, drawYAxis Third, complete the two functions that draw the x and y axes. These two functions are structurally similar, so complete one of them first then you can easily complete the other function. If you choose the x-axis, initially you can ignore updating the return values for xmin and xmax. You should be able to add these in later once you have the drawing working. When first drawing your axes, you can ignore the tick marks and labels. These can also be added in once you have the lines drawn successfully. My advice is to use two loops. Start at the chart origin (0,0) and use a loop to move 1 chart step away. Convert this point from a chart location into a screen location. Draw a line from the previous point to the current point. Once you draw to a location outside the screen, then stop looping. Go back to the origin and draw in the other direction. Once you can draw this line step by step, you should notice that you are stopping at each label/tick location. Make a call to the associated label/tick draw function included in the assignment. Then fill in the code of this function to draw the tick/labels. Once you complete the drawing part of the drawXAxis function, you will want to ensure the xmin and xmax hold the correct values when the function completes. These two values start at 0,0 for the chart origin location. You should update these values any time you draw at a chart x-location when drawing the axis. If the x-location is less than xmin update it, if it is greater than xmax then update it. You can use the min(),max() functions to do this, or simply use conditional statements if you desire. Part 4: drawExpr Finally, you will draw the actual expression that the user inputted. Python can evaluate a string using its eval function. For example, the following 3-line program is a simple calculator: expression = input("Enter an arithmetic expression: ") result = eval(expression) print("The result of that expression is", result) Python is also able to evaluate expressions which include a single variable x. The implementation of this function can be surprising at first. When the eval function is called, it uses whatever value is stored in the variable called x to evaluate the given expression. As a result, your program will need to include a variable named x, which represents the x coordinate in the Cartesian coordinate system. The value of x will change in a loop. Call eval inside of the loop so that you can compute the value of y for many different x values. For example, the following program evaluates an expression that includes the variable x for each integer value of x from 0 up to and including 5: expr = input("Enter an arithmetic expression: ") for x in range(0, 6): y = eval(expr) print("When x is", x, "the value of the expression is", y) You can use a similar technique to compute the y position of the curve for many different values of x. Those x and y values are what you need to draw the curve. You cannot complete this function correctly unless the previous functions are operational. Use the xmin and xmax that were determined in your previous function to loop through x-coordinates of the curve. You will need to pick a delta that makes the curve smooth. A delta of 0.1 or 0.2 should provide sufficient smoothness. Since you will be stepping through floating-point numbers, you will need to use a while loop instead of a for loop. Note: Two challenges you may have as you implement expression drawing are: (1) parts of the expression that exist outside the visible drawing window and, (2) discontinuities in expressions (Ex. tan(x)). For expressions outside the draw window, you are free to draw these parts of the expression, or implement conditionals that will avoid drawing them. For expressions with discontinuities, or undefined points, you can write your code under the knowledge they will either not be considered valid input, or if they are given as input, then the discontinuity does not have to be drawn. Ex. tan(x) will be drawn with a line connecting across the vertical discontinuity that exists at every odd multiple of ? ? .