"Jip " <firstname.lastname@example.org> wrote in message <email@example.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:
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.