Home > Back-end >  How to calculate winddirection from degrees?
How to calculate winddirection from degrees?

Time:01-08

I'm reading (JSON) weatherdata into a small delphi-application. The winddirection is represented by a floatvalue from 0-360. What i want is to calculate this value into 8 directions (N,NE,E,NW,S,SE,W,SW) on the compass and show them on my applicationform as a arrowsymbol. I can use a lot of if..then to solve this, but it would be much cleaner code to just calculate it. My mathematical skills is not what they used to be, so i hope some of you coluld help me? Thanks.

CodePudding user response:

You can use this formula: direction = (int)((windDegrees / 45) 0.5) % 8;

This will give you a value from 0 to 7, representing the 8 compass directions. We start counting at 0.

Heres an super simple example :

var
  windDegrees: float;
  direction: integer;
begin
  windDegrees := 190.0;
  direction := (int)((windDegrees / 45)   0.5) mod 8;
  case direction of
    0: ShowMessage('N');
    1: ShowMessage('NE');
    2: ShowMessage('E');
    3: ShowMessage('SE');
    4: ShowMessage('S');
    5: ShowMessage('SW');
    6: ShowMessage('W');
    7: ShowMessage('NW');
  end;
end;

This will display a message box with the string "S".

You can then use the direction variable to display the appropriate arrow symbol on your application form.

CodePudding user response:

Not deplhi but perhaps something like this?

winds=["N","NE","E","SE","S","SW","W","NW","N"]
wind_={WIND_IN_DEGREES}
index=int(round(wind_/45,0))
print(winds[index])
  • Related