how to find a line perpendicular to another line?
조회 수: 31 (최근 30일)
이전 댓글 표시
X = [126.3798 126.3818]
Y = [37.5517 37.5495]
I want to find a perpendicular line to this XY line at second point like this image.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1164448/image.png)
Thanks.
댓글 수: 0
채택된 답변
Jan
2022년 10월 21일
편집: Jan
2022년 10월 21일
% 2 points to define a line:
X = [126.3798 126.3818];
Y = [37.5517 37.5495];
% Its direction:
v = [diff(X); diff(Y)];
n = v / vecnorm(v); % Normalized
% Orthogonal means dot(n,m) = 0 or:
% n(1) * m(1) + n(2) * m(2) = 0
% There are 2 solutions:
m_a = [n(2), -n(1)];
m_b = [-n(2), n(1)]; % Other orientation
Now choose one of these m's as direction of the line. Use any point to start from as initial point.
댓글 수: 0
추가 답변 (2개)
Matt J
2022년 10월 21일
편집: Matt J
2022년 10월 21일
The equation is,
dot(X-Y,Z-Z0)=0
where Z0 is a known point on the desired line.
X = [126.3798 126.3818];
Y = [37.5517 37.5495];
Z0=(X+Y)/2;
line([X(1),Y(1)],[X(2),Y(2)],LineStyle='--'); hold on %original line
fimplicit(@(Z1,Z2) (X-Y)*[Z1-Z0(1);Z2-Z0(2)]); hold off %perpendicular bisector
댓글 수: 0
Image Analyst
2022년 10월 21일
Try this well commented demo:
% Define original line.
X = [126.3798 126.3818];
Y = [37.5517 37.5495];
% Plot it.
plot(X, Y, 'b.-', 'LineWidth', 2, 'MarkerSize', 30)
grid on;
% Determine slope of original line.
originalSlope = diff(Y)/diff(X)
% The slope of the perpendicular line is -1 over the original slope.
newSlope = -1/originalSlope
% To draw second perpendicular line use point slope formula
% (y-y2) = slope * (x-x2)
% Define range of x values for the new perpendicular line.
x = linspace(X(2), 126.385, 10);
% Get y values over that new x range.
yPerp = newSlope * (x - X(2)) + Y(2)
% Plot perpendicular line
hold on;
plot(x, yPerp, 'r.-', 'LineWidth', 2, 'MarkerSize', 30)
axis equal % Make it so it's scaled the same in x and y
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Mathematics and Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!