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

PYTHON! Exercise 3 - Total Line length Write a python function that will return the total...
PYTHON! Exercise 3 - Total Line length 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...
IN PYTHON Write a function called check_inventory(inventory, low) that passes a dictionary as inventory and low...
IN PYTHON Write a function called check_inventory(inventory, low) that passes a dictionary as inventory and low as an integer, and returns a sorted list of items that are below an inventory level that is given by thelowinteger parameter. The function should work without the low parameter supplied - in which case, you should assume low is 5. You do not have to worry about reading and writing to a file, that code is provided, and you don’t have to change...
(IN PYTHON) Write a function that accepts a line of text and a single letter as...
(IN PYTHON) Write a function that accepts a line of text and a single letter as input (case insensitive) and returns the number of times the letter is the last character of a word. Note your program should be able to handle different cases. And check if the user input is a single letter.
Write a Python function that will return the index of an element in a list.1- The...
Write a Python function that will return the index of an element in a list.1- The function will receive a list of at least 5 numbers as a single argument when the function is called. 2. The function will ask the user to input a number and will find and return the index of that number in the list.3. If the number is not an element of the list, the function returns the string "unknown."
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
Write a program function code in Python that prompts the user to enter the total of...
Write a program function code in Python that prompts the user to enter the total of his/her purchase and add 5% as the VAT. The program should display the total without and with VAT. ( that mean i should ask the user to enter the number of their item " so i think that i should use range" then print the result with and without the VAT )
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”.
In Python write a function that simulates the throwing of any even numbered dice which will...
In Python write a function that simulates the throwing of any even numbered dice which will be passed on as a parameter: customDice(sides). The function will return a random number rolled for that particular type of dice. Write the program that will utilize the customDice function with the following specification: Ask the user to pick which even-sided dice to use. The program will then roll the dice twice. If the total of both rolls is greater than the number of...
Part 1: Write a Python function called reduceWhitespace that is given a string line and returns...
Part 1: Write a Python function called reduceWhitespace that is given a string line and returns the line with all extra whitespace characters between the words removed. For example, ‘This line has extra space characters' 'This line has extra space characters’ • Function name: reduceWhitespace • Number of parameters: one string line • Return value: one string line The main file should handle the file operations to read from a .txt file you create and call the function from the...
*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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT