Analytical geometry
CPSC 231: Introduction to Computer Science for Computer Science Majors I Assignment 1: First Program/Analytical Geometry Weight: 6% 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. Cited material should never be used to complete core assignment specifications. You can and should verify and code you are concerned with your instructor/TA before submit. 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 exchange code with another student, write code while discussing it with a fellow student, or copy code from another person’s screen, then this code is not yours. 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 modelling code after another student's algorithm. You can not use (even with citation) another student’s code. 5. Making your code available, even passively, for others to copy, or potentially copy, is also plagiarism. 6. 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/). 7. 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. A common penalty is an F on a plagiarized assignment. Late Penalty Late assignments will not be accepted. Goal Writing a first program in Python using I/O, casting, expressions, and conditionals. Technology Python 3 Submission Instructions 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. Your assignment must be completed in Python 3 and be executable with Python version 3.6.8+. For graphical drawing you must use the Python turtle library. Description 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 (no other libraries are allowed for your math calculations). Your TA will introduce you to these libraries and their details during tutorials, so please attend the tutorials. 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, problem- solving, 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 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. 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. End your program with screen.exitonclick() so that the window stays open until it is clicked on. 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. 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 to change drawing colours. 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). (If you turn your pointer [turtle], then because the circle starting point is to the left of the turtle this will change how your circle is drawn https://docs.python.org/3.3/library/turtle.html?highlight=turtle ). Getting Input: 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 ???? and integer ???? for the centre (???? ,????) of the circle and a float ?? for the radius. You will need an integer ??1 and integer ??1 for the start (??1,??1) of the line and an integer ??2 and integer ??2 for the end (??2,??2) 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 (800,600) axis in the window [i.e., 300-300==0 and 300+300==600]. Analytical Geometry for Intersection: We have 7 values that you should have stored as input: ???? ,???? , ??, ??1,??1, ??2,??2. We will use these in our calculations. 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???? 2?? Before we jump directly to using this to calculate alpha (α) we can look at the value under the root (??2 − 4???? ) to determine the number of intersections. https://docs.python.org/3.3/library/turtle.html?highlight=turtle 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