Calculate if a point lies above or below (or right to left) of a line

15.8k Views Asked by At

If you are given two points $A$ and $B$ which both have $x,y$ values in $\mathbb{R^2}$. These points join to make a line.

You are then given a third point $P$, also in $\mathbb{R^2}$.

You are then asked to calculate whether $P$ sits above or below the line created by joining $A$ and $B$.

I have been researching this question over the past few days and it seemed that everyone was suggesting the solution of using the two vectors $\vec{AB}$ and $\vec{AP}$. You then calculate the determinant of these vectors as follows:

$$(B_x-A_x)(P_y-A_y)-(B_y-A_y)(P_x-A_x)$$

As far as I am aware this actually calculates the area of a triangle that is created by joning the three points (and extending the sides to meet).

If the answer is $0$ it means that $P$ is sitting on the line $\vec{AB}$. If the value is positive, it sits on one side of the line, if it is negative it sits on the other side of the line.

To find out what side of the line, you then create another point that you know is above or below the line (e.g. A(x, y+1) would sit above the line). You can then use the above equation again and find out if being above the line results in a positive or negative value and use that to figure out where $P$ sits.

This is great and all, but this question is going to be asked in an interview, and although I have studied it I think the reasoning behind this logic is a bit over my head to explain.

That is when I asked a friend how they would solve the same question.

They said that they would calculate the line equation from the two points (AB) into the format $y = mx + b$

You could then substitute the value $x$ into the equation, and rearrange the equation to find out what $y$ would need to be in order for $P$ to lie on the line (make the line equation = 0). If the $y$ value needs to be decreased it would mean that the point is above the line, if the $y$ value needs to increase that means that the point is below the line.

I understand the second solution a lot more in my own head, but I am a bit worried that it was not suggested as a solution in any of the threads I was looking at, and maybe I am overlooking something?

I appreciate any feedback or advice regarding this.

1

There are 1 best solutions below

0
On

See, first you write your equation in $ax+by+c=0$ form.Next,let $f(x,y)=ax+by+c$,now put $(x,y)=(0,0) \implies f(0,0)=c$(Doesn't it?).Now suppose you have to find $(a,b)$ lies above or below(origin side or non-origin side).For the same,find $f(a,b) $,if sign(+ve or -ve) of $f(a,b)$ is same as that of $f(0,0)$,it means the point $(a,b)$ lies in origin side(Now,I hope you can verify if it's above the line or below the line ).If sign of $f(0,0) $ is not same as that of $f(a,b)$ then $(a,b)$ lies in non-origin side.

PS:A line divides the Cartesian plane into two planes .By Origin side ,I mean- The plane containing origin .