How to calculate normal to a line?

I plot a line in MATLAB using the points. Please tell me how to obtain the normal of that line? Can I get these plots in a single plot?

2 Comments

hi i have a 2D image of echocardiography. is there any solution to compute the normal vector of this image?
normal vector to image?

Sign in to comment.

 Accepted Answer

Jan
Jan on 27 Aug 2013
It is easier to answer, if you explain any details. At first I assume you mean 2D-lines, because for 3D-lines a normal line is not defined.
If you have a vector with the coordinates [x, y], the vectors [y, -x] and [-y, x] are orthogonal. When the line is defined by the coordinates of two points A and B, create the vector B-A at first, determine the orientation by the above simple formula, decide for one of the both vectors, and the midpoint between the points (A+B) * 0.5 might be a nice point to start from. Adjusting the length of the normal vector to either 1 or e.g. the distance norm(B-A) might be nice also.

12 Comments

These are the values I have A=(-0.6779,-0.7352), B(0.7352,-0.6779) I have drawn a line using them by the command: line(A,B) or plot(A,B). Can you please tell me how to get the normal to this line? How can I get this two lines in a single plot?
@Shivakumar: Please show us what you have tried so far and explain the occurring problems. Otherwise it looks, like you want us to solve your problems with investing as less own time as possible. If you post the example in valid Matlab syntax, copy&pasting it in the answer is easier. Not that these seconds really matter, but you can demonstrate respect by making answering as easy as possible. Ok?
A = [-0.6779,-0.7352];
B = [0.7352,-0.6779];
V = B - A;
midV = A + 0.5 * V;
normal1 = [ V(2), -V(1)];
normal2 = [-V(2), V(1)];
plot([A(1), B(1)], [A(2), B(2)], 'r');
hold on
plot([midV(1), midV(1) + normal1(1)], [midV(2), midV(2) + normal1(2)], 'g');
plot([midV(1), midV(1) + normal2(1)], [midV(2), midV(2) + normal2(2)], 'b');
Does this explain what I have written in words?
Shivakumar
Shivakumar on 29 Aug 2013
Edited: Shivakumar on 29 Aug 2013
I am sorry. You got me wrong. My intention is to tell my aim concerning the problem.I tried what you told in the first answer. I don't think I understood your first answer clearly. So, I posted the values I have and the command I used to do the plot. I respect you. Please forgive me. But I thank you for the answer you provided. I thank you again for the clear answer you gave me and for the time spent for me. Now after looking at the syntax you provided, I understand your first answer up to some extent.
I think I did wrong by posting the numerical values, I might have given you like (x,y). I sincerely request you to forgive me. Thank you
Dear Shivakumar, this is a public forum and questions are even more important than answers (therefore I still think the name of this forum is misleading). It is the nature of questions, that some details of them are not clear to the asking person. Misunderstandings about the which details are useful or not to create an answer occur frequently.
My intention in posting answers is that 1. the current problem is solves as good and fast as possible and 2. that future problems can be solved efficiently also e.g. by improving the quality of the way to ask or by learning how to use debugging techniques and the documentation by your own.
In any case your question about Matlab is very welcome in the forum and a discussion about the details of your question should only improve the chance that you get a useful answer. Therefore there is nothing, I could forgive.
Sometimes answers from the frequent contributors sounds a little bit to hard, but this is caused by the fact, that some answers or suggestions have been given hundreds of times before. So do not take this personally.
Kind regards and welcome to the forum.
I thank you for your kind reply.
I've tried to obtain normal to the line at origin but I didn't get it. Can you please tell me how to obtain normal to a line at the origin(0,0)? Thank you
Are you still working on this a month later, even after you accepted it?
@Shivakumar: There is no normal to a point. You need a line in 2D. So please explain how you can determine the coordinates of this line.
I have the coordinates A(x,y) and B(y,-x). The line formed by these coordinates is passing through origin. I want to obtain normal to this line at the origin O(0,0).
What does "A(x,y) and B(y,-x)" exactly mean? Using Matlab-Syntax is a common method in this forum.
@Jan Simon: My question is similar to the one you answered before. For the values I gave you already(A = [-0.6779,-0.7352]; B = [0.7352,-0.6779];), you helped me in finding normal to the line at the mid-point. Now, I am trying to find normal to the same line at the origin. Please help me. Thank you.
@Shivakumar: The normal of a line is a vector. Vectors can be move freely in the space, because they have a direction and a length. but no start point. Therefore the normal of the line at the midpoint is the normal at any other point also. If you want to draw the normal, it looks nice, when you start it at the midpoint of the line segment. but in a mathematical sense it is correct to start it from any other point of the X-Y-plane as well, e.g. at the origin.

Sign in to comment.

More Answers (2)

A perpendicular line has a negative inverse slope. So if you used polyfit
coeffs = polyfit(x, y, 1);
then coeffs(1) is the slope. The new slope is -1/coeffs(1). Now you simply use the point-slope formula of a line to draw it. Obviously you need to know at least one point that that line goes through since there are an infinite number of lines at that slope (all parallel to each other of course).

5 Comments

These are the values I have A=(-0.6779,-0.7352), B(0.7352,-0.6779) I have drawn a line using them by the command: line(A,B) or plot(A,B). Can you please tell me how to get the normal to this line? How can I get this two lines in a single plot?
I did tell you how to do it. Did you try what I instructed? I'll do part of it for you
coeffs = polyfit([A(1), B(1)], [A(2), B(2)], 1);
newSlope = -1/coeffs(1);
% Now specify a new point A_perp
A_perp = [whatever.....];
% Then calculate B_perp
B_perp = ..... (I know you can do this 9th grade math)
Is this your homework? It sounds like homework.
Shivakumar
Shivakumar on 29 Aug 2013
Edited: Shivakumar on 29 Aug 2013
It is a part of my project work. I tried what you instructed before. I didn't understood how to take a point-slope formula to draw the line. So, I gave the values to you. But I thank you for the answer you provided and for the time you spent in giving me this help
You gave the two endpoints of the original line. Those do not have to be on the new, perpendicular line, though they could. Where do you want the perpendicular line to cross/intersect the first line? At the end? In the middle? Somewhere else?
I want the perpendicular line to cross in the middle. Thank you.

Sign in to comment.

If this is a homework, please spend some time familiarizing yourself with basics of MATLAB. You can start by going through the Getting Started guide
There are several ways you could do this and all of the already suggested approaches are good. Here is how you can think about it in terms of linear algebra.
Answer: Normal lies in the null space of the the matrix A-B
A = [-0.6779, -0.7352]; B = [0.7352, -0.6779];
null(A-B)
Proof:
(A-B)*null(A-B) % Should yield a number close to zero
If you are looking to plot:
x = [A(1);B(1)];
y = [A(2);B(2)];
line(x,y,'color','k','LineWidth',2)
normal = [mean(x),mean(y)] + null(A-B)';
line([mean(x),normal(1)],[mean(y),normal(2)],'color','r','LineWidth',2)

2 Comments

Shivakumar
Shivakumar on 29 Aug 2013
Edited: Shivakumar on 29 Aug 2013
This is not my homework but It is a part of my project work. I thank you for the answer you provided.
is the null space a vector or line? does it have direction?

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products

Asked:

on 27 Aug 2013

Commented:

on 4 May 2018

Community Treasure Hunt

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

Start Hunting!