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:
fast angle calculation between 2D vectors

Subject: fast angle calculation between 2D vectors

From: Jip

Date: 19 Oct, 2010 14:54:04

Message: 1 of 3

Hi all,

My problem is the following..I have a grid with about 8000 grid points. On one of those grid points, there's a 2 dimensional vector. I should now calculate the angle of every single gridpoint compared to the direction where the "master-vector" points. To know in which direction the angle is oriented, the results should be between -180° and 180°.
At the moment I make a vector for every gridpoint (endpoint-startpoint) and then tried several things with atan2 I found here, but had to find out that "dot" takes extremely long to calculate. The code should be as fast as possible, because I have to calculate those angle for all the 8000 gridpoints for 1020 "master-vectors".

Anybody of the experts an idea how I could speed up the process, or somebody knowing a completely different approach which could give me the result I'm looking for?

Thanks in advance! cheers, jip

Subject: fast angle calculation between 2D vectors

From: Matt J

Date: 19 Oct, 2010 16:17:04

Message: 2 of 3

"Jip " <nonexistent@gmail.com> wrote in message <i9kbec$5dn$1@fred.mathworks.com>...
>
> At the moment I make a vector for every gridpoint (endpoint-startpoint) and then tried several things with atan2 I found here, but had to find out that "dot" takes extremely long to calculate. The code should be as fast as possible, because I have to calculate those angle for all the 8000 gridpoints for 1020 "master-vectors".
==============


The dot() and norm() operations can be replaced by calls to MTIMESX:

http://www.mathworks.com/matlabcentral/fileexchange/25977-mtimesx-fast-matrix-multiply-with-multi-dimensional-support

See also, this thread for a related discussion:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/293747#787738

Subject: fast angle calculation between 2D vectors

From: Roger Stafford

Date: 19 Oct, 2010 16:57:03

Message: 3 of 3

"Jip " <nonexistent@gmail.com> wrote in message <i9kbec$5dn$1@fred.mathworks.com>...
> Hi all,
>
> My problem is the following..I have a grid with about 8000 grid points. On one of those grid points, there's a 2 dimensional vector. I should now calculate the angle of every single gridpoint compared to the direction where the "master-vector" points. To know in which direction the angle is oriented, the results should be between -180° and 180°.
> At the moment I make a vector for every gridpoint (endpoint-startpoint) and then tried several things with atan2 I found here, but had to find out that "dot" takes extremely long to calculate. The code should be as fast as possible, because I have to calculate those angle for all the 8000 gridpoints for 1020 "master-vectors".
>
> Anybody of the experts an idea how I could speed up the process, or somebody knowing a completely different approach which could give me the result I'm looking for?
>
> Thanks in advance! cheers, jip
- - - - - - - - - - -
  It's not quite clear what angle you are computing, whether an angle between vectors associated with two different grid points, or an angle between one "master vector" and the vector extending from that grid point to another grid point.

  In any case your basic problem is to calculate the angle lying between -180 and +180 between two vectors, call them v1 = [v1x,v1y] and v2 = [v2x,v2y], however you arrive at them. The formula going positively from v1 toward v2, that I would normally recommend is:

 ang = 180/pi*atan2( v1x*v2y-v1y*v2x , v1x*v2x+v1y*v2y );

  If the answer to the question in the first paragraph above is that each grid point has an associated vector, it would be best to just calculate the angle between that vector [vx,vy] and, say, the x-axis, which would be:

 angx = 180/pi*atan2(vy,vx)

Then the angle between any two vectors would be obtained by subtraction combined with a 'mod' operation. That is, if a and b are two x-axis angles, then the angle between them going positively from the first vector to the second is:

 mod(b-a+180,360)-180;

which would be a fast operation.

  If the answer above is to find the angle between master vectors and vectors pointing from the master points to other grid points, you could again calculate the angle between each master vector and the x-axis. To get the angle between every line connecting grid points and the x-axis is more laborious. However, you can take advantage of the inherent regularity in a grid arrangement. The x-axis angle for a vector from (x1,y1) to (x2,y2) grid points is the same as that from the different grid points (x1+h,y1+k) to (x2+h,y2+k), so there are far fewer combinations that need to be computed than taking every possible pairing.

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