# Does segment contain point of intersection of the line and perpendicular

In cartesian coordinate system for segment $AB$ with known points $A(x_1, y_1)$ and $B(x_2, y_2)$ which lies on a line $a$ what is the easiest way to realise does the segment contains point of intersection $C (x_0, y_0)$ of the line $a$ and it's perpendicular $b$ to the centre $O (0, 0)$? The point of intersection of lines $a$ and $b$ isn't necessary.

Following algorithm looks like difficult:
- find equation of the $a$;
- find equation of the $b$;
- solve the system of equations and find the point $C$;
- check does $x_o$ and $y_0$ belong to $AB$.

If $OB\ge OA$, segment $AB$ contains the projection of $O$ on line $AB$ if and only if $OB^2- OA^2\le AB^2$. That way you check if $\angle OAB\le 90°$.

• This is indeed what I'm looking for! – Gluttton Mar 31 '17 at 5:08

A couple of ways off the top of my head:

Method 1: Parameterize the line as $(1-t)A+tB$ and find the value of $t$ that minimizes the (square of the) distance from the origin. This involves solving a linear equation in $t$. If $t\in[0,1]$ then $C$ is on the line segment.

Method 2: Find the equation of the perpendicular line and see whether or not $A$ and $B$ are on the same side. A simple way to do this is to compare the signs of the signed distances from $A$ and $B$ to the line. The normal equation of the perpendicular line is $(y2-y1)x-(x2-x1)y=0$, so the signed distance of a point $(x,y)$ to this line is $${(y2-y1)x-(x2-x1)y\over\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}}.$$ Plug the two points $A$ and $B$ into this expression: if the results are both positive or both negative, then the intersection lies outside of the line segment. Since you only care about the sign and not the actual value of this distance, you can compute the numerators only, which are just the dot products of the two points with the normal vector of the line $a$.