atan2: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "\|seealso= *\[\[([^ ]+)\]\], \[\[([^ ]+)\]\]" to "|seealso= $1 $2")
(swap x and y to avoid confusion, since tangent is typically shown as y/x, not x/y)
Line 24: Line 24:
|gr1= Math - Geometry
|gr1= Math - Geometry


|descr= [[Image:atan.jpg|right|200px]] ArcTangent of ''x/y''. Used to determine the angle of a vector ''[x,y]''. Result in [[Number#Degrees|Degrees]] between -180 and 180.
|descr= [[Image:atan.jpg|right|200px]] ArcTangent of ''y/x''. Used to determine the angle of a vector ''[x,y]''. Result in [[Number#Degrees|Degrees]] between -180 and 180.
{{Feature | Informative | This command can handle ''y'' being 0, unlike when using [[atan]], and will return 90 }}
{{Feature | Informative | This command can handle ''x'' being 0, unlike when using [[atan]], and will return 90 }}
{{Feature | important | Even though this command is a binary operator just like [[select]] command, it has [[SQF Syntax#Rules_of_Precedence | higher precedence]] than [[select]] command, therefore the following expression: <br>{{hl|_pos select 0 [[atan2]] (_pos select 1)}}<br> will produce an error. The correct usage in this case will be: <br>{{hl|(_pos select 0) [[atan2]] (_pos select 1)}}}}
{{Feature | important | Even though this command is a binary operator just like [[select]] command, it has [[SQF Syntax#Rules_of_Precedence | higher precedence]] than [[select]] command, therefore the following expression: <br>{{hl|_pos select 0 [[atan2]] (_pos select 1)}}<br> will produce an error. The correct usage in this case will be: <br>{{hl|(_pos select 0) [[atan2]] (_pos select 1)}}}}


|s1= x [[atan2]] y
|s1= y [[atan2]] x


|p1= x: [[Number]]
|p1= y: [[Number]]


|p2= y: [[Number]]
|p2= x: [[Number]]


|r1= [[Number]]
|r1= [[Number]]
Line 41: Line 41:
|x2= Get direction from _obj1 to _obj2:<code>_vd = [[getPosASL]] _obj2 [[vectorDiff]] [[getPosASL]] _obj1;
|x2= Get direction from _obj1 to _obj2:<code>_vd = [[getPosASL]] _obj2 [[vectorDiff]] [[getPosASL]] _obj1;
_dir = (_vd [[select]] 0) [[atan2]] (_vd [[select]] 1); {{cc|_dir range from -180 to +180}}
_dir = (_vd [[select]] 0) [[atan2]] (_vd [[select]] 1); {{cc|_dir range from -180 to +180}}
[[if]] (_dir < 0) [[then]] {_dir = 360 + _dir}; {{cc|_dir range from 0 to 360}}</code>
_dir = (_dir + 360) % 360; {{cc|_dir range from 0 to 360}}</code>


|x3= Get relative direction from _obj1 to _obj2:<code>_xy = _obj1 [[worldToModel]] [[getPosASL]] _obj2;
|x3= Get relative direction from _obj1 to _obj2:<code>_xy = _obj1 [[worldToModel]] [[getPosASL]] _obj2;
_dir = (_xy [[select]] 0) [[atan2]] (_xy [[select]] 1); {{cc|_dir range from -180 to +180}}
_dir = (_xy [[select]] 0) [[atan2]] (_xy [[select]] 1); {{cc|_dir range from -180 to +180}}
[[if]] (_dir < 0) [[then]] {_dir = 360 + _dir}; {{cc|_dir range from 0 to 360}}</code>
_dir = (_dir + 360) % 360; {{cc|_dir range from 0 to 360}}</code>


|seealso= [[atan]] [[tan]] [[sin]] [[cos]] [[asin]] [[acos]] [[rad]] [[pi]] [[vectorCos]] [[getPos]] [[getRelPos]] [[Math Commands]]
|seealso= [[atan]] [[tan]] [[sin]] [[cos]] [[asin]] [[acos]] [[rad]] [[pi]] [[vectorCos]] [[getPos]] [[getRelPos]] [[Math Commands]]

Revision as of 13:08, 23 March 2022

Hover & click on the images for description

Description

Description:
atan.jpg
ArcTangent of y/x. Used to determine the angle of a vector [x,y]. Result in Degrees between -180 and 180.
This command can handle x being 0, unlike when using atan, and will return 90
Even though this command is a binary operator just like select command, it has higher precedence than select command, therefore the following expression:
_pos select 0 atan2 (_pos select 1)
will produce an error. The correct usage in this case will be:
(_pos select 0) atan2 (_pos select 1)
Groups:
Math - Geometry

Syntax

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

Examples

Example 1:
_xy = [5,3]; _degrees = (_xy select 0) atan2 (_xy 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:_xy = _obj1 worldToModel getPosASL _obj2; _dir = (_xy select 0) atan2 (_xy 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 getPos getRelPos Math Commands

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
Posted on 08:00, 18 November 2009
KeV
To get the direction of an object from the player: _dir = ((getPos _obj select 0) - (getPos player select 0)) atan2 ((getPos _obj select 1) - (getPos player select 1)); //_dir will be from -180 to 180. If positive values are needed then use: if (_dir < 0) then {_dir = _dir + 360}; Or just use BIS_fnc_dirTo directly.