1 - 为一行有方向的尝试以下代码:
%Initial line information
startPoint = [20 50] ;
direction = [4 3] ;
lineLength = 100;
%Initialize line points with zeros
x = zeros(lineLength);
y = zeros(lineLength);
% Update line points
for i = 1 : lineLength
x(i) = startPoint(1) + direction(1) * i;
y(i) = startPoint(2) + direction(2) * i;
end
%Plot the line
plot( y , x ,'r.');
2 - 如果您希望每个点的方向都发生变化,
使用以下代码:
%Initial line values
startPoint = [20 50] ;
lineLength = 100;
%create random direction vector
randomMax = 100;
direction = randi(randomMax,lineLength,2);
%Initialize line points with zeros
x = zeros(lineLength);
y = zeros(lineLength);
%set first points
x(1) = startPoint(1);
y(1) = startPoint(2);
% Update line points accumulating on previous point
for i = 2 : lineLength
x(i) = x(i - 1) + direction(i,1) * i;
y(i) = y(i - 1) + direction(i,2) * i;
end
%Plot the line
plot( y , x ,'r.');
3 - 对于每条具有不同方向的线,请使用以下代码:
%Initial line values
startPoint = [20 50] ;
lineLength = 100;
%create random the 100 directions vector
randomMax = 100;
directions = randi(randomMax,lineLength,2);
%Initialize line points with zeros
x = zeros(lineLength,100);
y = zeros(lineLength,100);
%set first points
x(1) = startPoint(1);
y(1) = startPoint(2);
h3 = figure;
% Update line points accumulating on previous point
for k = 1 : 100
for i = 2 : lineLength
x(i,k) = x(i - 1,k) + directions(k,1) * i;
y(i,k) = y(i - 1,k) + directions(k,2) * i;
end
%hold the figure and plot the k-th line
hold on;
plot( y(: , k) , x(: , k) , 'r.');
end