Plotting Functions

Earlier in the course, you got to write some code to control a software “turtle” as it drew shapes onto an electronic canvas. T

c/c++代写,java代写,python代写,matlab代写,作业代写,留学生作业代写
Plotting Functions

Introduction

Earlier in the course, you got to write some code to control a software “turtle” as it drew shapes onto an electronic canvas. The software tool that let Python 3 become a turtle graphics environment for that exercise is a module called tkinter. tkinter is the Python front-end (interface) for Tk, a software “toolkit that provides a library of basic elements … for building a graphical user interface (GUI)” (Wikipedia). In this assignment, you’ll using tkinter more directly, and plotting math functions onto another GUI canvas.

Because this is probably very new for most of you, we’re giving you a head start by providing some code to get the tkinter graphics “engine” started, some code in a main() function, and a collection of functions, most of which are just stubs* that need to be made into useful code. In this assignment, you will see some things you may not have seen previously, specifically,

· optional parameters and named arguments

· functions as parameters

*A stub is a tiny piece of code standing in temporarily for as-yet-unwritten code.

What you’re given

Here’s a stripped-down version of the code you’ll need to complete:

from tkinter import *

from math import *

CANV_WIDTH = 750

CANV_HEIGHT = 250

def draw_axes():

"""Draws a horizontal line across the middle of the canvas, and a vertical

line down the centre of the canvas using tkinter's default line thickness

and colour.

"""

# Draw the x axis

width = w.winfo_width()

