{"id":660,"date":"2021-05-12T18:59:13","date_gmt":"2021-05-12T18:59:13","guid":{"rendered":"http:\/\/meatpie\/?p=660"},"modified":"2021-05-16T03:42:02","modified_gmt":"2021-05-16T03:42:02","slug":"weather-app-wind-direction","status":"publish","type":"post","link":"https:\/\/snersbots.co.uk\/index.php\/weather-app-wind-direction\/","title":{"rendered":"Weather App &#8211; Wind Direction"},"content":{"rendered":"\n<p>The obvious way to convert the wind direction into NSWE is with a nice long switch statement. But that seemed very long winded and boring &#8211; and this is my project, I am allowed to find a more interesting way. Thanks to <a href=\"https:\/\/www.mathsisfun.com\/measure\/compass-north-south-east-west.html\">mathsisfun.com<\/a> for their excellent content on this subject.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Starting value<\/h2>\n\n\n\n<p>A number from 0 to 360. This allows for both numbers included.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Result<\/h2>\n\n\n\n<p>A string indicating wind direction &#8211; N, NE, NW etc.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Algorithm<\/h2>\n\n\n\n<p>DirectionMap = ( 0 : &#8220;N&#8221;, 1 : &#8220;E&#8221;, 2 : &#8220;S&#8221;, 3 : &#8220;W&#8221;}<\/p>\n\n\n\n<pre class=\"wp-block-verse\">DirectionPointer = Round(Input \/ 45)\nValA = Round  to integer(DirectionPointer \/ 2)\nValB = Absolute value of (ValA - 1)\n\nIf DirectionPointer is Even\n     Direction is DirectionMap[ValA]\nElse If ValB is odd\n     Direction is DirectionMap[ValA] + DirectionMap[ValB]\nElse\n      Direction is DirectionMap[ValB] + DirectionMap[ValA]    \n<\/pre>\n\n\n\n<p>And that&#8217;s it &#8211; seems to be working correctly for two letters indicating the wind direction.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Kotlin implementation<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/**<br><\/em><em> * Wind direction to string<br><\/em><em> * @param <\/em><em>windDir <\/em><em>Wind direction degrees<br><\/em><em> * @return A string representing wind direction<br><\/em><em> *\/<br><\/em>fun windDirToString(windDir: Float): String {<br><br>    val directionPointer = Math.round(windDir \/ 45).toFloat()<br>    val valA = Math.round(directionPointer \/ 2f)<br>    val valB = Math.abs(valA - 1)<br><br>    if (Math.round(directionPointer) % 2 == 0) {<br>        <em>\/\/  wind is due something<br><\/em><em>        <\/em>return getWindAngleFor(valA)<br>    } else if (valB % 2 == 1) {<br>        return getWindAngleFor(valA) + getWindAngleFor(valB)<br>    } else {<br>        return getWindAngleFor(valB) + getWindAngleFor(valA)<br>    }<br>}<br><br><em>\/**<br><\/em><em> * Get wind angle for<br><\/em><em> * @param <\/em><em>pointer <\/em><em>A number from 0 to 3<br><\/em><em> * @return Direction as string<br><\/em><em> *\/<br><\/em>fun getWindAngleFor(pointer: Int): String {<br><br>    return when (pointer % 4) {<br>        0 -&gt; WeatherConstants.NORTH<br>        1 -&gt; WeatherConstants.EAST<br>        2 -&gt; WeatherConstants.SOUTH<br>        3 -&gt; WeatherConstants.WEST<br>        else -&gt; \"\"<br>    }<br>}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The obvious way to convert the wind direction into NSWE is with a nice long switch statement. But that seemed very long winded and boring &#8211; and this is my project, I am allowed to find a more interesting way. Thanks to mathsisfun.com for their excellent content on this subject.<\/p>\n","protected":false},"author":1,"featured_media":662,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,8],"tags":[35,32,45],"class_list":["post-660","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-algorithms","category-software","tag-kotlin","tag-weather","tag-wind-direction"],"_links":{"self":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/comments?post=660"}],"version-history":[{"count":12,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/660\/revisions"}],"predecessor-version":[{"id":673,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/660\/revisions\/673"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/media\/662"}],"wp:attachment":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/media?parent=660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/categories?post=660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/tags?post=660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}