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

Exercise 4.31 shows how to test whether a point is on the left side of a directe

ID: 3841503 • Letter: E

Question

Exercise 4.31 shows how to test whether a point is on the left side of a directed line, on the right, or on the same line. Write the following functions: Return true if point (x2, y2) is on the left side of the #directed line from (x0, y0) to (x1, y1) def leftof The Line(x0, y0, x1, y1, x2, y2): #Return true if point (x2, y2) is on the same #line from (x0, y0) to (x1, y1) def onThe SameLine(x0, y0, x1, y1, x2, y2): #Return true if point (x2, y2) is on the #line segment from (x0, y0) to (x1, y1) def on TheLinesegment (x0, y0, x1, y1, x2, y2):

Explanation / Answer

To determine which side of the line from A=(x1,y1) to B=(x2,y2) a point P=(x,y) falls on you need to compute the value:-

d=(xx1)(y2y1)(yy1)(x2x1)

If d<0d<0 then the point lies on one side of the line, and if d>0 then it lies on the other side. If d=0 then the point lies exactly on the line.

def leftOfTheLine(x0, y0 x1, y1, x2, y2)

{

d = (x2-x0)*(y1-y0) - (y2-y0)*(x1-x0)

if (d < 0)

return true;

else

return false

}

def onTheSameLine(x0, y0, x1, y1, x2, y2)

{

d = (x2-x0)*(y1-y0) - (y2-y0)*(x1-x0)

if (d == 0)

return true;

else

return false

}

To determine whether a point lies on line segment between (x0,y0) and (x1,y1) we compute the distances among the points and verify.

def onTheLineSegment(x0, y0,x1, y1, x2, y2)

{

dist1 = sqrt((x2-x0)2+(y2-y0)2)

dist2 = sqrt((x2-x1)2+(y2-y1)2)

dist3 = sqrt((x1-x0)2+(y1-y0)2)

if(dist1+dist2 == dist3)

return true

else

return false

}