George Lua
George Lua

Reputation: 17

How to adjust length of a line drawn between two lines?

I have a line that I have drawn between two points

origin = [1 0 2];
point = [1 -2.8 2.8 ];

I want to draw a line between them at a certain distance. I have tried the following, which gives me wrong results... Is there a better way to do this?

distance = 0.5;
point3 = origin + (point-origin) * distance;

MY SOLUTION: which assumes that I will start at the same origin, just the last point changes:

cff = 0.5; % Coefficient that determines the length of the line
point2(1)= origin(1) + cff* (point(1) - origin(1));
point2(2)= origin(2) + cff* (point(2)- origin(2));
point2(3)= origin(3) + cff* (point(3) - origin(3));

For a length of a certain distance from origin:

d = 3; % Distance
unit_v = point - origin;
u = unit_v / norm(unit_v);
point2(1)= origin(1) + d  * u(1) ;
point2(2)= origin(2) + d  * u(2) ;
point2(3)= origin(3) + d  * u(3) ;

Upvotes: 0

Views: 38

Answers (1)

saastn
saastn

Reputation: 6015

You can calculate the unit vector from start point to end point and move the start point:

A = [1 0 2];
B = [1 -2.8 2.8 ];
d = 0.5;

V = B-A;        % A->B vector
l = norm(V);    % length of V
U = V/l;        % unit vector
C = A + d*U;    
D = A + (l-d)*U;

hold on
plot3([A(1) B(1)], [A(2) B(2)],[A(3) B(3)], '--o')
plot3([C(1) D(1)], [C(2) D(2)],[C(3) D(3)], '-x', 'linewidth', 2)

enter image description here

Upvotes: 2

Related Questions