distanceSqr: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "\{\{GameCategory\|(arma[0123])\|New[ _]Scripting[ _]Commands[ _]List\}\}" to "{{GameCategory|$1|New Scripting Commands}}")
m (2.16 -> 2.18)
 
(38 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Command|Comments=
{{RV|type=command
____________________________________________________________________________________________


| arma3 |Game name=
|game1= arma3
|version1= 0.50


|0.50|Game version=
|gr1= Math - Geometry


|gr1= Math - Geometry |GROUP1=
|descr= Computes the {{Link|https://en.wikipedia.org/wiki/Euclidean_distance#Squared_Euclidean_distance|squared distance}} between two objects or positions.
____________________________________________________________________________________________
This command is faster than {{Link|distance}} by saving a {{Link|sqrt|square root}} operation, useful for mass-checking distance without the need of the distance value itself.


| Computes the [http://en.wikipedia.org/wiki/Euclidean_distance#Squared_Euclidean_distance squared distance] between two objects or positions. |DESCRIPTION=
|s1= var1 [[distanceSqr]] var2
____________________________________________________________________________________________


| var1 '''distanceSqr''' var2 |SYNTAX=
|p1= var1: [[Object]] or [[Position]]


|p1= var1: [[Object]] or [[Position]] |PARAMETER1=
|p2= var2: [[Object]] or [[Position]]


|p2= var2: [[Object]] or [[Position]]  |PARAMETER2=
|r1= [[Number]] - distance in meters or 1e10 if distance cannot be calculated


| [[Number]] Distance in meters or 1e10 if distance cannot be calculated |RETURNVALUE=
|s2= a [[distanceSqr]] b


|s2since= arma3 2.18


|x1= <code>[[player]] [[distanceSqr]] ([[leader]] [[player]])</code>|EXAMPLE1=
|p21= a: [[Number]]


|x2= <code>_distance = [[sqrt]] ((x2 - x1) ^ 2 + (y2 - y1) ^ 2 + (z2 - z1) ^ 2);
|p22= b: [[Number]]
_distanceSqr = (x2 - x1) ^ 2 + (y2 - y1) ^ 2 + (z2 - z1) ^ 2;
_distance = [x1, y1, z1] [[distance]] [x2, y2, z2];
_distanceSqr = [x1, y1, z1] [[distanceSqr]] [x2, y2, z2];
_distance = [[sqrt]] ([x1, y1, z1] [[distanceSqr]] [x2, y2, z2]);
_distanceSqr = ([x1, y1, z1] [[distance]] [x2, y2, z2]) ^ 2;</code>|EXAMPLE2=


If normal distance is calculated according to this formula:
|r2= [[Number]] - distance c calculated as <sqf inline>c = a * a + b * b</sqf>


|x1= <sqf>
private _distanceSquared = 5 * 5;
if (player distanceSqr leader player < _distanceSquared) then // similar to player distance leader player < 5
{
hint "You are less than 5m from the leader!";
};
</sqf>


squared distance is calculated according to this formula: 
|x2= <sqf>
private _distance = sqrt ((x2 - x1) ^ 2 + (y2 - y1) ^ 2 + (z2 - z1) ^ 2);
private _distanceSqr = (x2 - x1) ^ 2 + (y2 - y1) ^ 2 + (z2 - z1) ^ 2;
_distance = [x1, y1, z1] distance [x2, y2, z2];
_distanceSqr = [x1, y1, z1] distanceSqr [x2, y2, z2];
_distance = sqrt ([x1, y1, z1] distanceSqr [x2, y2, z2]);
_distanceSqr = ([x1, y1, z1] distance [x2, y2, z2]) ^ 2;
</sqf>


[[distanceSqr]] <nowiki>=</nowiki>
<!-- KK, your turn!
If normal distance is calculated according to this formula:


____________________________________________________________________________________________
squared distance is calculated according to this formula: 
 
-->
| [[distance]], [[vectorDistanceSqr]], [[distance2D]] |SEEALSO=


| |MPBEHAVIOUR=  
|seealso= [[distance]] [[vectorDistanceSqr]] [[distance2D]]
____________________________________________________________________________________________
}}
}}
<h3 style='display:none'>Notes</h3>
<dl class='command_description'>
<!-- Note Section BEGIN -->
<!-- Note Section END -->
</dl>
<h3 style='display:none'>Bottom Section</h3>
{{GameCategory|arma3|New Scripting Commands}}
{{GameCategory|arma3|Scripting Commands}}
[[Category:Scripting Commands|{{uc:{{PAGENAME}}}}]]

Latest revision as of 16:07, 8 February 2024

Hover & click on the images for description

Description

Description:
Computes the squared distance between two objects or positions. This command is faster than distance by saving a square root operation, useful for mass-checking distance without the need of the distance value itself.
Groups:
Math - Geometry

Syntax

Syntax:
var1 distanceSqr var2
Parameters:
var1: Object or Position
var2: Object or Position
Return Value:
Number - distance in meters or 1e10 if distance cannot be calculated

Alternative Syntax

Syntax:
a distanceSqr b
Parameters:
a: Number
b: Number
Return Value:
Number - distance c calculated as c = a * a + b * b

Examples

Example 1:
private _distanceSquared = 5 * 5; if (player distanceSqr leader player < _distanceSquared) then // similar to player distance leader player < 5 { hint "You are less than 5m from the leader!"; };
Example 2:
private _distance = sqrt ((x2 - x1) ^ 2 + (y2 - y1) ^ 2 + (z2 - z1) ^ 2); private _distanceSqr = (x2 - x1) ^ 2 + (y2 - y1) ^ 2 + (z2 - z1) ^ 2; _distance = [x1, y1, z1] distance [x2, y2, z2]; _distanceSqr = [x1, y1, z1] distanceSqr [x2, y2, z2]; _distance = sqrt ([x1, y1, z1] distanceSqr [x2, y2, z2]); _distanceSqr = ([x1, y1, z1] distance [x2, y2, z2]) ^ 2;

Additional Information

See also:
distance vectorDistanceSqr distance2D

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