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 line and axis

Subject: Angle between line and axis

From: Suzana

Date: 10 Jan, 2012 13:43:08

Message: 1 of 10

Hello!

How I can calculate angle between line and x axis in Matlab.
For instance, I have point P1 with coordinate x1 = 20; y1=35 and point P2 with coordinates x2=68 and y2= 25.

I can draw line between these points and calculate it length, etc. But I don't know how I can calculate angle between THIS line and x - axis.....

Thaks in advance....

Subject: Angle between line and axis

From: eige

Date: 10 Jan, 2012 14:45:27

Message: 2 of 10

On Jan 10, 8:43 am, "Suzana " <suzana.petro...@mfkg.rs> wrote:
> Hello!
>
> How I can calculate angle between line and x axis in Matlab.
> For instance, I have point P1 with coordinate x1 = 20; y1=35 and point P2 with coordinates x2=68 and y2= 25.
>
> I can draw line between these points and calculate it length, etc. But I don't know how I can calculate angle between THIS line and x - axis.....
>
> Thaks in advance....

um, atan?

Subject: Angle between line and axis

From: Suzana

Date: 10 Jan, 2012 15:04:08

Message: 3 of 10

eige <jeicke@gmail.com> wrote in message <d9d5fbf1-2532-4943-9114-3b12d20e06bb@i6g2000vbk.googlegroups.com>...
> On Jan 10, 8:43 am, "Suzana " <suzana.petro...@mfkg.rs> wrote:
> > Hello!
> >
> > How I can calculate angle between line and x axis in Matlab.
> > For instance, I have point P1 with coordinate x1 = 20; y1=35 and point P2 with coordinates x2=68 and y2= 25.
> >
> > I can draw line between these points and calculate it length, etc. But I don't know how I can calculate angle between THIS line and x - axis.....
> >
> > Thaks in advance....
>
> um, atan?

If that function will help me to find an angle between line and x-axis and if you know how to implement that function, then I meant on that function...

Subject: Angle between line and axis

From: Suzana

Date: 10 Jan, 2012 15:37:07

Message: 4 of 10

I hope so that I find the right angle. I try to use this

angle=atan2(z2-z1,y2-y1)*180/pi;

Am I on the right way with this?


"Suzana" wrote in message <jehk18$2mu$1@newscl01ah.mathworks.com>...
> eige <jeicke@gmail.com> wrote in message <d9d5fbf1-2532-4943-9114-3b12d20e06bb@i6g2000vbk.googlegroups.com>...
> > On Jan 10, 8:43 am, "Suzana " <suzana.petro...@mfkg.rs> wrote:
> > > Hello!
> > >
> > > How I can calculate angle between line and x axis in Matlab.
> > > For instance, I have point P1 with coordinate x1 = 20; y1=35 and point P2 with coordinates x2=68 and y2= 25.
> > >
> > > I can draw line between these points and calculate it length, etc. But I don't know how I can calculate angle between THIS line and x - axis.....
> > >
> > > Thaks in advance....
> >
> > um, atan?
>
> If that function will help me to find an angle between line and x-axis and if you know how to implement that function, then I meant on that function...

Subject: Angle between line and axis

From: Rune Allnor

Date: 10 Jan, 2012 16:05:04

Message: 5 of 10

On 10 Jan, 16:37, "Suzana " <suzana.petro...@mfkg.rs> wrote:
> I hope so that I find the right angle. I try to use this
>
> angle=atan2(z2-z1,y2-y1)*180/pi;
>
> Am I on the right way with this?

No. You shouldn't believe everything you
read on the internet. You might want to
consult the odd textbook as well.

For two vecors u and v, the angle theta
between them is given by the inner product,

<u,v> = |u|*|v|*cos(theta).

Solve this expression for cos(theta), and
plug in the vectors that describe the line
and the axis, respectivley.

Rune

Subject: Angle between line and axis

From: Bruno Luong

Date: 10 Jan, 2012 16:43:07

Message: 6 of 10

Rune Allnor <allnor@tele.ntnu.no> wrote in message <09d3b079-5702-4fe9-8036-71369a6af095@dp8g2000vbb.googlegroups.com>...
> On 10 Jan, 16:37, "Suzana " <suzana.petro...@mfkg.rs> wrote:
> > I hope so that I find the right angle. I try to use this
> >
> > angle=atan2(z2-z1,y2-y1)*180/pi;
> >
> > Am I on the right way with this?
>
> No. You shouldn't believe everything you
> read on the internet. You might want to
> consult the odd textbook as well.

Wait a minute.

angle_in_radian=atan2(y2-y1,x2-x1);
angle_in_deg=atan2(y2-y1,x2-x1)*180/pi;

