http://www.mathworks.com/matlabcentral/newsreader/view_thread/314681
MATLAB Central Newsreader  Projection of a point over a line
Feed for thread: Projection of a point over a line
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Thu, 24 Nov 2011 17:23:08 +0000
Projection of a point over a line
http://www.mathworks.com/matlabcentral/newsreader/view_thread/314681#859471
Waleed ElBadry
Hi,<br>
I recently created read an algorithm for finding the coordinates of projected point on a line here:<br>
<a href="http://www.google.com.eg/url?sa=t&rct=j&q=finding%20the%20projection%20point%20on%20a%20line&source=web&cd=1&ved=0CBkQFjAA&url=http%3A%2F%2Fcs.nyu.edu%2F~yap%2Fclasses%2Fvisual%2F03s%2Fhw%2Fh2%2Fmath.pdf&ei=5nvOTqboLc7ogber4WzDg&usg=AFQjCNEI8ZC1XeLDRyPL4gPo6Wbyyq1ahg&cad=rja">http://www.google.com.eg/url?sa=t&rct=j&q=finding%20the%20projection%20point%20on%20a%20line&source=web&cd=1&ved=0CBkQFjAA&url=http%3A%2F%2Fcs.nyu.edu%2F~yap%2Fclasses%2Fvisual%2F03s%2Fhw%2Fh2%2Fmath.pdf&ei=5nvOTqboLc7ogber4WzDg&usg=AFQjCNEI8ZC1XeLDRyPL4gPo6Wbyyq1ahg&cad=rja</a><br>
<br>
Consequently I created this function:<br>
ffunction [ pp ] = pointprojection( c1,c2,p)<br>
%pointprojection function used to calculate the projecttion of a point over<br>
% a line<br>
<br>
% Parameters<br>
% c1...point 1 c1=[c1x c1y]<br>
% c2...point 2 c1=[c2x c2y]<br>
% p... point to find its projection p=[px py]<br>
% pp...projected point pp=[ppx;ppy]<br>
<br>
a=[c2(1)c1(1),c2(2)c1(1);c1(2)c2(2),c2(1)c1(1)];<br>
b=[p(1)*(c2(1)c1(1))p(2)*(c2(2)c1(2));c1(2)*(c2(1)c1(1))+c1(1)*(c2(2)c1(2))];<br>
pp=a\b;<br>
plot(c1(1),c1(2),'xr',c2(1),c2(2),'xr',[c1(1) c2(1)],[c1(2) c2(2)],'r',p(1),p(2),'+g',pp(1),pp(2),'*g',[p(1) pp(1)],[p(2) pp(2)],'b');<br>
end<br>
<br>
The problem is the formula only works if the line is horizontal and fail in case of vertical or inclined line !<br>
<br>
Do I miss something?<br>
<br>
Thanks

Thu, 24 Nov 2011 17:46:38 +0000
Re: Projection of a point over a line
http://www.mathworks.com/matlabcentral/newsreader/view_thread/314681#859477
Rune Allnor
On 24 Nov, 18:23, "Waleed ElBadry" <wba...@must.edu.eg> wrote:<br>
> Hi,<br>
> I recently created read an algorithm for finding the coordinates of projected point on a line<br>
...<br>
> The problem is the formula only works if the line is horizontal and fail in case of vertical or inclined line !<br>
><br>
> Do I miss something?<br>
<br>
I don't know, as I have no idea how you were<br>
thinking when you wrote that code.<br>
<br>
The way to do these kinds of things is to<br>
set up matrix/vector expressions for the<br>
point and the line:<br>
<br>
Assume the vector u represents the point and<br>
the vector v represents the line (through origo).<br>
<br>
In that case the vector P representing the projected<br>
point is computed as (u and v column vectors)<br>
<br>
P = vv'u.<br>
<br>
If the line doesn't intersect origo, you need a<br>
modification which is simple if you understood a word<br>
above what I explained above (that is, if you<br>
know linear algebra), and rather uncomprehensable<br>
if not.<br>
<br>
Rune

Thu, 24 Nov 2011 18:10:09 +0000
Re: Projection of a point over a line
http://www.mathworks.com/matlabcentral/newsreader/view_thread/314681#859481
Roger Stafford
"Waleed ElBadry" <wbadry@must.edu.eg> wrote in message <jaluhs$ep8$1@newscl01ah.mathworks.com>...<br>
> a=[c2(1)c1(1),c2(2)c1(1);c1(2)c2(2),c2(1)c1(1)];<br>
         <br>
I believe your expression for 'a' has an error. It should be:<br>
<br>
a=[c2(1)c1(1),c2(2)c1(2);c1(2)c2(2),c2(1)c1(1)];<br>
<br>
Roger Stafford

