# How can I check if perpendicular on a line segment even exists? [closed]

I have segment line with two points $T_1(x_1,y_1), T_2(x_2,y_2)$ and then I place the $3.$ point $T_3(x_3,y_3)$. Is there a way to check (with given $x_1, x_2, x_3, y_1, y_2, y_3)$ if perpendicular to the segment line $T_1,T_2$ from $T_3$ exists? I've managed to calculate the perpendicular for line but not the segment line. I am programming in C#.

double dx = x2 - x1;
double dy = y2 - y1;
double mag = Math.Sqrt(dx * dx + dy * dy);
dx /= mag;
dy /= mag;
double lambda = (dx * (x3 - x1)) + (dy * (y3 - y1));
rx1 = (dx * lambda) + x1;
ry1 = (dy * lambda) + y1;


rx1 and ry1 are coordinates of a point T3.

## closed as off-topic by Xander Henderson, GNUSupporter 8964民主女神 地下教會, Saad, Shailesh, LeucippusMar 23 '18 at 3:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question is missing context or other details: Please improve the question by providing additional context, which ideally includes your thoughts on the problem and any attempts you have made to solve it. This information helps others identify where you have difficulties and helps them write answers appropriate to your experience level." – Xander Henderson, GNUSupporter 8964民主女神 地下教會, Saad, Shailesh, Leucippus
If this question can be reworded to fit the rules in the help center, please edit the question.

## 2 Answers

The equation of the line through $T_3$ that is perpendicular to the line through $T_1$ and $T_2$ is $$(x_2-x_1)(x-x_3)+(y_2-y_1)(y-y_3)=0.\tag{*}$$ The line segment $\overline{T_1T_2}$ does not intersect this line if the points $T_1$ and $T_2$ are on the same side of the line (*). This will occur when the values that you get by plugging in the coordinates of these points into the left-hand side of (*) have the same sign.

That is, compare the signs of $(T_1-T_3)\cdot(T_2-T_1)$ and $(T_2-T_3)\cdot(T_2-T_1)$: if they are both positive or both negative, then the projection of $T_3$ onto the line through $T_1$ and $T_2$ does not lie on the segment $\overline{T_1T_2}$. However, if you’re already computing the projection, anyway, a simple range check of its coordinates against those of $T_1$ and $T_2$ will also tell you whether or not the point lies on $\overline{T_1T_2}$.

One way to check is

• consider the line $T_1T_2$ that is $T_1+t(T_2-T_1)$
• consider the plane through $T_3$ orthogonal to $T_1T_3$ that is $ax+by+cz=d$ with $(a,b,c)=T_2-T_1$ and $d$ to find by the condition that $T_3\in$ plane
• intersec line $T_1T_2$ with the plane and check whether the intersection lies between $T_1$ and $T_2$

As an alternative find the angles on $T_1T_2$ of the $\triangle T_1T_2T_3$, if they are both $\le90°$ such segment exists.