Got Questions? Get Answers.
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:
Angle between two points

Subject: Angle between two points

From: Kale Hommy

Date: 24 Sep, 2008 16:09:01

Message: 1 of 9

I was wondering how to calculate the angle between two points using matlab, unfortunately this is my first time using matlab and when I input my points I did it individually, ex. x1=2 y1=7, x2=4 y2=9 and x3=4 y3=8. Is there any easy way to calculate the angle formed by those three points?

Thanks, Kale Hommy.

Subject: Angle between two points

From: David

Date: 24 Sep, 2008 16:32:01

Message: 2 of 9

"Kale Hommy" <k.hommy@gmail.com> wrote in message <gbdomt$d2u$1@fred.mathworks.com>...
> I was wondering how to calculate the angle between two points using matlab, unfortunately this is my first time using matlab and when I input my points I did it individually, ex. x1=2 y1=7, x2=4 y2=9 and x3=4 y3=8. Is there any easy way to calculate the angle formed by those three points?
>
> Thanks, Kale Hommy.

well, from the subject, i could say that 2 points make a line and there is no angle.

But since you have given 3 points i have to change that and ask, which of the 3 angles do you want to compute?

Subject: Angle between two points

From: Feng

Date: 24 Sep, 2008 16:38:02

Message: 3 of 9

point1 = [2 7];
point2 = [4 9];
point3 = [4 8];

sita2 = GetAngle(point1, point2);
....
....


function sita = GetAngle(p1, p2)
   temp =(p1(1)*p2(1) + p1(2)*p2(2))/(sqrt(sum(p1.^2))*sqrt(sum(p2.^2)));
   sita = acos(temp);
end

"Kale Hommy" <k.hommy@gmail.com> wrote in message <gbdomt$d2u$1@fred.mathworks.com>...
> I was wondering how to calculate the angle between two points using matlab, unfortunately this is my first time using matlab and when I input my points I did it individually, ex. x1=2 y1=7, x2=4 y2=9 and x3=4 y3=8. Is there any easy way to calculate the angle formed by those three points?
>
> Thanks, Kale Hommy.

Subject: Angle between two points

From: Walter Roberson

Date: 24 Sep, 2008 16:40:54

Message: 4 of 9

Kale Hommy wrote:
> I was wondering how to calculate the angle between two points using matlab,
> unfortunately this is my first time using matlab and when I input my points
> I did it individually, ex. x1=2 y1=7, x2=4 y2=9 and x3=4 y3=8. Is there any
> easy way to calculate the angle formed by those three points?

Something close to this:

P1 = [x1, y1]; P2 = [x2, y2]; P3 = [x3, y3];
angle = acos(dot(P1-P2,P3-P2) / norm(P1-P2) / norm(P3-P2));

For more information, look up the definition of dot product.

Subject: Angle between two points

From: Feng

Date: 24 Sep, 2008 16:41:02

Message: 5 of 9

function sita = GetAngle(p1, p2)
    temp = sum(p1.*p2)/(sqrt(sum(p1.^2))*sqrt(sum(p2.^2)));
    sita = acos(temp);
end

"Kale Hommy" <k.hommy@gmail.com> wrote in message <gbdomt$d2u$1@fred.mathworks.com>...
> I was wondering how to calculate the angle between two points using matlab, unfortunately this is my first time using matlab and when I input my points I did it individually, ex. x1=2 y1=7, x2=4 y2=9 and x3=4 y3=8. Is there any easy way to calculate the angle formed by those three points?
>
> Thanks, Kale Hommy.

Subject: Angle between two points

From: Loren Shure

Date: 26 Sep, 2008 13:28:02

Message: 6 of 9

In article <gbdqiu$560$1@fred.mathworks.com>, nclxin@hotmail.com says...
> function sita = GetAngle(p1, p2)
> temp = sum(p1.*p2)/(sqrt(sum(p1.^2))*sqrt(sum(p2.^2)));
> sita = acos(temp);
> end
>
> "Kale Hommy" <k.hommy@gmail.com> wrote in message <gbdomt$d2u$1@fred.mathworks.com>...
> > I was wondering how to calculate the angle between two points using matlab, unfortunately this is my first time using matlab and when I input my points I did it individually, ex. x1=2 y1=7, x2=4 y2=9 and x3=4 y3=8. Is there any easy way to calculate the angle formed by those three points?
> >
> > Thanks, Kale Hommy.
>

Might be overkill, but subspace might do what you want:

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/subspace.html

--
Loren
http://blogs.mathworks.com/loren

Subject: Angle between two points

From: Sangjae

Date: 13 Nov, 2011 13:34:11

Message: 7 of 9

wow... you're great!

Subject: Angle between two points

From: Sangjae

Date: 13 Nov, 2011 13:34:11

Message: 8 of 9

thanks, great@!!

Subject: Angle between two points

From: Roger Stafford

Date: 13 Nov, 2011 22:28:11

Message: 9 of 9

"Sangjae " <kor_rea@hanmail.net> wrote in message <j9oh0j$g3r$1@newscl01ah.mathworks.com>...
> wow... you're great!
- - - - - - - - - -
  This is a very old thread, but I would like to point out that using matlab's atan2 function rather than its acos will yield a more robust computation, though more computing is involved. For values of x very near 1 or -1, acos(x) is subject to a marked decrease in accuracy, due to the fact that the inverse cosine function attains an infinite derivative at these two extreme values.

  For example, let theta = 3.14159265 radians which is very slightly below pi. Then we can compare theta with theta1 using atan2 and with theta2 using acos:

 theta1 = atan2(sin(theta),cos(theta));
 theta2 = acos(cos(theta));

 theta = 3.14159265000000
 theta1 = 3.14159265000000
 theta2 = 3.14159265358979

The atan2 has maintained an accuracy out to at least 15 decimal places while acos retains only 9 places for theta this close to pi.

  Assuming the original poster intended 'angle' to mean one of the inner angles in the triangle formed by the given three points, then the use of matlab's 'subspace' function would give erroneous results for obtuse inner angles, since subspace values are restricted to the range from 0 to pi/2.

  If we assume that the desired inner angle lies at the vertex P1 = (x1,y1), it can be computed in radians as follows:

 a = atan2( abs((x2-x1)*(y3-y1)-(y2-y1)*(x3-x1)), ...
                (x2-x1)*(x3-x1)+(y2-y1)*(y3-y1) );

Roger Stafford

Tags for this Thread

No tags are associated with 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