Question on how to position points on a line

1 view (last 30 days)
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!

Accepted Answer

Toder
Toder on 9 May 2020
Edited: Toder on 11 May 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 Comments
Deepa Maheshvare
Deepa Maheshvare on 11 May 2020
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.

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!