Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am working with a game and drawing lines on the screen. I have a line given by two points A(x1, y1) and B(x2, y2). I am looking to find the new point C(x3,y3) that is closer to point A than B.

So for example if the length of line A,B is 20 (the known values) then I want to find point C such that the length of line A,C is 15.

I have this semi-working in the game thus far however I think I'm getting tripped up on a very small detail as it works but only if I draw the line in certain quadrants (like 30,30 and 45,15). Can anyone point me in the right direction as to where this is breaking down? Is it that the coordinate system of the game is different from the traditional axis in math (0,0 is top left and y increases from top)?

My current math is below:

c = current length of line
n = amount to subtract from current length
slope = (y2-y1)/(x2-x1)
t = arctan(slope)
x3 = x1 + ((c-n) * cos(t))
y3 = m * (x - x1) + y1

I then plot the line

x, y, x3, y3

Where x,y is my original starting point for the line (unchanged).

share|cite|improve this question
You should try using the two-argument arctangent instead: t = atan2(y2-y1, x2-x1). – Rahul Sep 18 '11 at 22:20
This also works. Thank you! – methodin Sep 18 '11 at 23:00
Your original approach is easily modified such that you can avoid the cos() and atan2(), since cos(atan2(y, x))==x/sqrt(x^2 + y^2). As an exercise, show that my proposal and your original proposal are in fact identical. ;) – J. M. Sep 18 '11 at 23:09
up vote 3 down vote accepted

Here's a simpler idea: represent your new point $C$ in the following parametric form:


where $0\leq t \leq 1$, such that you have the point $\begin{pmatrix}x_1\\y_1\end{pmatrix}$ when $t=0$ and the point $\begin{pmatrix}x_2\\y_2\end{pmatrix}$ when $t=1$. Your task now is to find the value $t^\prime$ such that the length from $t=0$ to $t=t^\prime$ is $c-n$.

Luckily, the distance from $t=0$ to $t=t^\prime$ is easily derived:

$c-n=t^\prime \sqrt{(x_2-x_1)^2+(y_2-y_1)^2}$

whose derivation I leave as an exercise. Solve for the value of $t^\prime$, substitute into the parametric expression, and you have yourself the desired point.

share|cite|improve this answer
Holy crap that is insanely easier. Awesome! – methodin Sep 18 '11 at 22:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.