0

Technology

MySQL

Problem

Given is a radian value that defines the direction.

This radian can be easily converted to degrees (f.e. 1 radian = 57.296°). But this would result in the following:

  • 0° would match East
  • 90° would match North
  • 180° would match West
  • ...

Desired outcome

When given a radian value the degree is returned where the result is the following:

  • 0° would match North
  • 90° would match East
  • 180° would match South
  • 270° would match West
  • ...

1 Answers1

0

Solution

Degrees

The formula to convert a degree where 0° = East and going counterclockwise to degrees where 0° = North and going clockwise is the following:

F(c) = (-(c-90)) % 360

c = input in degrees F(c) = output in degrees

This can be translated to SQL as following:

IF(IF(((-(DEGREES(@input)-90))%360) > 0, ((-(DEGREES(@input)-90))%360), (((-(DEGREES(@input)-90))%360)+360)))

@input = value in radians

The if statement is present because the modulo (%) of a negative value in SQL would result in a negative value (see Mod negative numbers in SQL just like excel for more details)

Radians (Theta)

The formula to convert a radian where 0 = Easy and going counterclockwise to a radian where 0 = North and going clockwise is the following:

F(c) = ((-(c+π)) % 2π) - π/2

c = input in radians F(c) = output in radians