are absolutely correct ways to compute angle between x-axis and P1P2, where P1=(x1,y1) and P1=(x2,y2). It is more accurate than acos(.) (see Roger's many posts on the topic).

Bruno

Subject: Angle between line and axis

From: Suzana

Date: 10 Jan, 2012 16:49:08

Message: 7 of 10

Bruno, thank you very much. This is really helps me.


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jehpqr$nmb$1@newscl01ah.mathworks.com>...
> Rune Allnor <allnor@tele.ntnu.no> wrote in message <09d3b079-5702-4fe9-8036-71369a6af095@dp8g2000vbb.googlegroups.com>...
> > On 10 Jan, 16:37, "Suzana " <suzana.petro...@mfkg.rs> wrote:
> > > I hope so that I find the right angle. I try to use this
> > >
> > > angle=atan2(z2-z1,y2-y1)*180/pi;
> > >
> > > Am I on the right way with this?
> >
> > No. You shouldn't believe everything you
> > read on the internet. You might want to
> > consult the odd textbook as well.
>
> Wait a minute.
>
> angle_in_radian=atan2(y2-y1,x2-x1);
> angle_in_deg=atan2(y2-y1,x2-x1)*180/pi;
>
> are absolutely correct ways to compute angle between x-axis and P1P2, where P1=(x1,y1) and P1=(x2,y2). It is more accurate than acos(.) (see Roger's many posts on the topic).
>
> Bruno

Subject: Angle between line and axis

From: Rune Allnor

Date: 10 Jan, 2012 17:09:29

Message: 8 of 10

On 10 Jan, 17:43, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> Rune Allnor <all...@tele.ntnu.no> wrote in message <09d3b079-5702-4fe9-8036-71369a6af...@dp8g2000vbb.googlegroups.com>...
> > On 10 Jan, 16:37, "Suzana " <suzana.petro...@mfkg.rs> wrote:
> > > I hope so that I find the right angle. I try to use this
>
> > > angle=atan2(z2-z1,y2-y1)*180/pi;
>
> > > Am I on the right way with this?
>
> > No. You shouldn't believe everything you
> > read on the internet. You might want to
> > consult the odd textbook as well.
>
> Wait a minute.
>
> angle_in_radian=atan2(y2-y1,x2-x1);
> angle_in_deg=atan2(y2-y1,x2-x1)*180/pi;
>
> are absolutely correct ways to compute angle between x-axis and P1P2, where P1=(x1,y1) and P1=(x2,y2). It is more accurate than acos(.)

Two comments:

1) The inner product is the general method, while
   your suggestion only works with respct to one of
   the axes.

2) Somebody who needs to ask the question at all
   is not concerned with numerical accuracies.

Rune

Subject: Angle between line and axis

From: Bruno Luong

Date: 10 Jan, 2012 17:30:09

Message: 9 of 10

Rune Allnor <allnor@tele.ntnu.no> wrote in message <859cce56-847e-4b00-92ae-e68004ae82bf@z1g2000vbx.googlegroups.com>...

>
> Two comments:
>
> 1) The inner product is the general method, while
> your suggestion only works with respct to one of
> the axes.

Wong again, the atan2() method can extend to angle between two arbitrary vectors (in R2 and R3, which is the case). I leave for you to find the formula as exercise Rune.

>
> 2) Somebody who needs to ask the question at all
> is not concerned with numerical accuracies.
>

This is a bonus (beside it is a *correct* method, contrary to what you did claim)

Bruno

Subject: Angle between line and axis

From: Roger Stafford

Date: 10 Jan, 2012 18:26:07

Message: 10 of 10

Rune Allnor <allnor@tele.ntnu.no> wrote in message <859cce56-847e-4b00-92ae-e68004ae82bf@z1g2000vbx.googlegroups.com>...
> Two comments:
>
> 1) The inner product is the general method, while
> your suggestion only works with respct to one of
> the axes.
>
> 2) Somebody who needs to ask the question at all
> is not concerned with numerical accuracies.
>
> Rune
- - - - - - - - - - -
  In 3D and 2D space the inner (or scalar) product is not more fundamental than the vector product (or in 2D, the determinant.)

  The advantage of matlab's 'atan2' function is that it can take advantage of both the sine and cosine functions to ensure accuracy over its full range of angles. Also it allows a wider range from -pi to +pi for the angle, as opposed to a restriction to 0 to pi for the arccosine and -pi/2 to +pi/2 for the arcsine (and the arctangent) functions, if that is desired. A single glance at the plot of matlab's 'acos' function with its infinite derivatives at both the two ends should be a convincing argument as to the loss of accuracy there.

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