Example Code: Random Area Distribution: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "{{arma}}" to "{{arma1}}")
m (Text replacement - "\[ *(https?:\/\/[^ = ]+) +([^= ]+) *\]" to "{{Link|$1|$2}}")
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
[[File:ExampleCode RandomDiscDistribution.png|thumb|From left to right:<ol><li>random radius<li>random position in a circle<li>Gaussian normal distribution</ol>]]
[[File:ExampleCode RandomDiscDistribution.png|thumb|From left to right:<ol><li>random radius<li>random position in a circle<li>Gaussian normal distribution</ol>]]
Originally from [[User:Commy2|Commy2]]'s [https://armaworld.de/index.php?thread/3796-zuf%C3%A4llige-position-erzeugen/ forum post], also fully translated on [[User:Dedmen|Dedmen]]'s [https://sqf.ovh/sqf%20math/2018/05/05/generate-a-random-position.html sqf.ovh post].
Originally from [[User:Commy2|Commy2]]'s {{Link|https://armaworld.de/index.php?thread/3796-zuf%C3%A4llige-position-erzeugen/|forum post}}, also fully translated on [[User:Dedmen|Dedmen]]'s {{Link|https://sqf.ovh/sqf%20math/2018/05/05/generate-a-random-position.html|sqf.ovh post}}.




{{ArgTitle|{{arma3}}|2|{{GVI|arma3|1.55}}}}
{{ArgTitle|2|{{arma3}}|{{GVI|arma3|1.56}}}}
{{Informative |
{{Feature | Informative |
* {{Inline code|object [[getPos]] [distance, direction]}} has been introduced in {{GVI|arma3|1.55}}
* <sqf inline>_object getPos [_distance, _direction]</sqf> has been introduced in {{GVI|arma3|1.56}}
* Gaussian {{Inline code|[[random]] [min, mid, max]}} has been introduced in {{GVI|arma3|1.55}}
* Gaussian <sqf inline>random [_min, _mid, _max]</sqf> has been introduced in {{GVI|arma3|1.56}}
}}
}}


[[private]] _center = [[getPosATL]] [[player]]; {{cc|disc center definition}}
<sqf>
[[private]] _radius = 50; {{cc|radius definition}}
private _center = getPosATL player; // disc center definition
private _radius = 50; // radius definition
{{cc|random radius}}
[[private]] _angle = [[random]] 360; {{cc|angle definition (0..360)}}
[[private]] _distance = [[random]] _radius; {{cc|distance from the center definition (0..radius)}}
[[private]] _position = _center [[getPos]] [_distance, _angle];
{{cc|random position}}
[[private]] _angle = [[random]] 360; {{cc|angle definition (0..360)}}
[[private]] _randomSquareRoot = [[sqrt]] [[random]] 1; {{cc|random square-root to obtain a non-linear 0..1 value}}
[[private]] _distance = _radius * _randomSquareRoot; {{cc|distance from the center definition (0..radius)}}
[[private]] _position = _center [[getPos]] [_distance, _angle];
{{cc|Gaussian normal distribution}}
[[private]] _angleHalf = [[random]] 180;
[[private]] _randomGauss = [[random]] [-1, 0, 1];
[[private]] _distance = _radius * _randomGauss;
[[private]] _position = _center [[getPos]] [_distance, _angleHalf];


// random radius
private _angle = random 360; // angle definition (0..360)
private _distance = random _radius; // distance from the center definition (0..radius)
private _position = _center getPos [_distance, _angle];


{{ArgTitle|{{arma2}}|2|{{GVI|arma2|1.03}}}}
// random position
{{Informative |
private _angle = random 360; // angle definition (0..360)
private _randomSquareRoot = sqrt random 1; // random square-root to obtain a non-linear 0..1 value
private _distance = _radius * _randomSquareRoot; // distance from the center definition (0..radius)
private _position = _center getPos [_distance, _angle];
 
// Gaussian normal distribution
private _angleHalf = random 180;
private _randomGauss = random [-1, 0, 1];
private _distance = _radius * _randomGauss;
private _position = _center getPos [_distance, _angleHalf];
</sqf>
 
 
{{ArgTitle|2|{{arma2}}|{{GVI|arma2|1.03}}}}
{{Feature | Informative |
* [[getPosATL]] has been introduced in {{GVI|arma2|1.03}}
* [[getPosATL]] has been introduced in {{GVI|arma2|1.03}}
}}
}}


[[private]] ["_center", "_radius", "_angle", "_distance", "_offset", "_position"]; {{cc|private definitions}}
<sqf>
_center = [[getPosATL]] [[player]]; {{cc|disc center definition}}
private ["_center", "_radius", "_angle", "_distance", "_offset", "_position"]; // private definitions
[[private]] _radius = 50; {{cc|radius definition}}
_center = getPosATL player; // disc center definition
private _radius = 50; // radius definition
{{cc|random radius}}
_angle = [[random]] 360;
_distance = [[random]] _radius;
_offset = [<nowiki/>[[sin]] _angle * _distance, [[cos]] _angle * _distance, 0];
_position = [_center, _offset] [[call]] [[BIS_fnc_vectorAdd]];
{{cc|random position}}
_angle = [[random]] 360;
_distance = _radius * [[sqrt]] [[random]] 1;
_offset = [<nowiki/>[[sin]] _angle * _distance, [[cos]] _angle * _distance, 0];
_position = [_center, _offset] [[call]] [[BIS_fnc_vectorAdd]];


// random radius
_angle = random 360;
_distance = random _radius;
_offset = [sin _angle * _distance, cos _angle * _distance, 0];
_position = [_center, _offset] call BIS_fnc_vectorAdd;


{{ArgTitle|{{arma1}}&nbsp;/&nbsp;{{ofpr}}|2|{{GVI|arma|1.00}}&nbsp;{{GVI|ofpr|1.85}}}}
// random position
{{Informative |
_angle = random 360;
* [[getPosASL]] has been introduced in {{GVI|arma|1.00}}
_distance = _radius * sqrt random 1;
_offset = [sin _angle * _distance, cos _angle * _distance, 0];
_position = [_center, _offset] call BIS_fnc_vectorAdd;
</sqf>
 
 
{{ArgTitle|2|{{arma1}}&nbsp;/&nbsp;{{ofpr}}|{{GVI|arma1|1.00}}&nbsp;{{GVI|ofpr|1.85}}}}
{{Feature | Informative |
* [[getPosASL]] has been introduced in {{GVI|arma1|1.00}}
}}
}}


[[private]] ["_center", "_radius", "_angle", "_distance", "_offset", "_position"]; {{cc|private definitions}}
<sqf>
_center = [[getPos]] [[player]]; {{cc|disc center definition - [[getPosASL]] can be used in {{arma1}}}}
private ["_center", "_radius", "_angle", "_distance", "_offset", "_position"]; // private definitions
[[private]] _radius = 50; {{cc|radius definition}}
_center = getPos player; // disc center definition - getPosASL can be used in Armed Assault
private _radius = 50; // radius definition
{{cc|random radius}}
 
_angle = [[random]] 360;
// random radius
_distance = [[random]] _radius;
_angle = random 360;
_offset = [<nowiki/>[[sin]] _angle * _distance, [[cos]] _angle * _distance];
_distance = random _radius;
_position = [(_center [[select]] 0) + (_offset [[select]] 0), (_center [[select]] 1) + (_offset [[select]] 1)];
_offset = [sin _angle * _distance, cos _angle * _distance];
_position = [(_center select 0) + (_offset select 0), (_center select 1) + (_offset select 1)];
{{cc|random position}}
 
_angle = [[random]] 360;
// random position
_distance = _radius * [[sqrt]] [[random]] 1;
_angle = random 360;
_offset = [<nowiki/>[[sin]] _angle * _distance, [[cos]] _angle * _distance, 0];
_distance = _radius * sqrt random 1;
_position = [(_center [[select]] 0) + (_offset [[select]] 0), (_center [[select]] 1) + (_offset [[select]] 1)];
_offset = [sin _angle * _distance, cos _angle * _distance, 0];
_position = [(_center select 0) + (_offset select 0), (_center select 1) + (_offset select 1)];
</sqf>
 
 
{{ArgTitle|2|{{ofp}}|{{GVI|ofp|1.00}}}}
{{Feature | Informative | This is an [[SQS Syntax]] example.}}


<sqs>
; private definitions
private ["_center", "_radius", "_angle", "_distance", "_offset", "_position"]
_center = getPos player; "disc center definition"
_radius = 50; "radius definition"


{{ArgTitle|{{ofp}}|2|{{GVI|ofp|1.00}}}}
; random radius
{{Informative | This is an [[SQS syntax]] example.}}
_angle = random 360
_distance = random _radius
_offset = [sin _angle * _distance, cos _angle * _distance]
_position = [(_center select 0) + (_offset select 0), (_center select 1) + (_offset select 1)]


[[private]] ["_center", "_radius", "_angle", "_distance", "_offset", "_position"] {{cc|private definitions}}
; random position
_center = [[getPos]] [[player]] {{cc|disc center definition}}
_angle = random 360
_radius = 50 {{cc|radius definition}}
_distance = _radius * sqrt random 1
_offset = [sin _angle * _distance, cos _angle * _distance, 0]
{{cc|random radius}}
_position = [(_center select 0) + (_offset select 0), (_center select 1) + (_offset select 1)]
_angle = [[random]] 360
</sqs>
_distance = [[random]] _radius
_offset = [<nowiki/>[[sin]] _angle * _distance, [[cos]] _angle * _distance]
_position = [(_center [[select]] 0) + (_offset [[select]] 0), (_center [[select]] 1) + (_offset [[select]] 1)]
{{cc|random position}}
_angle = [[random]] 360
_distance = _radius * [[sqrt]] [[random]] 1
_offset = [<nowiki/>[[sin]] _angle * _distance, [[cos]] _angle * _distance, 0]
_position = [(_center [[select]] 0) + (_offset [[select]] 0), (_center [[select]] 1) + (_offset [[select]] 1)]




[[Category: Example Code]]
[[Category: Example Code]]

Latest revision as of 16:09, 28 April 2023

From left to right:
  1. random radius
  2. random position in a circle
  3. Gaussian normal distribution

Originally from Commy2's forum post, also fully translated on Dedmen's sqf.ovh post.


Arma 3

  • _object getPos [_distance, _direction] has been introduced in Arma 3 logo black.png1.56
  • Gaussian random [_min, _mid, _max] has been introduced in Arma 3 logo black.png1.56

private _center = getPosATL player; // disc center definition private _radius = 50; // radius definition // random radius private _angle = random 360; // angle definition (0..360) private _distance = random _radius; // distance from the center definition (0..radius) private _position = _center getPos [_distance, _angle]; // random position private _angle = random 360; // angle definition (0..360) private _randomSquareRoot = sqrt random 1; // random square-root to obtain a non-linear 0..1 value private _distance = _radius * _randomSquareRoot; // distance from the center definition (0..radius) private _position = _center getPos [_distance, _angle]; // Gaussian normal distribution private _angleHalf = random 180; private _randomGauss = random [-1, 0, 1]; private _distance = _radius * _randomGauss; private _position = _center getPos [_distance, _angleHalf];


Arma 2

private ["_center", "_radius", "_angle", "_distance", "_offset", "_position"]; // private definitions _center = getPosATL player; // disc center definition private _radius = 50; // radius definition // random radius _angle = random 360; _distance = random _radius; _offset = [sin _angle * _distance, cos _angle * _distance, 0]; _position = [_center, _offset] call BIS_fnc_vectorAdd; // random position _angle = random 360; _distance = _radius * sqrt random 1; _offset = [sin _angle * _distance, cos _angle * _distance, 0]; _position = [_center, _offset] call BIS_fnc_vectorAdd;


Armed Assault / Operation Flashpoint: Resistance

private ["_center", "_radius", "_angle", "_distance", "_offset", "_position"]; // private definitions _center = getPos player; // disc center definition - getPosASL can be used in Armed Assault private _radius = 50; // radius definition // random radius _angle = random 360; _distance = random _radius; _offset = [sin _angle * _distance, cos _angle * _distance]; _position = [(_center select 0) + (_offset select 0), (_center select 1) + (_offset select 1)]; // random position _angle = random 360; _distance = _radius * sqrt random 1; _offset = [sin _angle * _distance, cos _angle * _distance, 0]; _position = [(_center select 0) + (_offset select 0), (_center select 1) + (_offset select 1)];


Operation Flashpoint

This is an SQS Syntax example.

; private definitions private ["_center", "_radius", "_angle", "_distance", "_offset", "_position"] _center = getPos player; "disc center definition" _radius = 50; "radius definition" ; random radius _angle = random 360 _distance = random _radius _offset = [sin _angle * _distance, cos _angle * _distance] _position = [(_center select 0) + (_offset select 0), (_center select 1) + (_offset select 1)] ; random position _angle = random 360 _distance = _radius * sqrt random 1 _offset = [sin _angle * _distance, cos _angle * _distance, 0] _position = [(_center select 0) + (_offset select 0), (_center select 1) + (_offset select 1)]