""" University of Washington, Department of Geography Geography 465 Winter 2009 LAB ASSIGNMENT 1 Author: Michalis Avraam Basic Data Manipulation and Analysis (Python Fundamentals) You are given two tuples that represent a straight linear feature (line1, line2) You are asked to perform the following: 1. Identify the number of points in each line 2. Identify the equation of the two lines 3. Identify if the lines intersect 4. Print your results in the screen """ # ********************************************************************************* # # DATA PROVIDED - DO NOT ALTER # line1 = ((0.0, -1.0), (1.0, 1.0), (2.0, 3.0), (3.0, 5.0), (4.0, 7.0), (5.0, 9.0), (6.0, 11.0), (7.0, 13.0), (8.0, 15.0), (9.0, 17.0)) line2 = ((0.0, 2.0), (1.0, 1.0), (2.0, 0.0), (3.0, -1.0), (4.0, -2.0), (5.0, -3.0), (6.0, -3.0), (7.0, -5.0), (8.0, -6.0), (9.0, -7.0)) # ********************************************************************************* # # IDENTIFY THE NUMBER OF POINTS IN EACH LINE # # Python includes multiple tools that allow us to investigate data. # The simplest tool we can use in this case, is to measure the # elements in the tuple. This is performed with the len() function. # It accepts one argument and returns a number. If I need the length # of something, I can say: length = len("String to measure") line1Points = 0 line2Points = 0 # ********************************************************************************* # # CODE TO IDENTIFY EQUATIONS OF LINES # # Straight lines have an equation of the form y = mx + b, where x, y are # coordinates in space, m is the slope of the line and b is the intersect. # You need to identify the slope (m) and intersect (b) of the lines. # Slope is defined as the ratio of rise over run, or the difference in # the y-axis over the difference in the x-axis. # General procedure: # 1. Grab two points from line # 2. Find the difference in y-values # 3. Find the difference in x-values # 4. Divide the two to get your slope # 5. To find the intersect, solve the equations b: # y = mx + b -> b = y - mx # Since you have a point in the line and slope, solve to find b slopeLine1 = 0 interLine1 = 0 slopeLine2 = 0 interLine2 = 0 # ********************************************************************************* # # CODE TO IDENTIFY THE LINE INTERSECTION # # Two lines intersect when their equations have a solution in x,y that is the # same (meaning, there is a position they will both pass through). In order # to find that, we need to equate the two lines, once in the y-axis and once # in the x-axis. The results looks like the following: # line 1: y = m1x + b1 # line 2: y = m2x + b2 # Equating the two... (line 1 = line 2) # m1x + b1 = m2x + b2 # Solving the equation for x gives us their common x-coordinate # x = (b2 - b1) / (m1 - m2) # Knowing x, plug it in one of the two equations and you get a y. # The x, y pair is the point of intersection interX = 0 interY = 0 # ********************************************************************************* # # PRINT RESULTS TO USER IN A PRETTY WAY # # Make sure you print the following: # Line 1 Information : # Number of Points in line # Slope of line (m1) # y-intercept (b1) # Line 2 Information: # Number of Points in line # Slope of line (m2) # y-intercept (b2) # Line Intersection # x, y coordinates of line intersection