Question on how to position points on a line

6 ビュー (過去 30 日間)
Deepa Maheshvare
Deepa Maheshvare 2020 年 5 月 9 日
コメント済み: Toder 2020 年 5 月 11 日
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!

採用された回答

Toder
Toder 2020 年 5 月 9 日
編集済み: Toder 2020 年 5 月 11 日
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 件のコメント
Toder
Toder 2020 年 5 月 11 日
I've edited my answer.

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by