Python代写:CPSC231 Charting Expressions

使用Python的Turtle库,实现一个图形化的高级计算器程序。

Turtle

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 (x, y) = (0,1) in the charting coordinate system.

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:

1
2
3
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:

1
2
3
4
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 .

Additional Specifications:

Ensure that your program meets all the following requirements:

  • You should have the class, your name, your tutorial, your student id, the date, and description at the top of your code file in comments. Marks are given for these.
  • The code file should be CPSC231A2[LastName].py (ex. Mine would be CPSC231A2Hudson.py)
  • Import the necessary libraries
  • Use constants appropriately. Your TA may note one or two magic numbers as a comment, but more will result in lost marks.
  • Use descriptive variable names. Using the same variable names that appear in mathematical equations is OK.
  • Draw your axes black, alternate your colours for curves through red, green, and blue.
  • Use in-line comments to indicate blocks of code and describe decisions or complex expressions.
  • Do not put any code outside the functions given. You can add functions, but none should duplicate functionality described in a current function. For example, don’t create functions to draw the labels/tick marks as two already exist.
  • Break and continue commands are generally considered bad form. As a result, you are NOT allowed to use them in this assignment. In general, their use can be avoided by using a combination of if statements and writing better conditions on your while loops.
  • You do not need to perform any error checking in your program. You may assume that the user always enters a valid expression or a blank line indicating that they want to quit. A valid expression contains x, operators, numeric constants and standard mathematical functions.

Bonus

Looking for an A+? Improve the program so that it marks every local minimum in orange and every local maximum in purple. Use a small circle to do this. Create a function that draws a circle for you around a given point. Make sure that your program does not mark inflection points, such as (0, 0) when graphing y = x ** 3 (and other functions that include one or more inflection points). Also, print the largest local minimum (global maximum) and lowest local minimum (global minimum) to the shell window for each expression entered (ignore infinite values). At the same time track the largest global maximum and global minimum across all expression entered. To track these values across expressions, please use global variables. These two variables should be the only global variables in your program. Print the updated values tracked for each of these after each expression is entered.

Examples

The following image shows the result of plotting three curves (w/ bonus). User input is shown in bold.

Enter pixel coordinates of origin: 400,300
Enter ratio of pixels per step: 30
Enter an arithmetic expression: x**2
Enter an arithmetic expression: sin(x)
Enter an arithmetic expression: 0.01*x**3+5*sin(x)
Enter an arithmetic expression: <Enter>

Grading

Part one of the assignment will be graded out of 12, with the grade based on the program’s level of functionality and conformance to the specifications. To get more than an F, your code must not have syntax errors. Code with runtime errors that occur during proper usage of the program, every time it runs, will not get better than a C grade. Runtime errors are your program crashing.

Your TA will begin by grading your code with a general functionality grade starting point and subtract marks when smaller specifications are unfilled.