Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Cartesian to angle (http://www.chiefdelphi.com/forums/showthread.php?t=96187)

Ether 12-07-2011 10:24

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.



James Critchley 12-07-2011 10:51

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?

Jogo 12-07-2011 11:33

Re: Cartesian to angle
 
from the +y axis? atan2(-x,y) seems to do the trick.

Ether 12-07-2011 12:10

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.




Ether 12-07-2011 12:25

Re: Cartesian to angle
 
Quote:

Originally Posted by Jogo (Post 1068879)
from the +y axis? atan2(-x,y) seems to do the trick.

atan2(-x,y) gives you angles counter-clockwise from the +Y axis.



Ether 12-07-2011 12:47

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.



Dmentor 12-07-2011 12:53

Re: Cartesian to angle
 
atan2(-x,-y)+pi seems to avoid excessive logic.

Ether 12-07-2011 13:04

Re: Cartesian to angle
 
Quote:

Originally Posted by Dmentor (Post 1068890)
atan2(-x,-y)+pi seems to avoid excessive logic.

That's what I use. Here's a cheatsheet for other angle choices.



davidthefat 12-07-2011 15:19

Re: Cartesian to angle
 
A look up table?

AdamHeard 12-07-2011 15:21

Re: Cartesian to angle
 
Quote:

Originally Posted by davidthefat (Post 1068899)
A look up table?

Why? That's terribly cumbersome compared to the methods already posted.

davidthefat 12-07-2011 15:40

Re: Cartesian to angle
 
Quote:

Originally Posted by AdamHeard (Post 1068900)
Why? That's terribly cumbersome compared to the methods already posted.

Well, it takes less clock cycles to just retrieve data from an array compared to calculating the angle on the fly. Even though a look up table does take up considerable amount of space in the RAM (Not really since we are dealing with 64 MB of RAM...) Also given the level of accuracy we only really need, about ± pi/180 radian. I think it should be fine.

AdamHeard 12-07-2011 16:04

Re: Cartesian to angle
 
Quote:

Originally Posted by davidthefat (Post 1068901)
Well, it takes less clock cycles to just retrieve data from an array compared to calculating the angle on the fly. Even though a look up table does take up considerable amount of space in the RAM (Not really since we are dealing with 64 MB of RAM...) Also given the level of accuracy we only really need, about ± pi/180 radian. I think it should be fine.

The cycles are a nonissue on the cRIO, and using the function is substantially less code/setup time.

Ether 12-07-2011 16:11

Re: Cartesian to angle
 
Quote:

Originally Posted by davidthefat (Post 1068901)
Well, it takes less clock cycles to just retrieve data from an array compared to calculating the angle on the fly.

Where do you get your information ?



EricVanWyk 12-07-2011 16:27

Re: Cartesian to angle
 
Quote:

Originally Posted by Ether (Post 1068905)

Where do you get your information ?


You must spread some Reputation around before giving it to Ether again.

James Critchley 12-07-2011 17:20

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 23:35.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi