how to find a line perpendicular to another line?

73 views (last 30 days)
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.

Accepted Answer

Jan
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.

More Answers (2)

Matt J
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

Image Analyst
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)
originalSlope = -1.1000
% The slope of the perpendicular line is -1 over the original slope.
newSlope = -1/originalSlope
newSlope = 0.9091
% 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)
yPerp = 1×10
37.5495 37.5498 37.5501 37.5505 37.5508 37.5511 37.5514 37.5518 37.5521 37.5524
% 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

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!