Calculate on which side of a straight line is a given point located?

To determine which side of the line from $A=(x_1,y_1)$ to $B=(x_2,y_2)$ a point $P=(x,y)$ falls on you need to compute the value:- $$d=(x-x_1)(y_2-y_1)-(y-y_1)(x_2-x_1)$$ If $d<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 line.

To see whether points on the left side of the line are those with positive or negative values compute the value for $d$ for a point you know is to the left of the line, such as $(x_1-1,y_1)$ and then compare the sign with the point you are interested in.

Addendum:

For completeness an explanation as to how this works is as follows:

The direction of the line $AB$ can be defined as $<x_2, y_2> - <x_1, y_1> = <x_2 - x_1, y_2 - y_1>$

The orthogonal (perpendicular) direction to that line will be $\vec n=<y_2-y_1, -(x_2 - x_1)>$ (we flip the x's and the y's and negate one component i.e (y's, -x's)). You can verify this is orthogonal by taking the dot product with the original value and check it's 0).

One possible vector going from the line to the Point $P$ is $D = P-A = <x-x_1,y-y_1> $

This vector $D$ is made of 2 components, a component $D^{\parallel}$ that is parallel to the line $AB$ and a component $D^{\bot}$ that is perpendicular to the line $AB$. By definition $D^{\parallel} \cdot \vec n = 0$. Since a direction parallel to the line will be perpendicular to the normal of that line.

We are interested in knowing whether the point is on the side the normal points to, or the side the normal points opposite to. In other words, we want to know if $D^{\bot}$ is in the same direction as $\vec n$ or not.

This is essentially the sign of the dot product of $D$ and $\vec n$ since $D \cdot \vec n = (D^{\bot} + D^{\parallel}) \cdot \vec n = D^{\bot} \cdot \vec n$

Arithmetically: $$<x-x_1, y-y_1> \cdot <y_2 - y_1, -(x_2 - x_1)>$$ $$=(x-x_1)(y_2 - y_1) - (y-y_1)(x_2 - x_1)$$

In short, we are calculating the signed shortest distance from the line $AB$ to the point $P$ then evaluating the sign of that distance.

Diagram for clarity: enter image description here


If the point is given by the 2D vector $\vec{P}$ and the line end point by $\vec{A}$ and $\vec{B}$, then calculate the cross product: $$\vec{AB}\times\vec{AP} = x_{AB}\cdot y_{AP} - x_{AP}\cdot y_{AB}$$ $$x_{AB} = x_B - x_A,\ x_{AP} = x_P - x_A\ ...$$ The sign of the above will determine what side of the line your pixel is on.


An alternative derivation for the same formula is the dot product of $\vec{AP}$ and a vector perpendicular to $\vec{AB}$. Given $A=(x_1,y_1)$ and $B=(x_2,y_2)$, define vector $V$ perpendicular to $\vec{AB}$ as $V=[y_2-y_1, x_1-x_2]$. Then $$ V \cdotp \vec{AP} = (y_2-y_1)(x-x_1)+(x_1-x_2)(y-y_1)$$ Since dot product is negative iff the angle $\alpha$ between vectors satisfies $ \frac \pi 2 < \alpha < \frac 3 2 \pi$, the sign of the dot product determines which side of $\vec{AB}$ $P$ lies on.