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.
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
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)
Find the (X, Y) point: solve the
(X - Ax) * (By - Ay) - (Y - Ay) * (Bx - Ax) = 0
(X - Px) * Vx - (Y - Py) * Vy = 0
Your new vector will be Your old vector (both components) plus YVN * Dist;