Python programming assignment:

CS 249 Assignment Two due 31 January at 1PM Spring 2017 Python programming assignment:   1

The goal of this project is to develop an object oriented program to graph curves in the plane. The object oriented design separates the representation of the curves from their display with tkinter.

The curves are given as a tuple of formulas for the coordinates of each point in the time parameter t. For example, the unit circle could then be given via the formulas ’cos(t)’ and ’sin(t)’, respectively for the coordinates x and y for a point (x,y) on the circle. If the script curve.py contains the class definition Curve, then an interactive session could go as follows:

>>> from curve import Curve

>>> circle = Curve(’cos(t)’, ’sin(t)’)

>>> circle(1.5)

(0.0707372016677029, 0.9974949866040544)

>>> print(circle)

(x = cos(t), y = sin(t))

>>> circle

(x = cos(t), y = sin(t))

Here are the formal requirements on the Curve class:

  1. An object is instantiated with two strings, which contain valid Python expressions in t. The following mathematical functions are allowed: sin, cos, tan, exp, and log. The formulas may also contain the mathematical constant pi, e.g.: as in ’cos(2*pi*t)’. You may assume that the input of the user is correct.

  2. The object is callable. Even if the input for t is an integer and the formulas would give a pair of two integers, the result must be a tuple of two Python floats.

  3. The string representation returns the definition of the curve, as a tuple, in the format as in the example above. The representation of the object is the same as the string representation.

The script curve.py also contains a main(). Running the script at the command prompt $ in a Terminal window could go as follows:

$ python3 curve.py

Give the formula for x(t): cos(2*pi*t)

Give the formula for y(t): sin(2*pi*t) Your curve: (x = cos(2*pi*t), y = sin(2*pi*t))

Give a value for t: 0.5

The point at 5.000e-01: (-1.000e+00,

1.225e-16). Give the number of points: 4 A list of 4 points:

(1.0, 0.0)

(0.5000000000000001, 0.8660254037844386)

(-0.4999999999999998, 0.8660254037844387)

(-1.0, 1.2246467991473532e16) $

The list of n points is computed via the evaluation of the formulas for the coordinates, at ti = itend/(n−1). In the example above, the value for tend is 0.5 and the value for n equals 4.

To graph the curve, for t in the range tstart, ..., tstop, we compute a list of n points on the curve, at equal distance from each other. The coordinates for the i-th point in the list are computed via the evaluation of the

formulas that define the curve at t = ti, where

The class Graph inherits from the class Curve. The UML diagram is drawn below:

H

When instantiating an object of the class Graph, the first two parameters are the formulas for the x and y coordinates of the curve. The third parameter is a Canvas object. The fourth parameter is the dimension of the canvas. The session at the left below produces the picture at the right.

Running the script graph.py at the command prompt (python3 graph.py) brings up the same picture as on the right above. Observe that the graph of the curve does not touch the boundary of the canvas. There is a horizontal and vertical margin (both on the top, bottom, left, and right) of ten pixels.

The class Graph extends the class Curve with two extra data attributes: a canvas object and the number of pixels in the horizontal and vertical direction of the canvas. The method draw() of the class Graph takes three parameters:

  1. tstart: the start value for t is by default zero.

  2. tstop: the stop value for t is by default one.

  3. 3. n: the number of points is by default 100.

The coordinates of the points on the curve need to be mapped to the coordinates on canvas. In particular, if for all points (x, y) computed on the curve, we have x ∈ [xmin, xmax] and y ∈ [ymin, ymax], then we have to compute coordinates on canvas (X,Y ), so X ∈ [10,d−10] and Y ∈ [10,d−10], where d is the dimension of the canvas widget (the value for width and height) and 10 is the size of the left, right, top, and bottom margin. The general formula to map x ∈ [a, b] to X ∈ [A,B] is

Python programming assignment:   2.

The origin of the canvas is at the top left corner. Instead of Y , use d Y when drawing.

Some important points:

  1. The solution consists of two files: the first file is the script curve.py and the second file is the script graph.py. You must define the class Curve in the script curve.py. The script graph.py contains the definition of the class Graph.

  2. Submit the two files curve.py and graph.py in an email attachment.

  3. Every function in your program must have a documentation string.

Python programming assignment:   3Moreover, provide a documentation string for each class and for each module.