I want to change my wind bearing in the 'dayfile' to compass references for easier reading on the website. I've put it all in a grid for users to scroll through, etc.
I've had a 'google' but I just can't get to a site that will help me!
thanks
Code: Select all
function BearingToCP(bearing: integer): string;
// Converts bearing in degrees to compass point
var
compassp: array[0..15] of string = ('N', 'NNE', 'NE', 'ENE', 'E',
'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW');
begin
Result := compassp[(((bearing * 100) + 1125) mod 36000) div 2250];
end;Perfect, thankssteve wrote:This is how Cumulus does it. It's Delphi (Pascal) but hopefully self-explanatory![]()
Code: Select all
......
var langWindDir = new Array( /* used for alt and title tags on wind dir arrow and wind direction display */
"N", "NNE", "NE", "ENE",
"E", "ESE", "SE", "SSE",
"S", "SSW", "SW", "WSW",
"W", "WNW", "NW", "NNW");
var langWindVerbose = new Array( /* can be used in text for wind direction */
"North", "North-Northeast", "Northeast", "East-Northeast",
"East", "East-Southeast", "Southeast", "South-Southeast",
"South", "South-Southwest", "Southwest", "West-Southwest",
"West", "West-Northwest", "Northwest", "North-Northwest");
function windDirLang ($winddir) // user language NNW
// Take wind direction value, return the
// text label based upon 16 point compass -- function by beeker425
// see http://www.weather-watch.com/smf/index.php/topic,20097.0.html
{
return langWindDir[Math.floor(((parseInt($winddir) + 11) / 22.5) % 16 )];
} // END function windDirLang
function windDirVerbose ($winddir) // user language North-Northwest
// Take wind direction value, return the VERBOSE
// text label based upon 16 point compass -- function by beeker425
{
return langWindVerbose[Math.floor(((parseInt($winddir) + 11) / 22.5) % 16 )];
} // END function windDirVerbose
...
Code: Select all
<script>
document.write(windDirLang(degrees) );
</script>
Actually, there's a bug in that algorithm. Try it for 359 degrees.skyewright wrote:In addition to steve's code, Wikipedia's Boxing the compass page outlines an algorithm for converting degrees to a compass point.
I presume they just forgot to mention that because the whole thing "wraps around", 32 should be considered the same as 0 in this case?steve wrote:Actually, there's a bug in that algorithm. Try it for 359 degrees.skyewright wrote:In addition to steve's code, Wikipedia's Boxing the compass page outlines an algorithm for converting degrees to a compass point.
Code: Select all
int cardinalDirectionNumber( static_cast<int>( ( ( currentGustDirection / 360.0 ) * 16 ) + 0.5 ) % 16 );
const char* const cardinalDirectionTexts[] = { "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "SW", "WNW", "NW", "NNW" };
string dirString = cardinalDirectionTexts[ cardinalDirectionNumber ];