How to calculate normal to a line?
Show older comments
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?
Accepted Answer
More Answers (2)
Image Analyst
on 27 Aug 2013
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
Shivakumar
on 28 Aug 2013
Image Analyst
on 28 Aug 2013
Edited: Image Analyst
on 28 Aug 2013
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
on 29 Aug 2013
Edited: Shivakumar
on 29 Aug 2013
Image Analyst
on 29 Aug 2013
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?
Shivakumar
on 30 Aug 2013
Shashank Prasanna
on 28 Aug 2013
Edited: Shashank Prasanna
on 28 Aug 2013
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
on 29 Aug 2013
Edited: Shivakumar
on 29 Aug 2013
ddd ppp
on 4 May 2018
is the null space a vector or line? does it have direction?
Categories
Find more on Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!