$30
CPSC 231 Assignment 04
This assignment has one part: Part I – Analytical geometry (14 marks)
Part I - Analytical Geometry
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. You can start learning
about turtle graphics here. You can also find documentation about Turtle graphics here.
The assignment generally revises your concepts regarding constants, variables, expressions, as well as
importing and using libraries, getting input from users, casting variables to different types, problemsolving. It will also help you to develop an interactive application using graphics and drawing in a
coordinate system.
Your program will present an 800x600 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.
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). 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.
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.
Drawing in Turtle:
You can imagine the turtle drawing system as a turtle carrying a pen. The turtle starts at 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 to change
drawing colors. Draw the axis in black, the circle in red, the line in blue, and the intersect/text in
green.
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).
Getting Input:
Read the input from the user from command line (no command line argument). 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. 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.
Analytical Geometry:
We have 7 values that you should have stored as input: xc, yc, r, x1, y1, x2, y2. We will use these in our
calculations.
We will make use of 3 intermediate calculations to determine intersections.
These fulfill the parameters of the quadratic
We can solve this quadratic to determine intersections alpha ( ). However, we will not do this directly,
but instead using the quadratic formula.
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. Note that an alpha value greater than 1 or less than 0,
indicates that the intersection occurs outside the end points of the line segment. These intersections
should not be drawn.
If
then there are no intersections. We cannot use the imaginary number from square
rooting a negative number in this situation.
If
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
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
formulas
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.
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 a description
at the top of your code file in comments. Marks are given for these.
The code file should be <ID>.py
Import the necessary libraries
Use constants appropriately.
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.
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. So don’t worry about
input validation.
Example:
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
The output looks like:
Submission:
After you have completed this assignment, you should have written a total of one Python programs,
saved as .py files.
Use the University of Calgary Desire2Learn system (http://d2l.ucalgary.ca) to submit your assignment
work online. Log in using your UofC eID and password and find our course, CPSC 231 L01 and L02, in the
list. Then navigate to Assessments -> Dropbox Folders, and find the folder for Assignment #4 here.
Collaboration
Discussing the assignment requirements with others is a reasonable thing to do, and an excellent way to
learn. However, the work you hand-in must ultimately be your work. This is essential for you to benefit
from the learning experience, and for the instructors and TAs to grade you fairly. Handing in work that is
not your original work, but is represented as such, is plagiarism and academic misconduct. Penalties for
academic misconduct are outlined in the university calendar.
Here are some tips to avoid plagiarism in your programming assignments.
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
https://www.quackit.com/python/tutorial/python_hello_world.cfm.
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. Collaborative coding is strictly prohibited. Your assignment submission must be strictly your code.
Discussing anything beyond assignment requirements and ideas is a strictly forbidden form of
collaboration. This includes sharing code, discussing code itself, or modeling code after another
student's algorithm. You cannot use (even with citation) another student’s code.
5. We will be looking for plagiarism in all code submissions, possibly using automated software
designed for the task. For example, see Measures of Software Similarity (MOSS -
https://theory.stanford.edu/~aiken/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.
Grading for Part I:
14 – Fulfilled assignment specification and solved the problems completely
12 – Appears to fulfill assignment part 1 spec
9 – Code has part of intersection work done, but obvious errors/incomplete
6 – Code draws axis, gets input, draws circle/line
3 – Code draws axis and gets input
1 - 2 Syntax error or barely written any code.
Credits:
A few problems were derived from problems originally posted by Dr. Jonathan Hudson from University
of Calgary.