How to draw a perpendicular line using a point and slope

23 visualizaciones (últimos 30 días)
Hessa Alali
Hessa Alali el 24 de Mzo. de 2019
Comentada: Image Analyst el 20 de Feb. de 2022
I have a line between two point, P1=[374 448] and P2=[385 562]. i want to find a perpendicular line to the original line passing throw the middle point of the original line.

Respuestas (3)

Image Analyst
Image Analyst el 24 de Mzo. de 2019
Try this:
P1=[374 448]
P2=[385 562]
p1x = P1(1);
p1y = P1(2);
p2x = P2(1);
p2y = P2(2);
plot([p1x, p2x], [p1y, p2y], 'ro-', 'LineWidth', 2);
grid on;
xlabel('x', 'FontSize', 15);
ylabel('y', 'FontSize', 15);
% Find midpoint
midX = mean([p1x, p2x])
midY = mean([p1y, p2y])
hold on;
plot(midX, midY, 'r*', 'LineWidth', 2, 'MarkerSize', 25);
% Get the slope
slope = (p2y-p1y) / (p2x-p1x)
% For perpendicular line, slope = -1/slope
slope = -1/slope
% Point slope formula (y-yp) = slope * (x-xp)
% y = slope * (x - midX) + midY
% Compute y at some x, for example at x=300
x = 300
y = slope * (x - midX) + midY
plot([x, midX], [y, midY], 'bo-', 'LineWidth', 2);
axis equal
0001 Screenshot.png
  1 comentario
Hessa alali
Hessa alali el 2 de Abr. de 2019
I used this method and it works fine.
But when i applied it in an image, it doesn't gave me a perpendicular line.
does the image have a difference in their axis ? x and y?

Iniciar sesión para comentar.


John D'Errico
John D'Errico el 24 de Mzo. de 2019
Editada: John D'Errico el 24 de Mzo. de 2019
You want to "find" it? Or you want to draw it?
The midpoint of the line segment is at
(P1 + P2)/2
ans =
379.5 505
Y is irrelevant there, since you are asking about a VERTICAL line, thus a line with infinite slope. The line is described by the simple equation X = 379.5. Y takes on any value you wish.
How to draw that line? This will suffice.
xline((P1(1) + P2(1))/2)
xline was introduced in R2018b.
  3 comentarios
Hessa Alali
Hessa Alali el 24 de Mzo. de 2019
xline doesn't work for me
Rik
Rik el 24 de Mzo. de 2019
Do you mean you get an error using xline (possibly due to using an older release than R2018b), or do you mean you don't get the expected output? In the former case, provide details about the error and about what Matlab version you're using. In the latter case, explain how the result is different from what you expect.

Iniciar sesión para comentar.


michael scheinfeild
michael scheinfeild el 20 de Feb. de 2022
why if we dont use axis equal seems line is not
perpendicular
  1 comentario
Image Analyst
Image Analyst el 20 de Feb. de 2022
Because the scaling for each direction would be different. Imagine if you had equal scaling for x and y and had a 45 degree line going from (0,0) to (1,1) and it took up 4 inches on your screen in each direction.. Now let's say that instead you cut the distance along the x direction from 4 inches to 1 inch. It still goes from (0,0) to (1,1) but now the x=1 point is not at 4 inches, but it's at 1 inch. Would that change the apparent slope of the line? Sure it would. It's stil 45 degrees mathematically but on screen it's not - it's at a steeper angle now.

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by