![]() |
Cartesian to angle
Given arbitrary (x,y) Cartesian coordinates, how do you program the calculation of the 0-to-2pi angle clockwise from the +Y axis? Note: I accidentally wiped out the original post. If the reconstructed wording above is not exactly what I originally posted, let me know and I will fix it. |
Re: Cartesian to angle
I use "atan2(x,y)". It's not 100% what you're looking for (measured (-pi,pi] from the x-axis clockwise) but it does all of the "hard work" in the standard library so it should be efficient in the general case. The conversion from there to what you want is straight forward.
Don't feed it (0,0) :) Wikipedia gives a good description. How do you? |
Re: Cartesian to angle
from the +y axis? atan2(-x,y) seems to do the trick.
|
Re: Cartesian to angle
atan2(x,y) gives +/-pi clockwise from the +Y axis. That puts the discontinuity on the -Y axis, which then requires you to add conditional logic to fix it. There's a better way. BTW, concerning atan2(0,0), many modern implementations, such as the microcode in the FP unit of the Pentium*, helpfully return zero when both arguments are zero. Does anyone know what the FRC versions of LabVIEW, C++, and Java do? * here's a short test code written in Delphi: USES windows, sysutils; Function FPatan2(y : extended; x : extended): Extended; Assembler; asm fld [y] ; fld [x] ; fpatan end ; BEGIN write(FPatan2(0,0),' Pentium FP processor built-in microcode'); END. |
Re: Cartesian to angle
Quote:
|
Re: Cartesian to angle
Just to be clear, all references to atan2 in any of my posts, UOS, refer to the standard definition of the order of the parameters, not the reversed-order of, say, Microsoft Excel spreadsheet. |
Re: Cartesian to angle
atan2(-x,-y)+pi seems to avoid excessive logic.
|
Re: Cartesian to angle
Quote:
|
Re: Cartesian to angle
A look up table?
|
Re: Cartesian to angle
Quote:
|
Re: Cartesian to angle
Quote:
|
Re: Cartesian to angle
Quote:
|
Re: Cartesian to angle
Quote:
|
Re: Cartesian to angle
Quote:
|
Re: Cartesian to angle
Fantastic!
I though you were going to post the trig logic to do all of the calculations. :) "atan2" is certainly the way to go, transforming coordinates before using it is great. I need to look at a clock again... right hand rule = CCW!!! :ahh: See my earlier post. From a readability perspective, consider defining a macro statement such that. angle = HEADING(0,0); atan2(0,0) is undefined in the standard. So for cross platform use, you should check for this as an arbitrary compiler can throw an exception. if y == 0.0 then y = 1e-8. Assuming you aren't working with nano-bots :) this will do nicely and leave your (0,0) point at 0. Unfortunately this adds a comparison operation which, as implied here, are commonly the most costly operations and what you are trying to avoid. :( This is a prety good arguement for changing the standard. I did some reading, and as Ether pointed out, ALL recent Intel chips define (0,0) as 0 and this is on the chip and part of the FPU. If you use a look-up table, you need an unbounded (or large) 2D table in x and y... You can manage this by normalization or conversion to a one dimensional problem but atan2 solves it right there in the silicon. There are a host of other issues with the look-up table but the one that strikes me is the effort associated with continually moving a table back and forth through the layers of cache on the chip (Cache is MUCH smaller than RAM). It's not a good option and should not be faster (coding or execution with other intensive code in the loop) or portable to higher resolution applications. In the interest of exhausting all alternatives, the look-up table certainly is "out of the box" and I like it. Keep up the good work! With the Intel FPU, most modern PC's and Macs will get (0,0) = 0 "right" provided the compiler actually accesses the FPU directly for this calculation (that would be most compilers including gcc). But what does the cRIO do? Is the FPGA intel based? Looks like "Xilinx Virtex" is a custom processor for FPGA? So you need documentation of a firm commitment to define the result as 0, or you need to test and a re-test every time the chip is upgraded which could happen without a "model" change to cRIO. |
| All times are GMT -5. The time now is 01:05. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi