Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Introduction to Python (Geometry: same line?) Exercise 6.19 gives a function for

ID: 3723856 • Letter: I

Question

Introduction to Python

(Geometry: same line?) Exercise 6.19 gives a function for testing whether three points are on the same line. Write the following function to test whether all the points in the points list are on the same line: def sameLine(points): Write a program that prompts the user to enter five points and displays whether they are on the same line. Here are sample runs: Enter five points: 3.4 2 6.5 11.5 2.3 2.3 5.5 5 -5 4 The five points are not on the same line Enter five points: 1 1 2 2 3 3 4 4 5 5 The five points are on the same line

Explanation / Answer


"""
python 3 is used for this code
concept:
if slope of all lines genrated from every point is same then we can say
that all points are on same line
if at any points the slope is different the point is not on same line
"""
def sameLine(points) :   
"""
pointX is list of X co-ordinates
pointY is list of Y co-ordinates
"""

pointX = [points[0],points[2],points[4],points[6],points[8]]
pointY = [points[1],points[3],points[5],points[7],points[9]]
  
"""print(pointX)
print(pointY)"""
flag = True
slopePrev = -999.000
# variable to store the previously generated slope  
# if previous slope and current slope matches
for x in range (0,4) :
for x1 in range (1,4) :
num =(pointX[x]-pointX[x1]) # numerator
den = (pointY[x]-pointY[x1])
if den != 0.0 :
slope = float(num)/den
if slopePrev == -999.00 :
slopePrev = slope
else :
if slopePrev != slope :
#comparingprevious slope with current slope
#if slope does not matches previous slope then return from the loop
print("The five points are not on the same line")
flag = False
return
if flag :
print("The five points are on the same line")


if__name__== "__main__"
print("Enter the five points(coordinates) ex. (1 1 2 2 3 3 4 4 5 5) ")
points= [float(x) for x in input().split(' ')]
sameLine(points)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote