Question

In: Computer Science

Write a python function that will return the total length of line that passes through any...

Write a python function that will return the total length of line that passes through any number of provided points ( (x,y) ). The points should be passed as individual tuples or lists. The function should also have a parameter (True or False) to indicate whether the line should start from the origin, and that parameter should default to False. If True, the returned value should include the distance from the origin to the first point, otherwise start adding distances from the first point. So a function call could look something like this:

dist = lineLength((1,2), (2,3), (7,4), start=False)

Demonstrate it in your main program by calculating the length of line going through the following 5 points (with and without the origin option set to True):

(1,1), (-2,4), (-3,-2), (2,-1), (1,1)

Solutions

Expert Solution

import math

def findSlope(A,B):
  slopeVal = (B[1]-A[1])/B[0]-A[0]
  return slopeVal

def findSum(distance): # sum of all linesegments
  sumval = 0
  for k in distance:
    sumval += distance[k]
  return sumval

def findDist(point1,point2): # distance between 2 points
  return (math.sqrt((point1[0]-point2[0])**2 + (point1[1]-point2[1])**2))

def linelength (tupleList,start):
  distance = {}
  if start:
    for point in tupleList:
      slopeVal = findSlope((0,0),point)
      if slopeVal in distance.keys(): # if there are no points in the given list with calc slope add it in available slopes
        distance[slopeVal] = max(distance[slopeVal],findDist((0,0),point))
      else: # if multiple points are with same slope then pick the largest line segment that passes through point and lies in the given list
        distance[slopeVal] = findDist((0,0),point)
    return findSum(distance)
  else:
    for i in range(1,len(tupleList)):
      slopeVal = findSlope(tupleList[0],tupleList[i])
      if slopeVal in distance.keys(): # if there are no points in the given list with calc slope add it in available slopes
        distance[slopeVal] = max(distance[slopeVal],findDist(tupleList[0],tupleList[i]))
      else: # if multiple points are with same slope then pick the largest line segment that passes through point and lies in the given list
        distance[slopeVal] = findDist(tupleList[0],tupleList[i])
    return findSum(distance)
  return 0

def main():
  tupleList = [(1,1), (-2,4), (-3,-2), (2,-1), (1,1)]
  dist = linelength(tupleList,True)
  print(dist)
main()

Comment in case of any doubts, Please upvote


Related Solutions

Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the given wordlist  An anagram dictionary has each word with the letters sorted alphabetically creating a “key”.
Find the line that passes through the point (0, 1) and through the point of intersection...
Find the line that passes through the point (0, 1) and through the point of intersection of the two lines  and
Python pls 1. The function only allow having one return statement. Any other statements are not...
Python pls 1. The function only allow having one return statement. Any other statements are not allowed. You only have to have exactly one return statement. (ONE STATEMENT ALLOWED) For example the answer should be def hello(thatman): return thatman * thatman Create a function search_hobby. This function returns a set of hobby names and people's excitement level. input database = {'Dio': {'Lottery': 2, 'Chess': 4, 'Game': 3},'Baily': {'Game': 2, 'Tube': 1, 'Chess': 3}, 'Chico': {'Punch': 2, 'Chess': 5}, 'Aaron': {'Chess':...
Python pls 1. The function only allow having one return statement. Any other statements are not...
Python pls 1. The function only allow having one return statement. Any other statements are not allowed. You only have to have exactly one return statement. For example the answer should be def hello(thatman): return thatman * thatman 1. Create a function search_wholo function. This function returns a list of 2 tuples, which people and their excitement skills, and sorted by decreasing excitement level(highest excitement skill first). If two people have the same excitement level, they should appear alphabetically. The...
Python pls 1. The function only allow having one return statement. Any other statements are not...
Python pls 1. The function only allow having one return statement. Any other statements are not allowed. You only have to have exactly one return statement. For example the answer should be def hello(thatman): return thatman * thatman Create a function search_hobby. This function returns a set of hobby names and people's excitement level. input database = {'Dio': {'Lottery': 2, 'Chess': 4, 'Game': 3},'Baily': {'Game': 2, 'Tube': 1, 'Chess': 3}, 'Chico': {'Punch': 2, 'Chess': 5}, 'Aaron': {'Chess': 4, 'Tube': 2,...
*LISP PROGRAM* 2. Write a function that, given a list of lists, returns the total length...
*LISP PROGRAM* 2. Write a function that, given a list of lists, returns the total length of all the lists. This problem can be solved two different ways. 3. Write a program that prompts the user to enter two numbers and then outputs the sum of the two numbers. 4.Write ALLODDP, a recursive function that returns T if all the numbers in a list are odd.
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value...
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value pairs of the dictionary as tuples (key, value), reverse sorted by value (highest first) and where multiple keys with the same value appear alphabetically (lowest first).
how to write a code with python function trying to find minimum difference between any two...
how to write a code with python function trying to find minimum difference between any two elements in a list
Write a function, hexDigits that when passed an int array of any length greater than 0...
Write a function, hexDigits that when passed an int array of any length greater than 0 will print the corresponding hex digit for each int, one per line. The corresponding hex digit should be determined using a switch statement. The hex digits must be printed in uppercase and in order starting with the first entry. Each line of output should start with the array index of the number being written out, followed by a space, then the number, then another...
(a) Find symmetric equations for the line that passes through the point (5, −5, 6) and...
(a) Find symmetric equations for the line that passes through the point (5, −5, 6) and is parallel to the vector −1, 3, −2 . −(x − 5) = 3(y + 5) = −2(z − 6). x + 5 = y + 5 3 = z − 6 −2 .     x − 5 −1 = y + 5 3 = z − 6 −2 . x + 5 −1 = y − 5 3 = z + 6 −2 ....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT