Question on how to position points on a line

I've the following to divide a line to equal segments ref
xy1 = [141.64 399.53];
xy2 = [213.32 250.96];
t = linspace(0,1,11)';
xy = (1-t)*xy1 + t*xy2;
plot(xy(:,1),xy(:,2),'b-o')
Output:
xy =
141.6400 399.5300
148.8080 384.6730
155.9760 369.8160
163.1440 354.9590
170.3120 340.1020
177.4800 325.2450
184.6480 310.3880
191.8160 295.5310
198.9840 280.6740
206.1520 265.8170
213.3200 250.9600
I'd like to do the following for positioning new points along the line between points xy1 and xy2.
xy del
141.6400 399.5300 0
148.8080 384.6730 1
155.9760 369.8160 2
163.1440 354.9590 0
170.3120 340.1020 0
177.4800 325.2450 1
184.6480 310.3880 0
191.8160 295.5310 0
198.9840 280.6740 0
206.1520 265.8170 1.5
213.3200 250.9600 0
when shift is 0 , no point has to be added in the neighborhood of the corresponding (x,y).
For any nonzero value, 2 points (one to right and one to left) have to be added.
For example if xy = (10,0)
del=2 would mean 2 points (8,0) and (12,0) have to be generated in the code for adding the new points on line between points xy1 and xy2.
del=1 would mean 2 points (9,0) and (11,0) have to be generated in the code for adding the new points on line between points xy1 and xy2.
Any suggestions on how to do this will be of great help!

 Respuesta aceptada

Toder
Toder el 9 de Mayo de 2020
Editada: Toder el 11 de Mayo de 2020
Assuming del is a column vector of the same size as t and is the desired change in x, try this
xy1 = [141.64 399.53];
xy2 = [213.32 250.96];
t = linspace(0,1,11)';
% new stuff
del = [0 1 2 0 0 1 0 0 0 1.5 0]';
m = (xy1(2)-xy2(2))/(xy1(1)-xy2(1));
xOld = (1-t)*xy1(1) + t*xy2(1);
ind = del~=0;
x = unique(sort([xOld; xOld(ind)+del(ind); xOld(ind)-del(ind)]));
y = m*(x-xy1(1))+xy1(2);
plot(x,y,'b-o')
If del is the desired distance (2 norm) between the original point and one of the new points, try this
xy1 = [141.64 399.53];
xy2 = [213.32 250.96];
t = linspace(0,1,11)';
xyOld = (1-t)*xy1 + t*xy2;
del = [0 1 2 0 0 1 0 0 0 1.5 0]';
m = (xy1(2)-xy2(2))/(xy1(1)-xy2(1));
theta = atan(m);
ind = del~=0;
xy = unique( [xyOld; ...
xyOld(ind,:) + [del(ind)*cos(theta),del(ind)*sin(theta)]; ...
xyOld(ind,:) - [del(ind)*cos(theta),del(ind)*sin(theta)] ], 'rows' );
plot(xy(:,1),xy(:,2),'b-o')

6 comentarios

Deepa Maheshvare
Deepa Maheshvare el 10 de Mayo de 2020
Thanks a lot. Can we remove the old points (xy1 and xy2) in x,y? I'd like to plot only the new stuff.
Deepa Maheshvare
Deepa Maheshvare el 10 de Mayo de 2020
x = unique(sort([xOld(ind)+del(ind); xOld(ind)-del(ind)]));
helped.
Glad to help. You can do this to remove the endpoints:
x = x(2:end-1);
y = y(2:end-1);
I'm facing a problem
xy1 = [141.64 399.53];
xy2 = [213.32 250.96];
t = linspace(0,1,9)';
% new stuff
del = [0 0.8 0.8 0.8 0.8 0.8 0.8 0.8 0]';
m = (xy1(2)-xy2(2))/(xy1(1)-xy2(1));
xOld = (1-t)*xy1(1) + t*xy2(1);
ind = del~=0;
x = unique(sort([xOld; xOld(ind)+del(ind); xOld(ind)-del(ind)]));
%x = unique(sort([xOld(ind)+del(ind); xOld(ind)-del(ind)]));
y = m*(x-xy1(1))+xy1(2);
plot(x,y,'b-o')
When del=0.8 , I expect sqrt((xl-xr)^2 + (yl-yr)^2) to be 1.6 . But it's 2.3. I'm not sure what's going on.
Here, (xl = xleft,yl =yleft ) (x,y) (xr=xright, yr = yright). Could you please look into this?
I see a problem in this line
unique(sort([xOld; xOld(ind)+del(ind); xOld(ind)-del(ind)]));
I think it's not correct to do this xold + del and xold - del. This would work if the slope of a line is zero.
Toder
Toder el 11 de Mayo de 2020
I've edited my answer.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Versión

R2019b

Preguntada:

el 9 de Mayo de 2020

Comentada:

el 11 de Mayo de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by