Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Projection of a point over a line

Subject: Projection of a point over a line

From: Waleed El-Badry

Date: 24 Nov, 2011 17:23:08

Message: 1 of 5

Hi,
I recently created read an algorithm for finding the coordinates of projected point on a line here:
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=5nvOTqboLc7o-gber4WzDg&usg=AFQjCNEI8ZC1XeLDRyPL4gPo6Wbyyq1ahg&cad=rja

Consequently I created this function:
ffunction [ pp ] = pointprojection( c1,c2,p)
%pointprojection function used to calculate the projecttion of a point over
% a line

% Parameters
% c1...point 1 c1=[c1x c1y]
% c2...point 2 c1=[c2x c2y]
% p... point to find its projection p=[px py]
% pp...projected point pp=[ppx;ppy]

a=[c2(1)-c1(1),c2(2)-c1(1);c1(2)-c2(2),c2(1)-c1(1)];
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))];
pp=a\b;
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');
end

The problem is the formula only works if the line is horizontal and fail in case of vertical or inclined line !

Do I miss something?

Thanks

Subject: Projection of a point over a line

From: Rune Allnor

Date: 24 Nov, 2011 17:46:38

Message: 2 of 5

On 24 Nov, 18:23, "Waleed El-Badry" <wba...@must.edu.eg> wrote:
> Hi,
> I recently created read an algorithm for finding the coordinates of projected point on a line
...
> The problem is the formula only works if the line is horizontal and fail in case of vertical or inclined line !
>
> Do I miss something?

I don't know, as I have no idea how you were
thinking when you wrote that code.

The way to do these kinds of things is to
set up matrix/vector expressions for the
point and the line:

Assume the vector u represents the point and
the vector v represents the line (through origo).

In that case the vector P representing the projected
point is computed as (u and v column vectors)

P = vv'u.

If the line doesn't intersect origo, you need a
modification which is simple if you understood a word
above what I explained above (that is, if you
know linear algebra), and rather uncomprehensable
if not.

Rune

Subject: Projection of a point over a line

From: Roger Stafford

Date: 24 Nov, 2011 18:10:09

Message: 3 of 5

"Waleed El-Badry" <wbadry@must.edu.eg> wrote in message <jaluhs$ep8$1@newscl01ah.mathworks.com>...
> a=[c2(1)-c1(1),c2(2)-c1(1);c1(2)-c2(2),c2(1)-c1(1)];
- - - - - - - - - -
  I believe your expression for 'a' has an error. It should be:

 a=[c2(1)-c1(1),c2(2)-c1(2);c1(2)-c2(2),c2(1)-c1(1)];

Roger Stafford

Subject: Projection of a point over a line

From: Ali

Date: 17 Jan, 2012 06:54:08

Message: 4 of 5

Hi,

I tried this myself and something is certainly amiss, can anyone help?

% write function that projects the point (q = X,Y) on a vector
% which is composed of two points - vector = [p0x p0y; p1x p1y].
% i.e. vector is the line between point p0 and p1.
%
% The result is a point qp = [x y] and the length [length_q] of the vector
% between the point q and qp . This resulting vector between q and qp
% will be orthogonal to the original vector between p0 and p1.
%
% This uses the derivation found in the webpage:
% http://cs.nyu.edu/~yap/classes/visual/03s/hw/h2/math.pdf
%

function [ProjPoint, length_q] = ProjectPoint(vector, q)
    
    
    p0 = vector(1,:)
    p1 = vector(2,:)
    length_q = 1;
    
    a = [p1(1) - p0(1), p1(2) - p0(2); p0(2) - p1(2), p1(1) - p0(1)];
    b = [q(1)*(p1(1) - p0(1)) + q(2)*(p1(2) - p0(2)); ...
        p0(2)*(p1(1) - p0(1)) - p0(1)*(p1(2) - p0(2))];
    
    ProjPoint = a\b
end

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.

Any help gratefully accepted!


"Roger Stafford" wrote in message <jam1a1$mhk$1@newscl01ah.mathworks.com>...
> "Waleed El-Badry" <wbadry@must.edu.eg> wrote in message <jaluhs$ep8$1@newscl01ah.mathworks.com>...
> > a=[c2(1)-c1(1),c2(2)-c1(1);c1(2)-c2(2),c2(1)-c1(1)];
> - - - - - - - - - -
> I believe your expression for 'a' has an error. It should be:
>
> a=[c2(1)-c1(1),c2(2)-c1(2);c1(2)-c2(2),c2(1)-c1(1)];
>
> Roger Stafford

Subject: Projection of a point over a line

From: Roger Stafford

Date: 17 Jan, 2012 19:53:08

Message: 5 of 5

"Ali" wrote in message <jf35ug$9qj$1@newscl01ah.mathworks.com>...
> I tried this myself and something is certainly amiss, can anyone help?
>
> % write function that projects the point (q = X,Y) on a vector
> % which is composed of two points - vector = [p0x p0y; p1x p1y].
> % i.e. vector is the line between point p0 and p1.
> %
> % The result is a point qp = [x y] and the length [length_q] of the vector
> % between the point q and qp . This resulting vector between q and qp
> % will be orthogonal to the original vector between p0 and p1.
> %
> % This uses the derivation found in the webpage:
> % http://cs.nyu.edu/~yap/classes/visual/03s/hw/h2/math.pdf
>
> function [ProjPoint, length_q] = ProjectPoint(vector, q)
> p0 = vector(1,:)
> p1 = vector(2,:)
> length_q = 1;
> a = [p1(1) - p0(1), p1(2) - p0(2); p0(2) - p1(2), p1(1) - p0(1)];
> b = [q(1)*(p1(1) - p0(1)) + q(2)*(p1(2) - p0(2)); ...
> p0(2)*(p1(1) - p0(1)) - p0(1)*(p1(2) - p0(2))];
> ProjPoint = a\b
> end
>
> 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.
- - - - - - - - - -
  Your code for 'ProjPoint' looks perfectly valid to me, Ali. You can check the orthogonality directly with the 'dot' function:

 dot(p1-p0,ProjPoint-q)

which should be zero (except for a tiny round off error.)

  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.

  However your returned 'length_q' is completely wrong. There is no reason it should be one! The correct code would be:

 length_q = sqrt((ProjPoint(1)-q(1))^2+(ProjPoint(2)-q(2))^2);

or

 length_q = sqrt(sum((ProjPoint-q).^2));

or

 length_q = norm(ProjPoint-q);

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.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us