how to find a line perpendicular to another line?

71 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Jan
Jan il 21 Ott 2022
Modificato: Jan il 21 Ott 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.

Più risposte (2)

Matt J
Matt J il 21 Ott 2022
Modificato: Matt J il 21 Ott 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 il 21 Ott 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

Categorie

Scopri di più su Mathematics and Optimization in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by