# Find the point of intersection with only one endpoint and a slope.

So, I'm in the process of making a game, and I need coordinates to snap to a line. I have a point, and a segment, so I have a segment that is perpendicular to the other segment, and goes though my point. I can get it's slope with the negative reciprocal, but how can I get the point at which that perpendicular segment intersects my original segment? In simpler terms, I have a slope and an endpoint, and another segment of which I have two endpoints, how can I tell what the point of intersection is?

Don't use slope like in math class. Instead you should use a parameterized equation for your line segment. You can create the following for a line segment going through the points $(x_0, y_0)$ and $(x_1, y_1)$:

$$x = x_0 + t\Delta x \\ y = y_0 + t\Delta y$$

Where $\Delta x = x_1 - x_0$ and $\Delta y = y_1 - y_0$. Now the perpendicular line segment will be:

$$x_\perp = \begin{cases} x_0 - t\Delta y & \left|\Delta y\right| > \left|\Delta x\right| \\ x_0 + t\Delta y & \left|\Delta y\right| < \left|\Delta x\right| \end{cases} \\ y_\perp = \begin{cases} y_0 + t\Delta x & \left|\Delta y\right| > \left|\Delta x\right| \\ y_0 - t\Delta x & \left|\Delta y\right| < \left|\Delta x\right| \end{cases}$$

You should do the test because you don't want to negate something very close to zero (in fact it may actually be zero).

And the reason you should do it this way, is what if you have a vertical segment (or a horizontal segment)?

edit If you have a point $(x, y)$ which you want to then drop a perpendicular to your line segment, you just need to solve the equations:

$$x_\perp = x - t\Delta y = x_0 + s\Delta x \\ y_\perp = y + t\Delta x = y_0 + s\Delta y$$

Everything here is known except $t$ and $s$. Multiply both equations by $\Delta y$ and $\Delta x$, respectively:

$$x\Delta y - t\left(\Delta y\right)^2 = x_0\Delta y + s\Delta x \Delta y\\ y\Delta x + t\left(\Delta x\right)^2 = y_0 \Delta x + s\Delta y\Delta x$$

Subtract them to cancel the $s$'s:

$$y\Delta x - x\Delta y + t\left(\left(\Delta x\right)^2 + \left(\Delta y\right)^2\right)= y_0\Delta x - x_0\Delta y\\ t = \frac{\left(y_0 - y\right)\Delta x + \left(x - x_0\right)\Delta y}{\left(\Delta x\right)^2 + \left(\Delta y\right)^2}$$

Then just plug into your equation for the perpendicular line:

$$x_\perp = x - t\Delta y \\ y_\perp = y + t\Delta x$$

Here $x_\perp$ and $y_\perp$ are the points which should connect to $(x, y)$ to create a perpendicular to the original line segment of $(x_0, y_0)$ and $(x_1, y_1)$.

• What does the t mean? Other than that, I think I understand. – user2507230 Apr 6 '14 at 2:12
• It's just a variable that gives your line, so for instance if you plug in $t = 0$ to the original you get $(x_0, y_0)$ or if you plug in $t= 1$, you get $(x_1, y_1)$. – Jared Apr 6 '14 at 2:21
• And you actually don't need those case--negating a $0$. That's the beauty of doing it this way (which I didn't even fully believe at first). – Jared Apr 6 '14 at 2:51
• But actually, what you need is the $s$ of the second part of the answer, which is the parameter of the line through $(x_0,y_0)$ and $(x_1,y_1)$. $t$ is the parameter of the perpendicular line. Then you can replace $s$ with $0$ if $s<0$ and with $1$ if $s>1$ to get the closest point of the segment. – LutzL Apr 6 '14 at 16:15
• @LutzL It really depends on what the OP is trying to do. I'm not totally certain. It's pretty straightforward to solve for $s$ instead of $t$ in the above. Instead of multiplying the first by $\Delta y$ and the second by $\Delta x$, switch it so that now the $t$'s can cancel instead of the $s$'s. – Jared Apr 6 '14 at 17:05