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

Tools Window Help D introduction-to-programming-using python-y 2013-ww.pdf (page

ID: 3841481 • Letter: T

Question

Tools Window Help D introduction-to-programming-using python-y 2013-ww.pdf (page 227 of 582) "6.19 (Geometry: point position) Exercise 431 shows how to test whether apoint is on the left side of a directed line, on the right, or on the same line. Write the follow- ing functions: Return true if point (x2, y2) is on the left side of the directed line from (000, y0) to (x1, yl) def le Theline(x0, y0, x1, y1, x2, y2) Return true if point (x2, y2) is on the same line from (x0, y0) to (x1, y1) def onTheSameLine(x0, y0, x1. yl. x2, y2): Return true if point (x2, y2) is on the line segment from (x0, y0) to (x1, yl) def on TheLinesegmentoc0, y0, x1. yl. x2, y2): 208 Chapter 6 Functions write a program that prompts the user to enter the three points for po, pl. and and displays whether p2 is on the left of line from po to pl. on the right, on the the same line, or on the line segment. The sample runs of this program are the same as Exercise 4.31

Explanation / Answer

def leftOfTheLine(x0, y0, x1, y1, x2, y2):
    return True if (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) > 0 else False


def onTheSameLine(x0, y0, x1, y1, x2, y2):
    return True if (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) == 0 else False


def onTheLineSegment(x0, y0, x1, y1, x2, y2):
    if onTheSameLine(x0, y0, x1, y1, x2, y2):
        return True if min(x0,x2) <= x2 <= max(x0, x1) and min(y0,y2) <= y2 <= max(y0, y1) else False
    else:
        return False


def test():
    x0, y0, x1, y1, x2, y2 = eval(input("Enter coordinates for the three points p0, p1, and p2: "))
    if leftOfTheLine(x0, y0, x1, y1, x2, y2):
        print("on the left side")
    elif onTheSameLine(x0, y0, x1, y1, x2, y2):
        if onTheLineSegment(x0, y0, x1, y1, x2, y2):
            print("on the line segment")
        else:
            print("on the same line")
    else:
        print("on the right side")

test()

# 3.4,2,6.5,9.5,-5,4
# 1,1,5,5,2,2
# 3.4,2,6.5,9.5,5,2.5