# Does relative velocity vector lie on the line?

5 views (last 30 days)

Show older comments

### Answers (2)

Julian Hapke
on 19 Jun 2017

Edited: Julian Hapke
on 19 Jun 2017

define a vector between the two points and check if the cross product of the delta velocity and the connection vector is 0. so if

ra = [xa,ya,za]

rb = [xb,yb,zb]

% va and vb being the velocities

test = cross(ra-rb,va-vb)

if ~test % check if all are zero

disp('same direction')

end

EDIT: As Jan Simon pointed out, you may get precision problems, so

if all(abs(test))<eps % or any other threshold

disp('same direction')

end

Jan
on 19 Jun 2017

Edited: Jan
on 19 Jun 2017

Do you want to calculate the angle between the connection of the 2 points and the relative velocity? Then:

u = A - B;

v = vA - vB;

a = atan2(norm(cross(u,v)), dot(u,v));

Now check if the result a is below a certain limit. You cannot expect it to be exactly 0.0 or 180.0 due to the limited precision of the floating point values. Perhaps this is a smart limit:

limit = 10 * eps(max([A(:); B(:); vA(:); vB(:); u(:); v(:)])

isParallel = abs(a) < limit || abs(a) - 180 < limit;

But there is not "best" definition of the limit.

See https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab for a discussion, why atan2 is more accurare than acos or asin or the corresponding cross and dotr product methods only.

### See Also

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!