Archive: polar to cartesian?


4th December 2002 22:46 UTC

polar to cartesian?
Is there a good formula to convert polar coordinates to cartesian?
In some DMs, I want to be able to move the effect around. I tried searching the web, but found no good answers.


4th December 2002 22:50 UTC

x=cos(r)*d;y=sin(r)*d;

r=atan2(y,x);
d=sqrt(sqr(x)+sqr(y));

Though in AVS, the zero angle axis is oriented differently..


5th December 2002 01:47 UTC

Yeah. That's it.

Here's site with a whole buch of equation and such. I've found it usefull in the past.

http://mathworld.wolfram.com/


5th December 2002 06:40 UTC

In AVS:

r=atan2(x,-y);
d=sqrt(x*x+y*y)/sqrt(2); (if that doesn't work right then switch /sqrt(2) to *sqrt(2))


5th December 2002 15:13 UTC

Btw atero, sqr(x) is faster than x*x I believe.


5th December 2002 19:19 UTC

uhh...no, you of all people should know that calling upon a function is faster than using basic operators


5th December 2002 20:19 UTC

Here's the deal... AVS is NOT a typical programming language. It's a high-level scripting language, which means it's miles and miles away from the instructions that the CPU receives. Obviously you haven't heard of a little thing called inline operators.

First of all, having to type a*b or multiply(a,b) is just a difference in syntax. So just because an operation is defined using a function-like syntax doesn't mean that the code for that operation is stored in one place in memory that is jumped to and from every time it's needed. Just like the multiply and addition operator, the square operator's code is repeated every time it's needed, because the increase in memory is less important than the slowdown you'd get from having to jump around the code (effectively destroying the instruction cache every time the operator is needed).

Secondly, AVS has little or no optimizations. So, using 'a*a' will most like cause it to load the variable 'a' twice. By using sqr(), you explicitly indicate that you want to square the variable, which means it knows it has to perform a multiplication with the same variable on both sides, which means it only has to fetch it from memory once.

This goes for most AVS 'functions' actually. The sine function is a built-in cpu instruction, so it's very likely that if I do sin(a), then the compiled code would simply put a sine instruction right after loading the variable 'a', and not call a separate function in memory for that.


6th December 2002 04:30 UTC

Speaking of which, which is faster, integer divide or floating-point multiply?


6th December 2002 06:10 UTC

I think Atero treats all constants to floating point (considering the integer instructions are explicitly marked as so in the help), so a multiply would be faster.


6th December 2002 06:36 UTC

Little Freudian slip there, UnsToneD? :p


6th December 2002 11:58 UTC

Not really, that's more like 4am affecting my typing skills while chatting with you. I was completely clear of any influencing substances at the time :p. In fact I haven't smoked for 2 weeks because exams are nearing and I've got a lot to do.


6th December 2002 18:45 UTC

Originally posted by Atero
uhh...no, you of all people should know that calling upon a function is faster than using basic operators
An operator is a function. If they both do the exact same thing then neither should be faster than the other.

6th December 2002 19:08 UTC

Schouldnt lower level functions be faster than higher level ones.
+ - are faster than * / which are faster than ^ so a*a is faster than using sqr(a).


6th December 2002 20:20 UTC

nixa: what I wanted to explain is that AVS is so far from the code that the CPU receives, that you can't talk about how things work by guessing.

If sqr() was slower than multiply, would they have left it in? No... by using sqr(), you tell AVS you want to square a number. By using a*a, you're telling AVS you want to multiply two numbers (which just happen to be identical, but AVS doesn't notice that).

If it loads the var twice, the cpu has to wait longer and so it slows the calculation.