Question: I have this assignment to do in pycharm.. I have part of it but I am having problems getting it to run. Name your PyCharm
I have this assignment to do in pycharm.. I have part of it but I am having problems getting it to run. Name your PyCharm project Lab1, such that the project code goes in the folder Lab1. Name the zipped-up folder Lab1.zip and submit it to Blackboard. Do not submit any rar files. The code for this program must be placed in the file compute_directions.py. The project folder name and Python file must be exactly as specified above. I do not grade programs that have syntax errors or ones that raise exceptions when running the program. Email me broken code all zipped up as you would to submit it. Do not submit broken code. Requirements Specification For this program, the input is two points in an xy-plane. Each point is specified by providing their x and y-coordinates: Point 1: (x1, y1) and Point 2: (x2, y2). The program will then: a) Compute the distance between the 2 points b) Determine the direction of travel from point 1 to point 2 The directions of travel should translate to either N, S, E, W, NE, NW, SW, SE, ENE, NNE, NNW, WNW, WSW, SSW, SSE, or ESE, as shown in Figure 1, with point P1 transformed to the origin. The mathematical knowledge required to do this is provided below with a thorough explanation, thus a lengthy document. There is no need to use Google for help with this assignment. Background Consider two cities on a map that have GPS coordinates describing their location and a road that directly connects them. Two features of this scenario are the distance between the cities and the direction of travel from the first city to the second city. In Project 4, we will be using a graph data structure to represent a group of cities in North Carolina and the roads between. This assignment abstracts the notion of cities and roads to points and lines in a plane. This enables us to use mathematical algebra and trigonometry equations to find the distances and directions. To travel along a straight line from any point on a plane to another, you could consider the direction to be some combination of North, South, East, and West. For instance, if you travel due North (or due South) from one point to another along a straight line, the line would be parallel to the y-axis. If you travel due East (or due West) from one point to another along a straight line, the line would be parallel to the x-axis. In other words: a) Due North: you are moving in positive y direction b) Due South: you are moving in negative y direction c) Due East: you are moving in positive x direction d) Due West: you are moving in negative x direction Figure 1: Directions from First Point at Center (origin) Determining the 16 Directions For this assignment, consider two points in an xy-plane, given their x and y-coordinates: Point 1: P1 (x1, y1) and Point 2: P2 (x2, y2), then a) Compute the distance between them b) Determine the direction of travel from P1 to P2 The possible directions of travel are N, S, E, W, NE, NW, SW, SE, ENE, NNE, NNW, WNW, WSW, SSW, SSE, and ESE. This divides up the 360 degrees of the circle into sixteen 22.5-degree segments, as shown in Figure 1, where the first point, P1, is at the center (red dot) and is summarized in Table 1. The lines between the first point, P1, at the center of the circle and the points on the circle perimeter labeled A to P form the boundaries of the directions. To find the direction of travel, you need to find an angle between the line from P1 to P2 and either a) a line parallel to the x-axis OR b) a line parallel to the y-axis The 360 degrees of the circle can be divided into 4 quadrants of 90 degrees each. To determine whether to use the x-axis or the y-axis as the other side of the angle, we must divide up the circle into four quadrants as shown in Figure 2. Each quadrant covers 90 degrees, with 0 degrees starting at the x-axis or y-axis, depending on the quadrant. The angles run from 0-90 degrees in a counter-clockwise direction within each quadrant. a) East to North: called Quadrant I b) North to West: called Quadrant II c) West to South: called Quadrant III d) South to East: called Quadrant IV Figure 2: Four Quadrants The easiest way to visualize and compute the angle is to transform the points (x1,y1) to (x1',y1') and (x2,y2) to (x2',y2') such that (x1',y1') is (0,0) and the two points are in the same relative position and distance from each other. This transformation is achieved by determining the values of a and b from the equation below: x1': x1 + a = x1' = 0 --> a = -x1 y1': y1 + b = y1' = 0 --> b = -y1 And then adding a and b to x2 and y2: x2': x2 + -x1 = x2' y2': y2 + -y1 = y2' Using these transformed points, the parallel lines are directly on the axes: a) East thru North: Quadrant I, uses positive x-axis b) North thru West: Quadrant II, uses positive y-axis c) West thru South: Quadrant III, uses negative x-axis d) South thru East: Quadrant IV, uses negative y-axis Table 1: Direction Boundaries Quadrant Point Angle From axis Bounding I A 11.25 + x-axis E ENE I B 33.75 + x-axis ENE NE I C 56.25 + x-axis NE NNE I D 78.75 + x-axis NNE N II E 11.25 + y-axis N NNW II F 33.75 + y-axis NNW NW II G 56.25 + y-axis NW WNW II H 78.75 + y-axis WNW W III I 11.25 - x-axis W WSW III J 33.75 - x-axis WSW SW III K 56.25 - x-axis SW SSW III L 78.75 - x-axis SSW S IV M 11.25 - y-axis S SSE IV N 33.75 - y-axis SSE SE IV O 56.25 - y-axis SE ESE IV P 78.75 - y-axis ESE E Each direction covers 22.5 degrees with the four main directions straddling two quadrants with 11.25 degrees in each. Table 1 and Table 2 summarize all this. Table 1 gives the angles at the boundary of the directions, taken within the respective quadrant. Table 1 lists the points of Figure 1 (A, B, , O, and P) that lie on the perimeter of the circle. The line between these points and the center point form the 22.5 degree boundaries at which the directions change. These boundary lines lie in one of the four quadrants, as noted in the table. The boundary lines intersect either the x-axis (+ or -) or y-axis (+ or -) forming the angle listed in the table. Table 2 gives the angle intervals within the quadrants for each of the directions. Example: The line containing Point F in Quad II forms a 33.75-degree angle with the positive y-axis and lies on the boundary between NNW and NW. So, in Quad II, the direction NNW covers the angle interval 11.25 to 33.75. Table 2: Direction Intervals Within Quadrants Quadrant Direction Angle Interval From Axis I E (00.00, 11.25] + x-axis I ENE (11.25, 33.75] + x-axis I NE (33.75, 56.25] + x-axis I NNE (56.25, 78.75] + x-axis I N (78.75, 90.00] + x-axis II N (00.00, 11.25] + y-axis II NNW (11.25, 33.75] + y-axis II NW (33.75, 56.25] + y-axis II WNW (56.25, 78.75] + y-axis II W (78.75, 90.00] + y-axis III W (00.00, 11.25] - x-axis III WSW (11.25, 33.75] - x-axis III SW (33.75, 56.25] - x-axis III SSW (56.25, 78.75] - x-axis III S (78.75, 90.00] - x-axis IV S (00.00, 11.25] - y-axis IV SSE (11.25, 33.75] - y-axis IV SE (33.75, 56.25] - y-axis IV ESE (56.25, 78.75] - y-axis IV E (78.75, 90.00] - y-axis Using the Law of Cosines to Find the Angles Trigonometry is used to analyze triangles and determine the value of unknown angles or sides, given the value of known angles or sides. The Law of Cosines can be used to find any angle of a triangle given that we know the length of each of the sides. In the Law of Cosines figure above, the side lengths are the lowercase letters and the uppercase letters are the angles directly across from the sides. With these formulas, the angles are in radians. We will be changing these to degrees in this assignment. Using the first formula, lets solve for the cosine of angle A: COS (A) COS (A) = (b2 + c2 a2) / 2bc Then the angle A in radians is A = ARCCOS [(b2 + c2 a2) / 2bc] If we assume that transformed P1: (x1, y1) = (0, 0) is at angle A and transformed P2: (x2, y2) is at angle B, then c is the distance between these points and the direction of travel can be determined using the angle A. However, the formula also needs the lengths a and b. In addition, the angle in radians must be changed to degrees when finding the direction. We need to form a triangle with these two points and another point, say P3: (x3, y3). The easiest way to do this is to form a right triangle. Since the direction is based on angles from the x-axis or the y-axis we can form a right triangle, with one side being the x-axis (East and West) and the other side being the y-axis (North and South). See Figure 3 below. This particular right triangle is setup so that side b is the x-axis, side a is parallel to the y-axis and side c is the path from point A to point B. In this case, we are going to compute the value of the angle at point A and convert it to the direction as you travel from point A to point B along the side c. This triangle is setup to determine a direction in quadrant I. See Figures 4-6 for the triangle setup for the other three quadrants. Figure 3: Right Triangle: Quadrant I Figure 4: Right Triangle: Figure 5: Right Triangle: Quadrant II Quadrant III Figure 6: Right Triangle: Quadrant IV Finding the Third Point Using the right triangles from Figures 3-6, let point A be transformed P1: (x1, y1) = (0, 0), point B be transformed P2: (x2, y2) and point C be P3: (x3, y3). The xy-coordinates for point C are based on those for point A and point B and depend on the quadrant in which point B resides relative to point A. These are a) Quad I and Quad III: x3 = x2, y3 = y1 = 0 b) Quad II and Quad IV: x3 = x1 = 0, y3 = y2 Table 3 gives the values of x3 and y3 for each quadrant. To use the Law of Cosines to find the angle, you need the lengths of all three sides of the triangle: a) Length a: side across from point A, whose angle we need to compute b) Length b: side across from point B c) Length c: side across from point C, the line from point A to point B Table 3: P3 XY-coordinates Based on Quadrant Quadrant x3 y3 x3 y3 I x2 y1 x2 0 II x1 y2 0 y2 III x2 y1 x2 0 IV x1 y2 0 y2 Since we have all three points of our triangle, then we can determine the lengths of the sides using the distance formula: Side c = SQRT [(x2 x1)2 + (y2 y1)2] Side c = SQRT [(x2 0)2 + (y2 0)2] Side c = SQRT [(x2)2 + (y2)2] Side a = SQRT [(x3 x2)2 + (y3 y2)2] Side b = SQRT [(x3 x1)2 + (y3 y1)2] Side b = SQRT [(x3 0)2 + (y3 0)2] Side b = SQRT [(x3)2 + (y3)2] Determining The Quadrant To determine the direction, you need to know the quadrant and the angle. To determine the quadrant, you need to know the relative positions between x1 and x2 and between y1 and y2. The direction is in a) Quadrant I: when x2 > x1 (x2 > 0) and y2 >= y1 (y2 >= 0) b) Quadrant II: when x2 <= x1 (x2 <= 0) and y2 > y1 (y2 > 0) c) Quadrant III: when x2 < x1 (x2 < 0) and y2 <= y1 (y2 <= 0) d) Quadrant IV: when x2 >= x1 (x2 >= 0) and y2 < y1 (y2 < 0) Use of the mathematics explained in this background material is shown in the System Design. If you do not understand this material a) Read it multiple times b) Take notes as you read it: draw the pictures yourself c) Contact your instructor System Design Create a PyCharm project called Lab1. Create a Python file called compute_directions.py with the methods below. Use the sample output as a guide for what to display. At the end of this System Design is a list of points in each direction that you can use to test all your code. # Sample output for point (3, 3) and (-8, 2) Enter x and y coordinates of two points: Point 1: (x1, y1) x1 = 3 y1 = 3 Point 2: (x2, y2) x2 = -8 y2 = 2 The distance between these points: 11.05 The direction traveled from the first point to the second: W The Methods To Code The main method is used to request input and to call the appropriate methods described below in order to report the distance and direction. Code main using the following algorithm. 1. Prompt the user to enter two points 2. Transform the points P1 and P2, placing P1 at (0, 0) by calling the function transform_points. 3. Find the quadrant by calling the find_quadrant method, passing in the x1, y1, x2 and y2 values entered by user. 4. Use the found quadrant to find pt 3: (x3,y3): For Quad I and Quad III: x3 = x2 and y3 = y1 For Quad II and Quad IV: x3 = x1 and y3 = y2 5. Compute length of three sides, by calling the distance method for each side with the appropriate parameteres. 6. Compute the angle A at point 1, by calling the comp_angle method, which uses the Law of Cosines. See ComputeAngles.java code. 7. Compute the direction by calling the comp_direction method 8. Display the distance and direction. Round the distance to two decimal places before displaying it. The ComputeAngles.java code shows you how to do this rounding. Code the other methods as follows: Method Parameters Description transform_points() return floats x1,y1,x2,y2 x1, y1, x2, y2 Return the transformed points for P! and P2, where P1 is at the origin (0, 0) distance() return float x1, y1, x2, y2' Return the distance from P1: (0, 0) to P2: (x2, y2) compute_angle() return float sideA_len, sideB_len, sideC_len Return the angle at P1: (0, 0) in degrees, where sideA is across from P1: (0, 0), sideB is across from P2: (x2, y2), sideC is across from P3: (x3, y3) compute_direction() return str angle, quadrant Return the direction of travel using the angle and quadrant. Code this using a dictionary, where the keys are tuples holding the angle slices (low, high) and the value is the direction. find_quadrant() return int x1, y1, x2, y2' Return the quadrant in which the direction of travel takes you from P1: (0, 0) to P2: (x2,y2). Quadrants: Q1: when x2 > 0 and y2 >= 0 Q2: when x2 <= 0 and y2 > 0 Q3: when x2 < 0 and y2 <= 0 Q4: when x2 >= 0 and y2 < 0 return 1, 2, 3, or 4 main() See above Points To Use For Testing Figure 7 shows a series of points in the x-y plane and superimposed on this grid is the directions circle from Figure 1. These points are labeled A-T. They are not related to the points in Figure 1. Table 4 lists these points with their distance from the origin and the direction from the origin. These can be used to test the program, assuming P1 is (x1=0, y1=0). Figure 7: Points in Each Direction Table 4: Points in Each Direction with Distance Quadrant Direction Point Point Distance I E A (10, 1) 10.05 I ENE B (8, 3) 8.54 I NE C (9, 9) 12.73 I NNE D (3, 8) 8.54 I N E (1, 10) 10.05 II N F (-1, 10) 10.05 II NNW G (-3, 8) 8.54 II NW H (-9, 9) 12.73 II WNW I (-8, 3) 8.54 II W J (-10, 1) 10.05 III W K (-10, -1) 10.05 III WSW L (-8, -3) 8.54 III SW M (-9, -9) 12.73 III SSW N (-3, -8) 8.54 III S O (-1, -10) 10.05 IV S P (1, -10) 10.05 IV SSE Q (3, -8) 8.54 IV SE R (9, -9) 12.73 IV ESE S (8, -3) 8.54 IV E T (10,-1) 10.05
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
