How to make a line of fixed length?
조회 수: 29 (최근 30일)
이전 댓글 표시
Hi all,
I want to make a line that has a fixed length and it has a partciular direction. I would appreciate any suggestions for this. Thanks!
댓글 수: 2
Davide Masiello
2022년 11월 29일
I am afraid you need to be clearer. By making, do you mean plotting?
Moreover, what do you mean with direction?
채택된 답변
Star Strider
2022년 11월 29일
It is relatively straightforward to write an anonymous function for this —
createLine = @(startpt, slope, len) [startpt(1) startpt(1)+len.*cos(atan(slope)); startpt(2) startpt(2)+len.*sin(atan(slope))];
slope = rand % Scalar
startpoint = rand(1,2) % Two-Element Vector
long = 10*rand % Scalar
linevec = createLine(startpoint, slope, long)
Check = [tan(atan2(diff(linevec(2,:)), diff(linevec(1,:)))) hypot(diff(linevec(2,:)),diff(linevec(1,:)))]
figure
plot(linevec(1,:), linevec(2,:))
grid
axis('padded','equal')
title(sprintf('Line from (%.2f, %.2f) to (%.2f, %.2f), slope %.2f, length %.2f', [linevec(:,1); linevec(:,2); slope; long]))
Experiment with the ‘createLine’ function.
.
댓글 수: 2
추가 답변 (1개)
Vilém Frynta
2022년 11월 29일
You can specify length with numbers of your choice.
You can specify direction numerically as well, you just need to use your imagination and get creative.
x = [1 11];
y = [2 22];
line(x,y);
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!