w.create_line(get_x(-width//2),get_y(0),get_x(width//2),get_y(0))

# Draw the y axis

height = w.winfo_height()

w.create_line(get_x(0),get_y(-height//2),get_x(0),get_y(height//2))

def get_x(x_val):

"""Maps a Cartesian-style x coordinate (where x is 0 at the window's

horizontal centre) onto the tkinter canvas (where x is 0 is at the left

edge). x_val is the Cartesian x coordinate. The units of measurement

are pixels.

"""

return 0 # replace this line

def get_y(y_val):

"""Maps a Cartesian-style y coordinate (where y is 0 at the window's

vertical centre, and in which y grows in value upwards) onto the tkinter

canvas (where y is 0 is at the top edge, and y grows in value downwards).

y_val is the Cartesian y coordinate. The returned value is the

corresponding tkinter canvas y coordinate. The units of measurement are

pixels.

"""

return 0 # replace this line

def plot_point(x,y,colour=’black’):

"""Draws a single pixel "dot" at Cartesian coordinates (x,y).

The optional colour parameter determines the colour of the dot.

"""

pass # replace this line

def plot_fn(fn,start_x,end_x,scale=20,colour=’black’):

"""Plots a function, y = fn(x), onto the canvas.


Parameters:


* fn is a function that takes a single number parameter and returns a

  number.



* start_x is the left-most x value to be passed to fn.


* end_x is the right-most x value to be passed to fn.



* scale (optional) is used as a multiplier in both the x and y directions

  to "zoom in" on the plot. It is also used to increase the number of x

  coordinates "fed" to the fn function, to fill in all the horizontal gaps

  that would otherwise appear between the plotted points. scale is

  particularly useful for showing detail that would be otherwise be lost.


* colour (optional) determines the colour of the plotted function.



Note: nothing bad happens if start_x, end_x, or any y value computed from

fn(x) is off the canvas. Those points simply will not be displayed.

(Note to the student programmer: This happens automatically. You don't

have to program it.)

"""

pass # replace this line

def square(x):

"""Returns the square of x"""

return x * x

def func_1(x):

"""A quadratic polynomial function (for testing)."""

return -3 * square(x) + 2 * x + 1

def func_2(x):

"""Your choice of a function"""

return 0 # replace this line

def func_3(x):

"""Your choice of a function"""

return 0 # replace this line

master = Tk()

master.title(‘Plot THIS!’)

w = Canvas(master,

width=CANV_WIDTH,

height=CANV_HEIGHT)

w.pack(expand=YES, fill=BOTH)

w.update() # makes w.winfo_width() and w.winfo_height() meaningful

def main():

draw_x_axis(); draw_y_axis()

plot_fn(sin,-20,20,40,'green') # sin() is defined in the math module

plot_fn(cos,-20,20,40,'blue')  # cos() is defined in the math module

plot_fn(square,-20,20,40,'red')

plot_fn(func_1,-20,20,40,'purple')

#plot_fn(func_2,-20,20,40,'brown')

#plot_fn(func_3,-20,20,40,'cyan')

main()

mainloop()

What needs doing

The comment for each function describes what that function needs to do. We’ve made your task somewhat easier by figuring out the parameters each function requires, but except for the functions that draw the x and y axes, and a couple of testing functions, all the others currently have code blocks consisting of the Python 3 placeholder instruction, pass, for those functions that don’t return values, and return 0 for those functions that do. Even the few functions you are given currently don’t do anything useful because they need values they’re not getting from the incomplete functions.

Once you get everything else working, uncomment the last two instructions in main() and make func_2() and func_3() into visually interesting functions. If you can’t see much evidence of these functions on the plot, it may be because the interesting parts of them don’t fit onto it at the scale that’s been given to them (40). Try lowering or raising that argument.

What to tackle first

Filling in the code for the get_x() and get_y() functions is a good place to start with this assignment, because they’re the key to making the rest of the program work. You will know you’re at least on the right track with those functions if you can get your axes to appear. You should use w.winfo_width() and w.winfo_height() in get_x() and get_y(), respectively, to determine the dimensions of the canvas, as is done in the draw_x_axis() and draw_y_axis functions.

The new stuff

· Optional and named function parameters:

You’ll have noticed that the function parameters colour and scale differ from the others in the way they’re written and used. They are examples of optional parameters, and also named arguments. Within the function definition, the identifiers for these parameters are followed by an equal sign, and then some value. That value (referred to as the default value) becomes the value of the parameter in the function, but only if the function was called with the corresponding function argument missing. Python allows these optional parameters to be skipped when the function is called. For example, if you wanted to call plot_fn(), and were happy with the default scale value (20), but wanted to change the plotting colour to orange, you’d call the function using something like this:

plot_fn(func_x,-20,20,colour=’orange’)

(That is, you’d leave out the scale entirely, but then name the colour argument.)

· Functions as parameters

You’ll also have noticed that the identifiers (names) of functions can be used as arguments to a function, and then the corresponding parameter – which becomes an alias of the function – can be called within the function. This is a handy thing to know when you want to process a number of different functions in more or less the same way, as we’re doing here.

What NOT to do

Don’t get too bogged down. Each of the functions can be written in a very few lines of code, but things like inverting the direction that the y access grows in and how scale works have to be thought through carefully.

How to tell when you’re done

When it is run, and except for the addition of your own func_2() and func_3(), your program’s output should look like this:

SHAPE * MERGEFORMAT

BONUS!

We will give you extra credit (1 whole mark above 100%!) in this assignment if you go above-and-beyond the requirements above by automating the adding of ticks (very short lines) at integer intervals along the x and y axes. To judge where the ticks should go, add two parameters, scale and tick_length to the draw_axes() function. In general, a scale value of n would translate to a tick of length tick_length appearing every n pixels on the x and y axes. if tick_length is greater than 0, each tick on the x axis must protrude down from the axis, and each tick on the y axis must protrude left from the axis, as in this partial screen capture (which depicts ticks at a scale of 40 and a tick_length of 4):

SHAPE * MERGEFORMAT

However, if tick_length is less than 0, each tick on the x axis must protrude up from the axis, and each tick on the y axis must protrude right from the axis.

If you decide to take us up on this bonus challenge, you will, of course, need to modify the call to draw_axes() in the main()function. Make sure you test it with a variety of integer arguments.

What to turn in

Your completed program, called “assignment_2.py.”

留学生作业代写,cs作业代写,cs代写,作业代写,北美cs作业代写,澳洲cs作业代写,加拿大cs作业代写,cs作业代写价格,靠谱cs作业代写,程序代写
WeChat