Is there something more efficient than this?
float Game::Delta2Angle(int x, int z){
if ( x > 0 && z == 0 )
return 0;
if ( x > 0 && z > 0 )
return 45;
if ( x == 0 && z > 0 )
return 90;
if ( x < 0 && z > 0 )
return 135;
if ( x < 0 && z ==0 )
return 180;
if ( x < 0 && z < 0 )
return 225;
if ( x == 0 && z < 0 )
return 270;
if ( x > 0 && z < 0 )
return 315;
}
Is there something more efficient than this?
Other urls found in this thread:
en.cppreference.com
open-std.org
twitter.com
What the fuck is that even supposed to do? Anyway anything related to angles between vectors is done taking the dot product
It's ugly but it should be fast as fuck
this belongs in /dpt/
condense the identical conditions and use some elses
good point ,but , can you see a relation in order to write a polynomial function ? is just for curiosity
this is a trivial discrete case that would requiere float point aritmetic.
check what operator(,==) is faster (may be diferent for different values), always use it as the first one in if statements
also, look at binary representation of the values, you could probably organize those if statements in an order from the fastest go execute to the slowest
or, actually, do some statistics on which values are being return the most often and arrange the order according to the results
there is no point though, as it'll be such a insignifican't difference you won't be able to see it (or maybe even measure it)
define a const array with those values and then just pass in the appropriate values for each case, then you don't have to do potentially 8 comparisons. :shrug:
This combined with a range check for rounding, but it would be slower.
en.cppreference.com
But I mean, in the continous case this could be resumed to arctan( z/x ) ... is there a discrete equivalente and avoid arctan? because arctan internallly uses an iterative algorithm or lookup tables doesn't it?
>en.cppreference.com
yes I know thats what I want to avoid.
this shouldn't even compile since it can't return anything if both values are 0
It compiles and works on C++11/14 compilers because the input is never 0 0
Not returning a value from a value-returning function leads to undefined behavior.
you shouldn't use c++ if your programming skill is that low
either way if the values are always in a certain range (-1,0,1) you could just calculate an index from them and get the result from a switch statement
ayylmao I am fluent in C++ programming, you just don't get the question here.
so you are wondering if it can be calculated without if statements?
probably, but i don't wanna think about it
yes just as discrete math curiosity
and write a polynomial form.
* on second though it can be calculated directly if you treat your input as a direction vector
but i'm not sure it will be more efficient to do so
lets find a function with the form
(z > 0 )*a + (z == 0) *b + (z < 0 )*c + (x == 0)*d + (x < 0)*e + (x > 0)*f = Expected Angle.
In C# I would just build a Vector2, use some Math function and multiply the result by a constant value to turn it back into degrees :^)
do you want me to kill you IRL?
if (x > 0) {
if (z ==0) {
}
else if (z > 0) {
}
}
...
return 45 * ((x && y) + 2 * (y > 0 && x 0) + 4 * (y < 0 || y == 0 && x < 0));
>doing more calculations is faster than a couple simple boolean checks that are optimized by both the compiler and the hardware of the cpu itself
beautiful!!!
sugoi, but I think one of the cases is not working correctly.
nvm it werks.
you can't even realize the implications of this solution
are you envious?
The final (correct) solution ends up being an implementation of atan2, which is shit.
test
Sometimes programs are ugly. What does it do anyway? I get that it has to do with angles but that's it.
int newx = -(x < 0) + (x > 0);
int newz = -(z < 0) + (z > 0);
return (!newx && !(newz-1)) ? 90 : (225 + 45*(newx+1) + 45*newx*(newz+1)) % 360;
it is the discretion version of arctan
ayylmao thats messy but still interesting.