Programming 2022/23 — Assignment 2
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
Programming 2022/23 — Assignment 2
December 12th 2022
1 About the assignment
You will write Python functions to make various calculations and draw various plots related to the geometry of circles in the plane.
To support your programming, you will also need to do some mathematics.
Your mark for the assignment will be based on both the programming and mathematical parts. This is a programming module and so the programming is the main part of this assignment. Nonetheless, where there are relevant mathematical tasks, the idea is that your solution to a programming task should be based on them. Therefore, a student who submits only the programming part without corresponding maths may receive a mark of zero for any tasks for which a mathematical part was requested. Tasks where some maths is expected have “(Math)” after the task number.
It’s important that you test your functions before submitting them. No check will be made at the point of submission to the Moodle server. Therefore, pay close attention to examples given in relation to the earlier tasks and make sure that your functions work for those.
1.1 Rules
The assignment should be your own work without help from others. You may access all Python documentation that was permitted for the first assignment (see the information on Ultra). You may also view web pages referenced below (just click on a link that is formatted as underlined green text ) and may look up online or in books the meaning of technical terms used in the assignment. You should not seek further assistance from books or online sources.
You may import the math and numpy modules or individual items from those modules. You may also make the usual import of the pyplot part of matplotlib as plt and this is assumed in examples given below. Other imports are not allowed, specifically not other parts of matplotlib such as matplotlib.patches and matplotlib.collections.
When plotting, it is expected that you will use only plt.plot in the functions you submit as your solutions. You should not use other parts of matplotlib. However, when testing your code you will clearly want to use plt.show. Moreover, your circles will then tend to look more like circles if you also include plt.gca().set_aspect('equal', 'box') before plt.show() when testing.
1.2 Submission of answers
Your submission will consist of two files:
• A single file of Python code that contains only imports, function definitions and comments. This should be submitted to the Moodle server. Functions may call each other and you may include additional functions to support the ones required as solutions to the assignment. Feel free to include comments and to leave
commented-out code or commented-out print statements as examples of how your functions would be used. Do not call plt.show() in functions that are submitted as solutions to tasks.
A Python script will made available for you to test that you have given your
functions the correct names and the correct number of arguments . Details of where
to find the script and how to use it will be provided in an Ultra announcement when
the submission system is opened to you.
• A single PDF file of your mathematical work, clearly identifying to which task each part of your file relates. This should be submitted to Gradescope having followed the link from Ultra.
It will probably not be possible to submit either file until after Christmas.
1.3 Difficulty of the tasks and time required
The tasks are ordered in a way that seems natural for the material and this also is intended
to be in order of increasing challenge.
Tasks 1 to 6 are deliberately clearly sign-posted. Tasks 7 to 9 require increasing
independence and tasks 10 to 12 are intended to challenge.
Only you can decide how much time to spend on the assignment between now and the
deadline. However, I do not expect you to spend days on this assignment or to make yourself miserable trying to answer questions for which you are not ready. The more challenging tasks are provided to stimulate and provide interest for students who have been enjoying programming.
1.4 Marking
I have not decided a mark scheme yet and will not finalise the number of marks per task
until have done most of the marking. However, my inclination is to award about half the marks for the more straightforward tasks and to divide the remaining marks roughly equally between the more challenging tasks.
Some marks will be reserved for the quality of plots produced, for example for sensible use
of colour to distinguish different objects appearing in a plot. Marks may also be reserved for quality of code. In particular, there is a certain hierarchy to some of the tasks where a function written for an earlier task would naturally be called for a later task and doing so may be rewarded in marking.
2 The assignment
On practical sheet 3, you saw that a point in the plane may be represented in Python as a
list of two numbers: for example [−0.3, 1.2] represents the point ( −0.3, 1.2) . This representation will be used throughout what follows.
2.1 Working with colours
In what follows, you will need to write functions to add circles and line segments to
matplotlib plots in specified colours. A colour can be specified in several different ways described in the matplotlib tutorial on colours. The two easiest are by name (see the “xkcd color survey” link from the tutorial) or as an RGB colour by providing a list of three numbers each between 0 and 1 (the amounts of red, green and blue light to include in the colour). If you don’t know what an RGB colour is, see the Wikipedia article about RGB colours.
Here is an example of how to write a function to draw something in a specified colour:
def draw_segment (xy1 , xy2 , thecolour ):
plt .plot ([xy1 [0] , xy2 [0]] , [xy1 [1] , xy2 [1]] , color=thecolour )
draw_segment ([0 ,0] , [1 ,1] , 'pink ')
draw_segment ([ −1 ,3] , [2 , −2] , (1 , .5 ,0)) # Orange line
draw_segment ([ −2 ,0] , [2 , 0 .5] , (0 ,0 ,1)) # Blue line
plt .show ()
The example shows the definition and use of a function to add the line segment connecting
points xy1 and xy2 to a matplotlib plot where the colour of the segment is specified by the
thecolour argument to the function. The thecolour argument to the function is passed on to plt.plot using the named argument color that can be included in a call to plt.plot.
2.2 Drawing a circle in the plane
2.2.1 The unit circle (centered at the origin)
The unit circle in the plane is the circle of radius 1 centered at the origin. Points (x, y) on the unit circle satisfy the equation x2 + y2 = 1 and we can compute pairs of points using the equations x = cos θ and y = sin θ where θ ranges from 0 to 2π .
Task 1 (Python): Write a function draw_unit_circle(thecolour) that adds the unit circle to a matplotlib plot with the colour of the circle specified by the thecolour argument. Here is an example of how it could be used (test your function using this example):
draw_unit_circle ( 'red ')
plt .gca () .set_aspect ( 'equal ' , 'box ')
plt .show ()
2.2.2 A general circle
Consider the circle of radius T centered at point (X, Y ) in the plane.
The centre of the circle is a point in the plane and can therefore be represented as a list of
two numbers. A circle may therefore be represented as list with two elements: the first element is the center of the circle and the second element is the radius of the circle. For example [ [−0.3, 1.2], 2.3 ] represents the circle of radius 2.3 centered at ( −0.3, 1.2) .
Task 2a (Math): Write down the equations for points on the general circle corresponding to equations given in section 2.2.1 for the unit circle centered at the origin.
Task 2b (Python): Write a function draw_circle(circ, thecolour) that adds the circle represented by circ to a matplotlib plot, drawn in colour thecolour. Here is an example showing how the function might be used:
circ1 = [ [2 ,1] , 0 .5 ]
circ2 = [ [ −0 .5 ,1 .6] , 1 ]
draw_circle (circ1 , 'red ')
draw_circle (circ2 , [0 ,0 ,1])
plt .gca () .set_aspect ( 'equal ' , 'box ')
plt .show ()
Task 3 (Python): Test your function by writing a Python program that draws six circles each of radius 1/2 with centres given by (cos (jπ/3), sin(jπ/3)) for j = 0, 1, . . . , 5 . When submitting your solution, this program should be in the form of a function show_six_circles_example(). Note that this function has no arguments (like the functions you submitted for quiz 8).
2.2.3 Adding a tangent line
Given any circle and a point (x, y) on the circle, there is a unique line through the point that is a tangent to the circle.
Task 4a (Math): for the point (x, y) on the circle with radius r centered at (X, Y ), find the equation of the tangent line through the point. Hint: the tangent line is orthogonal to the diameter of the circle passing through the point (x, y) .
Task 4b (Python): Write a function draw_tangent(circ, xy, d, thecolour). Here circ represents a circle, xy a point (x, y) that should lie on the circle and d the length d of the tangent line segment to draw.
(a) The function first checks whether or not the point xy lies on the circle represented by
circ:
• if you know about Python “exceptions”, your function should raise and exception if the point does not lie on the circle;
• otherwise (you don’t know about Python “exceptions”), your function should return False without drawing anything if the point does not lie on the circle.
In checking whether or not the point lies on the circle, you should bear in mind the
limitations of floating point arithmetic.
(b) When the point lies on the circle, the function draws a tangent line segment of length d in the specified colour. The point of contact with the circle should be the midpoint of the line segment.
This function should not draw the circle, just the tangent line.
Task 5 (Python): Test your functions produced so far by writing a Python program that produces a plot showing the circle of radius 2 centered at ( −1, 3) and the tangent line segment of length 4 to the circle at the point (0.2, 4.6) . When submitting your solution, this program should be in the form of a function show_tangent_example().
2.3 Intersecting circles and radical axes
If two nonconcentric circles in the plane intersect, they do so either at one point (they are tangent to each other) or at two distinct points. In the latter case, the line through those two points is known as the radical axis.
2.3.1 Circles with centres on the x-axis
Consider two circles of radius r and R respectively and with corresponding centres (0, 0) and (c, 0) .
Task 6a (Math): Determine a condition on r , R , and c that determines whether or not the circles intersect at two points. For situations in which they do so intersect, determine the coordinates of the points at which they intersect.
Task 6b (Python): Write a function xcircles_and_radicalaxis(r, R, c, thecolours) that adds the two circles to a matplotlib plot. In cases where they intersect at two distinct points, it should also draw the segment of the radical axis connecting the points of intersection. thecolours is a list of three colours to be used for the two circles and the radical axis segment.
2.3.2 General circles
Task 7a (Math): generalise task 6a to circles with arbitrary centres in the plane. You can do so in more than one way: brute force solution of the more general equations or by thinking about translating and rotating the plane to take advantage of your mathematics in section 2.3.1.
Task 7b (Python): Write a function
gencircles_and_radicalaxis(circ1, circ2, thecolours) that adds two general circles represented by circ1 and circ2 to a matplotlib plot and adds the radical axis segment where appropriate. As in task 6b, thecolours specifies the colours to be used.
2.3.3 Radical axis theorem
Suppose that we have three circles such that no two of them are concentric and each pair
intersects at two points. The radical axis theorem states that the three radical axes (one for each pair of circles) either intersect in one point called the radical center or are parallel.
Task 8 (Python): illustrate the radical axis theorem for the following three circles: radius 1 centered at (1, 0), radius 1.5 centered at (0, 1) and radius 2 centered at (1.5, 1.5) . When submitting your solution, this program should be in the form of a function show_radicalaxes_example().
2.4 Circular “billiards”
The following material explores aspects of geometry that might be considered relevant to
playing billiards on a circular billiard table. But don’t worry: you don’t need to know what billiards is or how to play it in order to do this problem!
We will work with the unit circle centered at the origin. Given a starting point inside the
circle and a direction of initial movement, we can construct a track as follows.
• We move in a straight line until we hit the circle.
• When we hit the circle, we bounce and again move in a straight line until we the circle at another point.
• Again when we hit the circle, we bounce and move in a straight line and the process continues in principle for ever.
• Each bounce is such that the angles of incidence and reflection are equal with respect to the normal to tangent line at the point where we hit the circle.
The basic idea is illustrated in the left panel of the picture. The panel shows the circle in
blue, the x and y axes as green lines, and in red the first three sections of the track starting from some point (a, b) heading initially in the direction at angle θ to the horizontal. The dashed line is parallel to the x-axis.
The detail of a bounce is shown in the right panel of the picture (the two angles labelled ϕ are the same).
Task 9a (Math and Python): write a Python function track(n, ab, theta) that returns a list of the first n points at which we hit the circle starting from point (a, b) inside the circle and moving initially in direction θ as shown in the picture. The argument ab represents the point (a, b) and theta represents the angle θ in radians (assume radians throughout the assignment). Your mathematical work should justify your Python computation. There are various approaches to this, some involving more maths and less
Python and some vice versa. For developing a more computational approach, some of the material on sheet 7 might prove useful.
Hints: (i) think about the relationship between the angles (relative to horizontal) corresponding to (a) the path before a bounce, (b) the normal to the tangent, and (c) the path after a bounce; (ii) the atan2 function from the math library is a nice way to obtain an angle corresponding to a vector.
Task 9b: Write also show_track(n, ab, theta, thecolour) that adds the track nicely to a matplotlib plot. Some marks for this subtask will be reserved for solutions that highlight the direction of movement in a nice way (the restriction on which parts of matplotlib may be used will be relaxed for this task but more marks will be given if you can manage to use only plt.plot)
Challenges (likely to involve Math as well as Python):
• Task 10: Write a Python function theta(ab, c) that determines an angle θ such that the track starting from (a, b) moving initally in direction θ passes through the point (0, c) after a single bounce (where c ∈ ( −1, 1)).
• Task 11: It can be shown mathematically that for each point (a, b) other than the origin, there are either 2 or 4 distinct angles θ that are possible answers to the previous bullet. Write a Python function ntheta(ab,c) that correctly returns either
2 or 4.
• Task 12: Write a function that, for given c ∈ ( −1, 1), indicates the region of points (a, b) for which there are 4 distinct angles θ . There are different approaches that might be taken here which is why I used the word “indicate”. The function should be named show_theta4, and will have at least one argument, corresponding to c; the function may take other arguments as you see fit. When marking, I will look to see how your function should be called but it will help if you include in a comment an example of how to call it.
2023-01-17