atan2(y,x) snippet

Computes the arctangent angle in the Euclidean plane, given in radians. atan2 is far more superior because atan can only return an angle of -90 to +90 degrees or -pi/2 to pi/2. atan2 is mostly used on "Aimbots". I always use atan2 beside of atan when I am dealing with geometric computaions.


Method 1 SCM-Function:
Code:
{
computes atan2(y,x) where y = 0@ , x = 1@
Usage:
0AB1: @atan2 2 _floatY 23@ _floatX 5@ _floatresult 14@ // 14@ = atan2(23@,5@) where 14@ is in radians format
14@ *= 57.295779817106167876798171061675 // 14@ in degrees format(optional)

}
:atan2
if 1@ <> 0
then
    0087: 30@ = 0@ // (float)
    0073: 30@ /= 1@ // (float)
    0C08: math 31@ = arctangent 30@ // (float)
   
    if 1@ < 0 // a -x value
    then
        if 0@ < 0 // a -y value
        then 31@ -= 3.1415926535897981710616733832795
        else 31@ += 3.1415926535897981710616733832795
        end
    end
else
    if 0@ > 0
    then 31@ = 1.5707981710616766192313216916398
    else
        if 0@ < 0
        then 31@ = -1.5707981710616766192313216916398
        else 31@ = 9999999.0 // x = 0.0 , y = 0.0 ...THIS IS ACTUALLY UNDEFINED but I set it to 9999999.0 degrees that simulates positive infinity to avoid error... You can change it to either replace this with negative infinity number(super low value) or positive infinity number(super high value)
        end
    end
end
ret 1 31@

Method 2 - Memory Function:
Code:
0AA5: call 0x4207C0 num_params 2 pop 2 _x 6@ _y 14@ // atan2
0AE9: pop_float 8@ // 8@ = atan2(14@,6@)
 

ajom

Well-known member
Joined
Apr 14, 2020
Messages
389
Solutions
2
Reaction score
268
Location
Pluto
won't work w/o sfuncs, any leads?

if you do not have sampfuncs, use SCM built-in atan2. But that atan2 crashes when x = y = 0 so just make sure that they are not both equal to zero, and that will work
Method 2 - Memory Function:
Code:
0AA5: call 0x4207C0 num_params 2 pop 2 _x 6@ _y 14@ // atan2
0AE9: pop_float 8@ // 8@ = atan2(14@,6@)
 
Top