360 views
in GUI Development by
How can I do a atan2() function in Chora?

1 Answer

0 votes
by

Hello Mike,

the version 9,30 does not implement the function atan2(). We added this math function in version 10.0. Well, how to calculate such value in 9.30?

I suppose, you want to calculate the angle from a vector connecting the origin position 0,0 and some x,y position. In such case you can achieve the same by using e.g. the math_acos() function. The following is a code snippet I have extracted from the Rotate Touch Handler. This handler calculates from the performed 'rotation gesture' the corresponding angle:

var float x = ...
var float y = ...

// The distance between the x,y position and the center of the coordinate system
var float radius = math_sqrt( float(( x * x ) + ( y * y )));
var float angle  = 0.0;

if ( radius > 0.0 )
  angle = math_acos( x / radius );
else
  ... vector has length = 0 -> the angle is unknown

if ( y > 0 )
  angle = 360 - angle;

Is this something you are looking for?

Best regards

Paul Banach

by
Well, I'm trying to convert normalized X,Y on a color wheel (from a SimpleTouchHandler) into HSV and then into RGB. I'll give this a try, Thanks.

Also, does EW have a constant defined for Pi like M_PI in C?
by

Ok. I have understood. I think, the above example with math_acos() should produce similar results compared to atan2(). Let me know if you encounter a problem.

Concerning the usage of a PI constant, such PI constant is not defined in Chora. You have to calculate with the corresponding number 3.14159...

Best regards

Paul Banach

by
Ya, this should work. Your example just has 0deg at East rather than North.

I'll post up my code after. No need for others to reinvent the color wheel ... knuck knuck knuck.
by
O yes, you are right. The above code was taken from a touch handler and the coordinate system in EW is flipped vertically. The Y axis points down.

Best regards

Paul Banach
by
if ( r > 0.0 )
  angle = math_acos( Y / r );
// else RGB = 255,255,255 because were at center...?

if ( X < 0.0 )
  angle = 360 - angle;

Is the way to fix that incase anyone else ever reads this.

Ask Embedded Wizard

Welcome to the question and answer site for Embedded Wizard users and UI developers.

Ask your question and receive answers from the Embedded Wizard support team or from other members of the community!

Embedded Wizard Website | Privacy Policy | Imprint

...