how to find a line perpendicular to another line?
74 views (last 30 days)
Show older comments
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.
Thanks.
0 Comments
Accepted Answer
Jan
on 21 Oct 2022
Edited: Jan
on 21 Oct 2022
% 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 Comments
More Answers (2)
Matt J
on 21 Oct 2022
Edited: Matt J
on 21 Oct 2022
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 Comments
Image Analyst
on 21 Oct 2022
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 Comments
See Also
Categories
Find more on Mathematics and Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!