I have two points, $(x_0, y_0)$ and $(x_1, y_1)$. The point $(x_0, y_0)$ is on a line, which has angle $\theta$, relative to the X-axis.

Using these values, I want to be able to calculate the shortest distance, $d$, along the line (relative to $(x_0, y_0)$) which is less than 2 units away from $(x_1, y_1)$.

I suspect there may be something to do with slope and arctangent, but I can't make heads or tails of how I should attempt this (It's for a video game, in case that matters).

Sorry if this is a dupe, or if my math-speak is a bit too much programmer and a bit too little mathematician.

enter image description here

  • $\begingroup$ Angle $\theta$ with respect to what? $\endgroup$ Apr 1, 2019 at 2:53
  • $\begingroup$ @MichaelBiro X-axis, sorry $\endgroup$ Apr 1, 2019 at 2:54
  • $\begingroup$ I want to be able to calculate the _shortest_ distance, d, along the line $\endgroup$ Apr 1, 2019 at 2:55
  • $\begingroup$ It’s either $0$ if the distance between the two points is $\le2$ or the distance to the nearest intersection of the line with a circle of radius $2$. $\endgroup$
    – amd
    Apr 1, 2019 at 3:12
  • $\begingroup$ I added an image, maybe you can refine it or replace it with more information. $\endgroup$
    – NoChance
    Apr 1, 2019 at 3:17

2 Answers 2


I'm assuming you want a programmatic approach instead of a purely analytic one, so I would attack this problem using standard transformations.

First, translate so $(x_0,y_0)$ lands on the origin - this corresponds to subtracting $(x_0,y_0)$ from each point.

Second, rotate by $-\theta$ so that the line $L$ lands on the $x$-axis - this corresponds to multiplying by the rotation matrix $$\begin{bmatrix} \cos \theta & \sin \theta\\-\sin \theta & \cos \theta\end{bmatrix}$$

Now, $(x_1, y_1)$ has been transformed to $(x_1^\prime , y_1^\prime)$, and we want to find the point on the $x$-axis within a distance of $2$ that has the smallest $x$ coordinate. Assuming $y_1^\prime \leq 2$:

If ${x^\prime_1}^2 + {y^\prime_1}^2 \leq 4$ then $(0,0)$ is the nearest point.


If $x^\prime_1 > 0$, then $(x^\prime_1 - \sqrt{2^2 - {y_1^\prime}^2},0)$ is the nearest point.

If $x^\prime_1 < 0$, then $(x^\prime_1 + \sqrt{2^2 - {y_1^\prime}^2},0)$ is the nearest point.

Now, just reverse the transformations to find the coordinates you need.

  • $\begingroup$ Thanks! This is just what I need. $\endgroup$ Apr 1, 2019 at 3:51

I'll propose the example as I first understood it:

The line through x0,y0 does not hit x1,y1. Also there is a point-of-origin of x,y. So there is a line from x,y to x0,y0 as the first line and a line from x,y to x1,y1 as the second line. Then there is an angle between the two lines such that Sin(angle) * distance-of-second-line = right-angle-distance off the first line. Also, Tan(angle) * unknown-distance = previously-calculated-right-angle-distance then calculates the distance from x,y to the right-angle-offset on the first line.

I calculate problems like this with a land surveying system of working with bearing-directions and distances. For instance an angle to the first line of 10 degrees off the x-axis is a bearing of N80E.

The direction and distance from x,y to x1,y1 is an "inverse" of the second line:

Direction = InvTan((x1 - x) / (y1 - y)). If (x1 - x) is positive that is E otherwise W. If (y1 - y) is positive that is N otherwise S. The expected result in this example is a direction of N_angle_E.

Distance = Square Root of ((x1 - x)^2 + (y1 - y)^2) .


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .