atan2: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
No edit summary
m (Some wiki formatting)
 
(117 intermediate revisions by 17 users not shown)
Line 1: Line 1:
[[Category:Scripting Commands|ATAN2]]
{{RV|type=command
[[Category:Scripting Commands OFP 1.97|ATAN2]]
[[Category:Scripting Commands OFP 1.46|ATAN2]]
[[Category:Scripting Commands ArmA|ATAN2]]


|game1= ofp
|version1= 1.00


<h2 style="color:#000066">'''''x'' atan2 ''y'''''</h2>
|game2= ofpe
|version2= 1.00


|game3= arma1
|version3= 1.00


'''Operand types:'''
|game4= arma2
|version4= 1.00


'''x:''' [[Number]]
|game5= arma2oa
|version5= 1.50


'''y:''' [[Number]]
|game6= tkoh
|version6= 1.00


'''Type of returned value:'''
|game7= arma3
|version7= 0.50


[[Number]]
|gr1= Math - Geometry


'''Description:'''
|descr= [[File:atan.jpg|right|300px]] ArcTangent of ''y/x''. Used to determine the angle of a vector ''[y,x]''. Result in [[Number#Degrees|Degrees]] between -180 and 180.
{{Feature|informative|This command can handle ''x'' being 0, unlike when using [[atan]], and will return 90}}
{{Feature|important|
This command has [[SQF Syntax#Rules of Precedence|higher precedence]] than the [[select]] command, therefore beware of the case below:
<sqf notrim>
_pos select 0  atan2 (_pos select 1) // error
(_pos select 0) atan2 (_pos select 1) // OK
</sqf>


Returns the arctangens of '''x/y'''.
Alternatively, consider using the [[a hash b|#]] operator: <sqf inline>_pos # 0 atan2 _pos # 1</sqf>
}}


Returned value is in degrees, in the range -180 to +180, using the signs of both parameters to determine the quadrant of the return value.
|s1= y [[atan2]] x


Positive angles are clockwise and negative angles are counter-clockwise.
|p1= y: [[Number]]


|p2= x: [[Number]]


'''Example:'''
|r1= [[Number]]


_angle = 5 '''atan2''' 3 ..........Result is 59.0362
|x1= <sqf>
_yx = [5,3];
_degrees = (_yx select 0) atan2 (_yx select 1); // 59.0362
</sqf>


|x2= Get direction from _obj1 to _obj2:
<sqf>
_vd = getPosASL _obj2 vectorDiff getPosASL _obj1;
_dir = (_vd select 0) atan2 (_vd select 1); // _dir range from -180 to +180
_dir = (_dir + 360) % 360; // _dir range from 0 to 360
</sqf>


'''Notes:'''<br>
|x3= Get relative direction from _obj1 to _obj2:
'''atan2''' is safer than the alternative [[atan]].  This is because '''atan2''' will correctly return the angle when the tangent is infinite:<br>
<sqf>
1 '''atan2''' 0<br>
_yx = _obj1 worldToModel getPosASL _obj2;
will correctly return the answer 90.<br>
_dir = (_yx select 0) atan2 (_yx select 1); // _dir range from -180 to +180
[[atan]] 1/0 will return a Divide by Zero Error
_dir = (_dir + 360) % 360; // _dir range from 0 to 360
</sqf>
 
|seealso= [[atan]] [[tan]] [[sin]] [[cos]] [[asin]] [[acos]] [[rad]] [[pi]] [[vectorCos]] [[BIS_fnc_dirTo]] [[getRelPos]] [[getRelDir]]
}}

Latest revision as of 10:38, 25 March 2024

Hover & click on the images for description

Description

Description:
atan.jpg
ArcTangent of y/x. Used to determine the angle of a vector [y,x]. Result in Degrees between -180 and 180.
This command can handle x being 0, unlike when using atan, and will return 90
This command has higher precedence than the select command, therefore beware of the case below:

_pos select 0 atan2 (_pos select 1) // error (_pos select 0) atan2 (_pos select 1) // OK

Alternatively, consider using the # operator: _pos # 0 atan2 _pos # 1
Groups:
Math - Geometry

Syntax

Syntax:
y atan2 x
Parameters:
y: Number
x: Number
Return Value:
Number

Examples

Example 1:
_yx = [5,3]; _degrees = (_yx select 0) atan2 (_yx select 1); // 59.0362
Example 2:
Get direction from _obj1 to _obj2:
_vd = getPosASL _obj2 vectorDiff getPosASL _obj1; _dir = (_vd select 0) atan2 (_vd select 1); // _dir range from -180 to +180 _dir = (_dir + 360) % 360; // _dir range from 0 to 360
Example 3:
Get relative direction from _obj1 to _obj2:
_yx = _obj1 worldToModel getPosASL _obj2; _dir = (_yx select 0) atan2 (_yx select 1); // _dir range from -180 to +180 _dir = (_dir + 360) % 360; // _dir range from 0 to 360

Additional Information

See also:
atan tan sin cos asin acos rad pi vectorCos BIS_fnc_dirTo getRelPos getRelDir

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note