How to increase the efficiency of trigonometric code

I need help to make my code below more efficient and clean it up a bit.

As shown in this image , x and y can be any point around the entire screen, and I'm trying to find the angle t. Is there a way to reduce the number of lines here?

Note: the origin is in the upper left corner, and the movement to the right / down moves in the positive direction

o := MiddleOfScreenX - x; a := MiddleOfScreenY - y; t := Abs(Degrees(ArcTan(o / a))); if(x > MiddleOfScreenX)then begin if(y > MiddleOfScreenY)then t := 180 + t else t := 360 - t; end else if(y > MiddleOfScreenY)then t := 180 - t; 

The code is in pascal, but answers in other languages ​​with similar syntax in either C ++ or java are also fine.

 := sets the variable to that value Abs() result is the absolute of that value (removes negatives) Degrees() converts from radians to degrees ArcTan() returns the inverse tan 
+4
source share
3 answers

see this http://www.cplusplus.com/reference/clibrary/cmath/atan2/ for function C.

atan2 takes 2 separate arguments, so it can define a quadrant.

pascal can have arctan2 see http://www.freepascal.org/docs-html/rtl/math/arctan2.html or http://www.gnu-pascal.de/gpc/Run-Time-System.html

 o := MiddleOfScreenX - x; a := MiddleOfScreenY - y; t := Degrees(ArcTan2(o, a)); 
+6
source

The number of lines of code is not necessarily the only optimization you should consider. Trigonometric functions are expensive in terms of the time it takes to complete one calculation (i.e., a single call to cos () may require hundreds of additions and multiplications depending on the implementation).

In the case of the widely used signal processing function, discrete Fourier transform, the results of thousands of calculations cos () and sin () are pre-computed and stored in a massive search table. The trade-off is that you use more memory when you run your application, but it works much faster.

See the following article or search for the importance of “pre-calculated twisting factors,” which essentially means computing tons of complex exponentials well in advance.

In the future, you should also indicate what you are trying to optimize (i.e. used processor cycles, number of bytes of used memory, cost, among other things). I can only assume that you want to optimize in terms of the instructions being executed and, in addition, the number of processor cycles used (i.e.: you want to reduce CPU overhead).

http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.34.9421&rep=rep1&type=pdf

+3
source

You only need one test to determine what to do with arctan .. your existing tests recover information destroyed by Abs() .

atan() usually returns between -pi / 4 and pi / 4. Your coordinate system is a bit strange - turn 90 degrees clockwise to get a “standard”, although you take atan from x/y as opposed to y/x . It’s already hard for me to solve this in my head.

In any case, I believe that your test should be such that if you are negative a , add 180 degrees. If you want to avoid negative angles; add 360 degrees if this is negative.

+1
source

All Articles