1 using System.Collections.Generic;
4 namespace droid.Runtime.Utilities.Sampling {
11 List<Vector3> _random_walk_positions;
14 List<Vector3> _all_possible_directions =
new List<Vector3> {
15 new Vector3(0f, 0f, 1f),
16 new Vector3(0f, 0f, -1f),
17 new Vector3(-1f, 0f, 0f),
18 new Vector3(1f, 0f, 0f)
22 if (Input.GetKeyDown(KeyCode.Return)) {
23 this._random_walk_positions = this.GenerateSelfAvoidingRandomWalk();
29 if (this._random_walk_positions != null && this._random_walk_positions.Count > 1) {
30 for (var i = 1; i < this._random_walk_positions.Count; i++) {
31 Debug.DrawLine(this._random_walk_positions[i - 1], this._random_walk_positions[i]);
38 var start_pos = Vector3.zero;
40 var current_node =
new WalkNode(start_pos, null,
new List<Vector3>(this._all_possible_directions));
46 var visited_nodes =
new List<Vector3> {start_pos};
50 if (steps_so_far == this.stepsToTake) {
57 while (current_node._PossibleDirections.Count == 0) {
58 current_node = current_node._PreviousNode;
67 var random_dir_pos =
Random.Range(0, current_node._PossibleDirections.Count);
69 var random_dir = current_node._PossibleDirections[random_dir_pos];
72 current_node._PossibleDirections.RemoveAt(random_dir_pos);
75 var next_node_pos = current_node._Pos + random_dir;
78 if (!this.HasVisitedNode(next_node_pos, visited_nodes)) {
80 current_node =
new WalkNode(next_node_pos,
82 new List<Vector3>(this._all_possible_directions));
84 visited_nodes.Add(next_node_pos);
91 var random_walk_positions =
new List<Vector3>();
93 while (current_node._PreviousNode != null) {
94 random_walk_positions.Add(current_node._Pos);
96 current_node = current_node._PreviousNode;
99 random_walk_positions.Add(current_node._Pos);
102 random_walk_positions.Reverse();
104 return random_walk_positions;
108 bool HasVisitedNode(Vector3 pos, List<Vector3> list_pos) {
109 var has_visited =
false;
111 foreach (var t
in list_pos) {
112 var dist_sqr = Vector3.SqrMagnitude(pos - t);
115 if (dist_sqr < 0.001f) {
130 public WalkNode _PreviousNode;
133 public List<Vector3> _PossibleDirections;
135 public WalkNode(Vector3 pos, WalkNode previous_node, List<Vector3> possible_directions) {
138 this._PreviousNode = previous_node;
140 this._PossibleDirections = possible_directions;
List< Vector3 > GenerateSelfAvoidingRandomWalk()