Tue, 17 Jan 2012 06:54:08 +0000
Re: Projection of a point over a line
http://www.mathworks.com/matlabcentral/newsreader/view_thread/314681#863937
Ali
Hi,<br>
<br>
I tried this myself and something is certainly amiss, can anyone help?<br>
<br>
% write function that projects the point (q = X,Y) on a vector<br>
% which is composed of two points  vector = [p0x p0y; p1x p1y]. <br>
% i.e. vector is the line between point p0 and p1. <br>
%<br>
% The result is a point qp = [x y] and the length [length_q] of the vector <br>
% between the point q and qp . This resulting vector between q and qp <br>
% will be orthogonal to the original vector between p0 and p1. <br>
% <br>
% This uses the derivation found in the webpage:<br>
% <a href="http://cs.nyu.edu/~yap/classes/visual/03s/hw/h2/math.pdf">http://cs.nyu.edu/~yap/classes/visual/03s/hw/h2/math.pdf</a><br>
%<br>
<br>
function [ProjPoint, length_q] = ProjectPoint(vector, q)<br>
<br>
<br>
p0 = vector(1,:)<br>
p1 = vector(2,:)<br>
length_q = 1;<br>
<br>
a = [p1(1)  p0(1), p1(2)  p0(2); p0(2)  p1(2), p1(1)  p0(1)];<br>
b = [q(1)*(p1(1)  p0(1)) + q(2)*(p1(2)  p0(2)); ...<br>
p0(2)*(p1(1)  p0(1))  p0(1)*(p1(2)  p0(2))];<br>
<br>
ProjPoint = a\b<br>
end<br>
<br>
I have checked the maths and it is OK, so I wonder why the point that is returned doesn't make a vector with the projected point that is orthogonal to the original vector on which the point q is projected.<br>
<br>
Any help gratefully accepted!<br>
<br>
<br>
"Roger Stafford" wrote in message <jam1a1$mhk$1@newscl01ah.mathworks.com>...<br>
> "Waleed ElBadry" <wbadry@must.edu.eg> wrote in message <jaluhs$ep8$1@newscl01ah.mathworks.com>...<br>
> > a=[c2(1)c1(1),c2(2)c1(1);c1(2)c2(2),c2(1)c1(1)];<br>
>          <br>
> I believe your expression for 'a' has an error. It should be:<br>
> <br>
> a=[c2(1)c1(1),c2(2)c1(2);c1(2)c2(2),c2(1)c1(1)];<br>
> <br>
> Roger Stafford

Tue, 17 Jan 2012 19:53:08 +0000
Re: Projection of a point over a line
http://www.mathworks.com/matlabcentral/newsreader/view_thread/314681#864014
Roger Stafford
"Ali" wrote in message <jf35ug$9qj$1@newscl01ah.mathworks.com>...<br>
> I tried this myself and something is certainly amiss, can anyone help?<br>
> <br>
> % write function that projects the point (q = X,Y) on a vector<br>
> % which is composed of two points  vector = [p0x p0y; p1x p1y]. <br>
> % i.e. vector is the line between point p0 and p1. <br>
> %<br>
> % The result is a point qp = [x y] and the length [length_q] of the vector <br>
> % between the point q and qp . This resulting vector between q and qp <br>
> % will be orthogonal to the original vector between p0 and p1. <br>
> % <br>
> % This uses the derivation found in the webpage:<br>
> % <a href="http://cs.nyu.edu/~yap/classes/visual/03s/hw/h2/math.pdf">http://cs.nyu.edu/~yap/classes/visual/03s/hw/h2/math.pdf</a><br>
> <br>
> function [ProjPoint, length_q] = ProjectPoint(vector, q)<br>
> p0 = vector(1,:)<br>
> p1 = vector(2,:)<br>
> length_q = 1;<br>
> a = [p1(1)  p0(1), p1(2)  p0(2); p0(2)  p1(2), p1(1)  p0(1)];<br>
> b = [q(1)*(p1(1)  p0(1)) + q(2)*(p1(2)  p0(2)); ...<br>
> p0(2)*(p1(1)  p0(1))  p0(1)*(p1(2)  p0(2))];<br>
> ProjPoint = a\b<br>
> end<br>
> <br>
> I have checked the maths and it is OK, so I wonder why the point that is returned doesn't make a vector with the projected point that is orthogonal to the original vector on which the point q is projected.<br>
         <br>
Your code for 'ProjPoint' looks perfectly valid to me, Ali. You can check the orthogonality directly with the 'dot' function:<br>
<br>
dot(p1p0,ProjPointq)<br>
<br>
which should be zero (except for a tiny round off error.)<br>
<br>
When you do a plot, make sure you follow the 'plot' instruction with "axis equal", or else the plot may look deceptively skewed away from orthogonality because of unequal scaling along the two axes.<br>
<br>
However your returned 'length_q' is completely wrong. There is no reason it should be one! The correct code would be:<br>
<br>
length_q = sqrt((ProjPoint(1)q(1))^2+(ProjPoint(2)q(2))^2);<br>
<br>
or<br>
<br>
length_q = sqrt(sum((ProjPointq).^2));<br>
<br>
or<br>
<br>
length_q = norm(ProjPointq);<br>
<br>
Note that 'length' in this Euclidian sense should not be confused with matlab's 'length' function which is a count of elements. The two concepts are entirely different.<br>
<br>
Roger Stafford