Have do I implement something like the function Atan2(x,y)

Started by mogn, November 21, 2007, 10:55:30 AM

Previous topic - Next topic

mogn

Atan2(x,y) is a function which calculate an angle in a continous range from -PI to +PI.

If you makes a 'get position in geometry' and divides this vector with the length, you
get a vector cos(v),sin(v).
What I want is to find v from these two scalars in a continous range.

If any of you know 'apophysis', it contains a transform 'NGON', which transforms a circle to a
regular polygon. This is what I want to implement in tg2.

David Burnett

Of then top of my head atan2(x, y) is atan(y/x)  between -PI and +PI.
atan is arctan in the trig functions

Things that might cause issues / stuff I've forgotten....

I'm not sure how TG2 deals with divide by zero's.
It the trig functions take radians or degree
keeping x/y between -PI and +PI (-180, 180), probably mod PI

Actually here's some pascal code, if you're not a programmer read it out aloud, you'll get the idea.




Harvey Birdman

#2
If you're just trying to get some kind of continous scalar relative to an angle, why not calculate the dot product? A dot product, calculated on two unit vectors lying in a plane, will return the cosine of the angle betweenthe vectors; that is, it will yield a result between -1 and +1, where +1 means the two vectors are ponted in the same direction (angle = 0 degrees), and -1 means they're pointed in the opposite direction (angle = 180 degrees).

The key is making sure you have two unit vectors (magnitude 1.0).

My apologies if I'm telling you someting you already know.

<edit>
The dot product has the added benefit of being strictly multplication and addition - no expensive trig operations.
</edit>

mogn

Thanks for your replies. The use of dot function, does not solve my problem. By the way the dot function has othe uses than
calculate the cos of the angle between two normalized vectors.
Dot([x,y,z],[u,v,w]) is calculated as [x*u+y*v+z*w].

Thanks Dave I will try atan(z/x).

The function I'm going to build is:

u = PI/N
w = mod(v,2u)
f = length*cos(u)/cos(w-u)

EDIT: The last line should read length*cos(w-u)/cos(u)

Matt

This can be calculated by normalizing the position vector, using an Arccos on the X component to retrieve an angle, and then doing a bit of sign flippage and/or addition depending on the sign of the Y component. You can also do something based on Arctan but for some reason I chose to do it differently here. To make it more accurate in all situations you might need to do it differently depending on which value is larger, X or Y, but I haven't done that here.

I've attached a clip file with 3 function groups:

GetAngle: I think this is similar to the Atan2 function you wanted, and returns values between -Pi and +Pi

GetHeading: Same as GetAngle except that the angle is clockwise and starts at 0 along the Z axis. Returns values between -Pi and +Pi.

GetHeadingPositive: Same as GetHeading except that it returns values between 0 and 2 * Pi.

In the next update there will be a built-in function to convert from radians to degrees, but if you want to do that now you should multiply by 180 and divide by Pi (there is a Constant PI function in the clip file).

Matt
Just because milk is white doesn't mean that clouds are made of milk.

mogn


mr-miley

To quote Mr Gumby (from Monty Python)....

"Brain hurts, BRAIN HURTS...."

;D
I love the smell of caffine in the morning

rcallicotte

So this is Disney World.  Can we live here?

mogn

Although Matt's 0..2PI positive function works, I personally do not like conditionals in Programming.
So my solution is:

Cos_v = Get_x(Normalized vector)
v = Arccos(Cos_v)
v_minus_PI = v - PI
Sin_v = Get_z(Normalized vector)
Sign = floor(Sin_v)
Result = v + Sign*(v_minus_PI)

The result is rotated 90 degrees compared to Matts result.

The line 'Sign = floor(Sin_v)' should read: Sign = 2*floor(Sin_v)

mogn

A complete waste of time, but I have demonstrated that it is is possible to create regular polygons in tg2!

rcallicotte

Not a waste.  Something will probably come out of this.  Or a bunch of somethings.   ;D
So this is Disney World.  Can we live here?

old_blaggard

http://www.terragen.org - A great Terragen resource with models, contests, galleries, and forums.

mogn

I See what you mean. I lost an internal connection! :-[
So I hope I got it correct this time.

Tangled-Universe

That looks very nice  ;D
Something I'll probably never be able to, math is too much magic to me.

To get back to your primary question:

Where do you need this kind of function for? (function Atan2(x,y) )

Martin

j meyer

That's a nice polygon.Would you mind sharing the secret of
the internal connection(s)? And if you share: in simple terms
or with a visible example,please.