Game Development Stack Exchange is a question and answer site for professional and independent game developers. 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

Let's say a vector starts at Point P and points in the direction of v. How do I make sure that this vector exactly reaches the line? Its end should be exactly on the line, and not cross it.

Illustration of a vector exactly meeting a line

share|improve this question
up vote 5 down vote accepted

You should calculate the intersection point (IP) between the two lines.

      (X - Ax)     (Y - Ay)
EQ1 = --------  =  --------   =>   (X-Ax) * (By-Ay) - (Y - Ay) * (Bx - Ax) = 0;
      (Bx - Ax)    (By - Ay)

      (X - Px)     (Y - Py)
EQ2 = --------  =  --------   =>   (X-Px) * (Vx) - (Y - Py) * (Vy) = 0;
         (Vx)        (Vy)

Solve the two equations and you get IP (X,Y).

Maybe you'd need to check that IP is between A and B... before continue...

Then you build a new line with this equation:

  (P + V) = P + t*(IP-P)   => t = V / (IP - P) 

   t = (IP.X-P.X != 0) 
        ? (V.X / (IP.X - P.X)) 
        : (V.Y / (IP.Y - P.Y));

  if t>1 then (P+V) has overpassed the line


enter image description here


1) You have to calculate IP. the intersection point
2) You should check that IP is between A and B
3) You had the vector V, and now have the vector (IP-P),
4) You can choose betwwen two options:
   a) Now you can compare the vector lenghts to now if V is greater that (IP-P)
   b) Calculate t as I described before...
           if t==0 => V=(0,0);  
           if t=1 => V=(IP-P)
           if (t>0 && t<1) length(V) < length(IP-P)
           if (t>1) length(V) > length(IP-P)

enter image description here

share|improve this answer
It looks nice. Is it possible that to include a solved example. It will very helpful – user960567 Apr 18 '12 at 9:16
beat me to it.. +1 – teodron Apr 18 '12 at 9:17
@Blau, Please help. I have not understand your formula. P + t*(IP-P). I think it is a vector and you are checking it as scaler – user960567 Apr 18 '12 at 10:48
Maybe this will help: – Roy T. Apr 18 '12 at 10:58
@RoyT. It did'nt help. – user960567 Apr 18 '12 at 11:19

You should:

  1. Find an (X, Y) point of intersection of your line and a support line for the vector.
  2. Extend your vector by the distance from its end to than point you've found.


  1. Find the (X, Y) point: solve the

    (X - Ax) * (By - Ay) - (Y - Ay) * (Bx - Ax) = 0


    (X - Px) * Vx - (Y - Py) * Vy = 0


  2. Your new vector will be Your old vector (both components) plus YVN * Dist;

    • where YVN is Your old vector normalized (one-unit length);
    • where Dist is distance between N and V;
    • where N is your newfoundland point.
share|improve this answer
Is it possible that you please with an example. Like Blau demostrate with an example. – user960567 Apr 18 '12 at 12:29
@user960567 okay. – AgentFire Apr 18 '12 at 12:51
I can't extend the vector. Vector is fixed. I can only change the starting Point P. – user960567 Apr 18 '12 at 12:52
@user960567 okay now? – AgentFire Apr 18 '12 at 13:10
I said that I cannot change vector – user960567 Apr 18 '12 at 14:41

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.