{"id":1278,"date":"2015-02-13T10:12:27","date_gmt":"2015-02-13T10:12:27","guid":{"rendered":"http:\/\/www.clonefactor.com\/wordpress\/?p=1278"},"modified":"2015-02-13T15:09:37","modified_gmt":"2015-02-13T15:09:37","slug":"for-loop-whirl","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/1278\/","title":{"rendered":"for loop &#8211; whirling search loop"},"content":{"rendered":"<p>how to\u00a0create a whirling search loop, with 3 for&#8230;loop,<\/p>\n<p>haven&#8217;t find any reference for this on internet may be I using the wrong keyword, so&#8230; here the thing I created.<\/p>\n<pre class=\"brush:csharp\">public bool FindFreeSpace(Vector3 point, int searchRange, out Vector3 location)\r\n{\r\n\t\/\/ init checker\r\n\tGameObject fakeObject = new GameObject();\r\n\tStructureController checker = fakeObject.AddComponent&lt;StructureController&gt;();\r\n\t\/\/ your controller should return the space is available or not.\r\n\t\r\n\t\/*********************\r\n\t\/\/ loop pattern from range 1~5\r\n\t[5][5][5][5][5][5]\r\n\t[5][3][3][3][3]\r\n\t[5][3][1][1][4]\r\n\t[5][3][1][2][4]\r\n\t[5][2][2][2][4]\r\n\t[4][4][4][4][4]\r\n\t*********************\/\r\n\tint range = searchRange;\r\n\tint dir = 1;\t\t\t\t\/\/ -1\/1\r\n\tfloat session = 0f;\t\t\/\/ record next cycle start point, e.g. Vector2(session,session) , x == z\r\n\tfloat baseX = checker.transform.localPosition.x;\r\n\tfloat baseY = checker.transform.localPosition.y;\r\n\tfloat baseZ = checker.transform.localPosition.z;\r\n\tbool found = false;\r\n\r\n\tfor (int r = 1; !found &amp;&amp; r &lt;= range; r++ )\r\n\t{\r\n\t\tint x = 0;\r\n\t\tint z = 0;\r\n\t\tfor (x = 0; !found &amp;&amp; (dir == 1 &amp;&amp; x &lt; r) || (dir == -1 &amp;&amp; x &gt; -r); x += dir)\r\n\t\t{\r\n\t\t\t\/\/ move x relative 0~r or 0~-r\r\n\t\t\tif (found = MoveChecker(checker, new Vector3(baseX + session + (float)x, baseY, baseZ + session + (float)z))) break;\r\n\t\t}\r\n\t\tfor (z = 0; !found &amp;&amp; (dir == 1 &amp;&amp; z &lt; r) || (dir == -1 &amp;&amp; z &gt; -r); z += dir)\r\n\t\t{\r\n\t\t\t\/\/ move z relative 0~r or 0~-r\r\n\t\t\tif (found = MoveChecker(checker, new Vector3(baseX + session + (float)x, baseY, baseZ + session + (float)z))) break;\r\n\t\t}\r\n\t\tsession += (float)(dir * r);\t\t\/\/ next cycle start point.\r\n\t\tdir *= -1;\t\t\t\t\t\t\t\t\/\/ change direction\r\n\t}\r\n\t\r\n\t\/\/ update output variable\r\n\tif (found)\r\n\t\tlocation = checker.transform.localPosition;\r\n\telse\r\n\t\tlocation = point;\r\n\tGameObject.DestroyImmediate(fakeObject);\r\n\treturn found;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>how to\u00a0create a whirling search loop, with 3 for&#038;# &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,4],"tags":[],"class_list":["post-1278","post","type-post","status-publish","format-standard","hentry","category-c","category-program"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1278"}],"version-history":[{"count":0,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1278\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}