CPSC 231 Assignment 1
1. Cite all sources of code that you hand-in that are not your original work. You can put the citation
into comments in your program. For example, if you find and use code found on a web site, include
a comment that says, for example:
# the following code is from
Use the complete URL so that the marker can check the source.
2. Citing sources avoids accusations of plagiarism and penalties for academic misconduct. However,
you may still get a low grade if you submit code that is not primarily developed by yourself.
3. Discuss and share ideas with other programmers as much as you like, but make sure that when you
write your code that it is your own. A good rule of thumb is to wait 20 minutes after talking with
somebody before writing your code. If you find yourself exchanging code by electronic means,
writing code while sitting and discussing with a fellow student, typing what you see on another
person’s console, then you can be sure that “your” code is not substantially your own, and your
sources must then be cited to avoid plagiarism.
4. We will be looking for plagiarism in in all code submissions, possibly using automated software
designed for the task. For example, see Measures of Software Similarity (MOSS –
Remember, if you are having trouble with an assignment, it is always better to go to your TA and/or
instructor to get help, than it is to plagiarize.
Late assignments will not be accepted.
You must submit your assignment electronically. Use the Assignment 1 dropbox in D2L for the
electronic submission. You can submit multiple times over the top of a previous submission. Do not wait
until the last minute to attempt to submit. You are responsible if you attempt this and time runs out.
You will create a small graphical Python 3 program that draws a few shapes using the turtle library and
information taken from the user. You will also use the Python math library to do calculations. Your TA
will introduce you to these libraries and their intricate details during tutorials, so please attend the
The assignment requires you to understand constants, magic numbers, variables, expressions, as well as
importing and using libraries, getting input from users, casting variables to different types, problemsolving, and drawing in a coordinate system. It is possible to make use of functions for this assignment if
you understand them. However, if your program does not function correctly due to a failed attempt, you
will lose grades. There are no bonus grades for using functions. There are bonus grades for using loops,
which is discussed at the end of this document.
Your program will present an 800×600 pixel window with (0,0) being the bottom left corner point and
(800,600) being the top right corner point. Within this coordinate system, your program will draw an x
and y axes that identify the centre of the window. Your program will prompt the user for values that
define a circle and a line in this coordinate system and draw them. Finally, your program will perform
calculations to determine whether the line and circle intersect. At each of the points where the line
intersects the circle, you will draw a small circle to indicate the intersection point.
You can think of this assignment as a series of stages to complete. First, you import some libraries. Next,
you define constants (all upper case) that replace magic numbers. Some of these are obvious like the
window size. You will likely identify more constants as you write the rest of the program. Then setup
your window and carry out the rest of the assignment requirements. This code should get you started.
With your window ready, draw your axis. This axis should cross through the middle point of the screen,
i.e. (400,300). Remember to use your constants here and later on.
Then prompt the user for input, cast it to the proper type, and store it in variables. Remember to use
descriptive variable names. For this assignment, it is sufficient to adopt the same variable names used in
the mathematical equations below.
Draw your circle and your line.
Calculate the number of intersection points between the circle and line. Then, produce a conditional
statement that lets you display a message indicating the number of intersection points to the user.
Calculate the location of each interaction point (if any) and draw a circle around it.
Drawing in Turtle:
You can imagine the turtle drawing system as a turtle carrying a pen. The turtle starts in some default
location within the window and has its pen pressed down and ready to draw. To draw a line from the
turtle’s location to a new point, tell the turtle to move to the new location. It will drag the pen along as
it moves to the new location effectively drawing a line. If you tell the turtle to lift up the pen then tell it
to move to a new point, it will go to this new point without drawing the line.
For this assignment, you must know how to position the turtle in a specific location (a point in the
coordinate system, also known as pixel location), how to start and stop drawing, and how how to
change drawing colours. Draw the axis in black, the circle in red, the line in blue, and the intersect/text
You can use the circle command to draw the circle. The circle command, by default, draws circles from
the middle of the bottom edge. Therefore, to draw a circle around a centre point, you must determine
where the proper start location is (hint: adjust from the centre using the radius).
Read the input from the user using the techniques that we have discussed in class. Display a prompt in
the terminal window with a print statement, or by providing a parameter to the input function. Note
that the user will enter their input in the terminal window, not in the graphics window.
You will need to prompt for 7 and only 7 inputs (unless you are doing the bonus). You will need an
integer xc and integer yc for the centre (xc,yc) of the circle and a float r for the radius. You will need an
integer x1 and integer y1 for the start (x1,y1) of the line and an integer x2 and integer y2 for the end
(x2,y2) of the line. These are pixel locations in the window. A circle with middle (400,300) and radius 300
would be drawn in the middle of the window touching the top and bottom of the window.
We have 7 values that you should have stored as input: xc, yc, r, x1, y1, x2, y2. We will use these in our
We will make use of 3 intermediate calculations to determine intersections.
?? = (??2 − ??1)2 + (??2 − ??1)2
?? = 2[(??1 − ????)(??2 − ??1) + (??1 − ????)(??2 − ??1)]
?? = (??1 − ????)2 + (??1 − ????)2 − ??2
These fulfill the parameters of the quadratic
?? ??2 + ?? ?? + ?? = 0
We can solve this quadratic to determine intersections alpha (??). However, we will not do this directly,
but instead using the quadratic formula.
?? = −?? ± √??2 − 4????
Before we jump directly to using this to calculate alpha (??) we can look at the value under the root to
determine the number of intersections.
If ??2 − 4???? < 0 then there are no intersections. We cannot use the imaginary number from square
rooting a negative number in this situation.
If ??2 − 4???? = 0 then there is 1 intersection. The square root value is the root of 0. The result of adding
and subtracting 0 is the same number. So there is only one result to the alpha.
If ??2 − 4???? 0 then there are 2 intersections. Since the value is positive when add or subtract the
result of rooting it, we will produce two different alphas; one for each intersection.
After making a decision based on this value, we need to draw the result of the intersections. If there are
no intersections, you should draw the words “No Intersect!” in the centre of the window. If there is one,
or two intersections, you will should then calculate the alpha of the quadratic formula for each.
With each alpha, we can calculate the coordinates of the intersection point using the following two
?? = (1 − ??) ??1 + ?? ??2
?? = (1 − ??) ??1 + ?? ??2
With this coordinate, you should draw a circle around this location. Using a radius of 5 (constant) is a
good size. In effect the alpha is a ratio spot along the line between (x1,y1) and (x2,y2). An alpha of 0 is
the point (y1,x1) and an alpha of one is the point (x2,y2).
One final and very important point: although we may calculate an alpha that indicates that there is an
intersection, this intersection may not be between the two points of the line. The calculation we have
used so far also gives us intersections if line continue into infinity off either end. To ensure we only plot
intersections between the two points we need to make sure that the alpha greater than or equal to zero
and less than or equal to 1.
Ensure that your program meets all the following requirements:
• You should have the class, your name, your tutorial, your student id, the date, and a description
at the top of your code file in comments. Marks are given for these.
• The code file should be CPSC231A1<LastName.py (ex. Mine would be CPSC231A1Zaamout.py)
• Import the necessary libraries
• Use constants appropriately. Your TA may overlook one or two magic numbers, but more will
result in loss of marks.
• Use descriptive naming of variables. The use of variables that follow the math descriptions given
are fine for those purposes.
• Draw your axis black, your circle red, your line blue, and your intersection circles/text green.
• Use descriptive prompts for input and cast input to the indicated types.
• Use in-line comments to indicate blocks of code and describe decisions or complex expressions.
• No penalty for use of functions; but avoid adding unrequested functionality to the assignment.
• You do not have to worry about making sure input is of the correct type. Your program should
crash if the values given cannot be cast from strings into the request types.
Add a loop to your program that will prompt for additional pairs of coordinates that indicate the start
and end of more line segments to be drawn. The program should continue prompting for these
coordinates until the user enters a blank input (presses Enter without typing). Plot each line and circle
the intersection points. Do no refresh the drawing; just draw each line over top of the existing drawings.
You do not have to worry about old “No Intersect!” messages on the screen; just let them stay drawn.
Each time a new line is drawn, print to console the total of how many intersections have been found so
On the course website are 5 sample files for testing your program. Even if they all look correct, this does
not guarantee your program is fully correct or that your program will get a perfect grade. Each of these
files contains a sequence of 7 inputs cx, cy, r, x1, y1, x2, y2 on a separate line of the file. You can type in
these entries to get the picture that corresponds below.
In Linux, windows cmd shell (not power shell), and OSX we can push these into the Python program as if
we were typing input with the following:
python CPSC231A1LastName.py < sample1.dat
If you are using CPSC machines, make sure you are using the python command associated with the
correct version (version 3 or higher). You may have to use the following command on CPSC machines:
python3 CPSC231A1LastName.py < sample1.dat
Example input in the terminal window may looks as follow:
Enter circle x coordinate:400
Enter circle y coordinate:300
Enter radius of circle:200
Enter line start x coordinate:100
Enter line start y coordinate:200
Enter line end x coordinate:700
Enter line end y coordinate:200
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. The program must not have syntax errors to get
more than an F grade. A program with runtime errors that occur during proper usage of the program,
every time it runs, will not get better than a C grade.
Your TA will begin by grading your code by determining the general functionality of the code and
assigning a representative grade as a starting point. The TA will then subtract marks for mistakes and
incomplete requirements, such as not commenting your code adequately or at all!
The total mark achieved for the assignment will be translated to a letter grade using the following table:
Mark Letter Grade Starting Point Guidelines (not a final grading scheme!)
13 A+ Appears to fulfill assignment and bonus spec
12 A Appears to fulfill assignment spec
11 A10 B+
9 B Code has part of intersection work done, but obvious errors/incomplete
8 B7 C+
6 C Code draws axis, gets input, draws circle/line
5 C4 D+
3 D Code draws axis and gets input
1-2 F Syntax errors or barely started code